search: Search in topic titles and comments
This commit is contained in:
parent
b94f4c5944
commit
3a875253b4
|
@ -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()])
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue