From 621cd40659e220d80e0d86efb804ae6ff4c55b74 Mon Sep 17 00:00:00 2001 From: Eragon Date: Tue, 8 Aug 2023 23:10:06 +0200 Subject: [PATCH] search: Move from hand-crafted SQL to ORM --- app/routes/search.py | 40 ++++++++++++++++++++++++--------------- app/templates/search.html | 5 ++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/routes/search.py b/app/routes/search.py index d4d3eec..e19cc10 100644 --- a/app/routes/search.py +++ b/app/routes/search.py @@ -1,31 +1,41 @@ from app import app, db from app.forms.search import AdvancedSearchForm, SearchForm from app.models.post import Post +from app.models.comment import Comment +from app.models.topic import Topic +from app.models.forum import Forum from app.utils.render import render -from sqlalchemy import text +from sqlalchemy import text, func from flask import request +def websearch_to_tsquery_multilang(search): + return func.websearch_to_tsquery('french', search) + +def to_tsvector_multilang(text): + return func.to_tsvector('french', text) + + @app.route('/rechercher') def search(): form = AdvancedSearchForm(request.args) results = list() if form.validate(): # 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 + '%'})) + tsquery = websearch_to_tsquery_multilang(form.q.data) + results = db.session.query(Topic).where( + to_tsvector_multilang(Topic.title).bool_op('@@')(tsquery) + ).group_by( + Topic.id, + Post.id + ) # 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) + res = db.session.query(Comment).where( + to_tsvector_multilang(Comment.text).bool_op('@@')(tsquery) + ).group_by( + Comment.id, + Post.id + ) + results = list(results) + list(res) return render('search.html', form=form, results=results) diff --git a/app/templates/search.html b/app/templates/search.html index 2f0a56f..2f792e2 100644 --- a/app/templates/search.html +++ b/app/templates/search.html @@ -31,7 +31,10 @@
{% for i in results %}
- {{ i.id }} {{ i.title }} {{ rank }}
+ {{ i.id }} {{ i.title }} {{ i.forum }}
+ {% if i.forum %} + {{ i.title }} + {% endif %} {{ i.headline }}
{% endfor %}