From 3a875253b43e6f00e9c962eda47de23720236444 Mon Sep 17 00:00:00 2001 From: Eragon Date: Tue, 4 Jul 2023 23:18:58 +0200 Subject: [PATCH] search: Search in topic titles and comments --- app/forms/search.py | 8 +++++--- app/routes/search.py | 19 ++++++++++++++++--- app/templates/search.html | 3 ++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/forms/search.py b/app/forms/search.py index 576d8ea..d2dcfe7 100644 --- a/app/forms/search.py +++ b/app/forms/search.py @@ -47,10 +47,12 @@ class AdvancedSearchForm(SearchForm): return choices sortBy = SelectField('Trier', - choices={'date': ['Date croissante', + choices={'Pertinence': ['Pertinence'], + 'Date': ['Date croissante', 'Date décroissante'], - 'alphabétique': ['Alphabétique croissant', - 'Alphabétique décroissant']}, + 'Ordre Alphabétique': [ + 'Alphabétique croissant', + 'Alphabétique décroissant',]}, validators=[Optional()]) date = DateField('Date de publication', validators=[Optional()]) scope = SelectMultipleField('', choices=generate_choices, validators=[Optional()]) diff --git a/app/routes/search.py b/app/routes/search.py index 0267c81..d4d3eec 100644 --- a/app/routes/search.py +++ b/app/routes/search.py @@ -11,8 +11,21 @@ def search(): form = AdvancedSearchForm(request.args) results = list() if form.validate(): - # Search stuff - req = text("""SELECT id, text, thread_id, ts_rank_cd(textsearch, query) AS rank FROM comment, websearch_to_tsquery_multilang(:keywords) query, to_tsvector_multilang(text) textsearch WHERE query @@ textsearch ORDER BY rank DESC;""") - results = list(db.session.execute(req, {'keywords': '%' + form.q.data + '%'})); + # Topics are sorted first in results + topics = text("""SELECT topic.id, ts_headline(topic.title, query, 'StartSel=<<, StopSel=>>') AS headline, ts_rank_cd(textsearch, query) AS rank, MAX(topic.title) + FROM topic, websearch_to_tsquery_multilang(:keywords) query, to_tsvector_multilang(topic.title) textsearch + WHERE query @@ textsearch + GROUP BY topic.id,query,textsearch + ORDER BY rank DESC;""") + results = list(db.session.execute(topics, {'keywords': '%' + form.q.data + '%'})) + # Comments are less important than topics and programs + forums = text("""SELECT comment.id, ts_headline(comment.text, query, 'StartSel=<<, StopSel=>>') AS headline, ts_rank_cd(textsearch, query) AS rank, MAX(topic.title) as title + FROM comment, websearch_to_tsquery_multilang(:keywords) query, to_tsvector_multilang(comment.text) textsearch, topic, program + WHERE query @@ textsearch + AND comment.thread_id = topic.thread_id + GROUP BY comment.id,query,textsearch + ORDER BY rank DESC;""") + results.extend(list(db.session.execute(forums, {'keywords': '%' + form.q.data + '%'}))) + print(results) return render('search.html', form=form, results=results) diff --git a/app/templates/search.html b/app/templates/search.html index 79eedb8..2f0a56f 100644 --- a/app/templates/search.html +++ b/app/templates/search.html @@ -31,7 +31,8 @@
{% for i in results %}
- {{ i.id }}
{{ i.text }}
{{ i.thread_id }} + {{ i.id }} {{ i.title }} {{ rank }}
+ {{ i.headline }}
{% endfor %}