From 881712ed29c818f18f052b88d4769f1314aa118a Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Tue, 16 Nov 2021 14:50:05 +0100 Subject: [PATCH] search optimization 4 i give up --- buscribe-api/buscribeapi/buscribeapi.py | 75 +++++++++++++++---------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/buscribe-api/buscribeapi/buscribeapi.py b/buscribe-api/buscribeapi/buscribeapi.py index e90749a..2c78810 100644 --- a/buscribe-api/buscribeapi/buscribeapi.py +++ b/buscribe-api/buscribeapi/buscribeapi.py @@ -118,36 +118,51 @@ def get_json(): def fetch_lines(db_conn, start_time, end_time, ts_query=None, limit=None, offset=None): - query = f""" - WITH q AS ( - SELECT convert_query({"%(text_query)s" if ts_query is not None else "NULL"}) - ) - (SELECT *, ts_headline(transcription_line, (SELECT * FROM q), - 'StartSel='''', StopSel=') AS highlighted_text - FROM buscribe_all_transcriptions2 - WHERE start_time >= %(start_time)s AND end_time <= %(end_time)s - {"AND verified_line_ts @@ (SELECT * FROM q)" if ts_query is not None else ""} - ORDER BY {"ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) ||" + - "coalesce(names_ts, ''::tsvector), (SELECT * FROM q)) DESC," if ts_query is not None else ""} - start_time) - UNION - (SELECT *, ts_headline(transcription_line, (SELECT * FROM q), - 'StartSel='''', StopSel=') AS highlighted_text - FROM buscribe_all_transcriptions2 - WHERE start_time >= %(start_time)s AND end_time <= %(end_time)s - {"AND machine_line_ts @@ (SELECT * FROM q)" if ts_query is not None else ""} - ORDER BY {"ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) ||" + - "coalesce(names_ts, ''::tsvector), (SELECT * FROM q)) DESC," if ts_query is not None else ""} - start_time) - """ - - if limit is not None: - query += "LIMIT %(limit)s " - - if offset is not None: - query += "OFFSET %(limit)s " - - query += ";" + if ts_query is None: + query = "SELECT *" + \ + ",transcription_line AS highlighted_text" + \ + " FROM buscribe_all_transcriptions WHERE start_time >= %(start_time)s AND end_time <= %(end_time)s " + \ + "ORDER BY start_time " + + if limit is not None: + query += "LIMIT %(limit)s " + + if offset is not None: + query += "OFFSET %(limit)s " + + query += ";" + + else: + query = f""" + WITH q AS ( + SELECT convert_query({"%(text_query)s" if ts_query is not None else "NULL"}) + ) + (SELECT *, ts_headline(transcription_line, (SELECT * FROM q), + 'StartSel='''', StopSel=') AS highlighted_text + FROM buscribe_all_transcriptions2 + WHERE start_time >= %(start_time)s AND end_time <= %(end_time)s + {"AND verified_line_ts @@ (SELECT * FROM q)" if ts_query is not None else ""} + ORDER BY {"ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) ||" + + "coalesce(names_ts, ''::tsvector), (SELECT * FROM q)) DESC," if ts_query is not None else ""} + start_time) + UNION + (SELECT *, ts_headline(transcription_line, (SELECT * FROM q), + 'StartSel='''', StopSel=') AS highlighted_text + FROM buscribe_all_transcriptions2 + WHERE start_time >= %(start_time)s AND end_time <= %(end_time)s + {"AND machine_line_ts @@ (SELECT * FROM q)" if ts_query is not None else ""} + ORDER BY {"ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) ||" + + "coalesce(names_ts, ''::tsvector), (SELECT * FROM q)) DESC," if ts_query is not None else ""} + start_time) + """ + + if limit is not None: + query += "LIMIT %(limit)s " + + if offset is not None: + query += "OFFSET %(limit)s " + + query += ";" return database.query(db_conn, query, start_time=start_time if start_time is not None else '-infinity',