diff options
author | osmarks <osmarks> | 2021-06-14 18:06:48 +0000 |
---|---|---|
committer | osmarks <osmarks> | 2021-06-14 18:06:48 +0000 |
commit | 1f447eda8ed0fb1ba7c4ff6da8d40fe56aaddabf (patch) | |
tree | f33666313dd1f356923115dda97c474c5cb0f9ea /apioforum/forum.py | |
parent | 5d394e581b108eaf1470d5d00b1699d6ade25b4c (diff) |
Implement search (SQLite FTS5)
Diffstat (limited to 'apioforum/forum.py')
-rw-r--r-- | apioforum/forum.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/apioforum/forum.py b/apioforum/forum.py index 83362fc..81674a8 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -6,6 +6,7 @@ from flask import ( g, redirect, url_for, flash ) from .db import get_db +from .mdrender import render bp = Blueprint("forum", __name__, url_prefix="/") @@ -55,3 +56,25 @@ def create_thread(): return render_template("create_thread.html") +@bp.route("/search") +def search(): + db = get_db() + query = request.args["q"] + results = db.execute(""" + SELECT posts.id, highlight(posts_fts, 0, '<mark>', '</mark>') AS content, posts.thread, posts.author, posts.created, posts.edited, posts.updated, threads.title AS thread_title + FROM posts_fts + JOIN posts ON posts_fts.rowid = posts.id + JOIN threads ON threads.id = posts.thread + WHERE posts_fts MATCH ? + ORDER BY rank + LIMIT 50 + """, (query,)).fetchall() + + display_thread_id = [ True ] * len(results) + last_thread = None + for ix, result in enumerate(results): + if result["thread"] == last_thread: + display_thread_id[ix] = False + last_thread = result["thread"] + rendered_posts = [render(q['content']) for q in results] + return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id)
\ No newline at end of file |