search: Search in topic titles and comments

This commit is contained in:
Eragon 2023-07-04 23:18:58 +02:00
parent b94f4c5944
commit 3a875253b4
Signed by: Eragon
GPG Key ID: 087126EBFC725006
3 changed files with 23 additions and 7 deletions

View File

@ -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()])

View File

@ -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)

View File

@ -31,7 +31,8 @@
<section class="search-results">
{% for i in results %}
<div>
{{ i.id }}<br>{{ i.text }}<br>{{ i.thread_id }}
{{ i.id }} {{ i.title }} {{ rank }}<br>
{{ i.headline }}<br>
</div>
{% endfor %}
</section>