@@ -470,6 +470,8 @@ async def search_msgs(
470
470
count_args = args
471
471
count_clauses = clauses
472
472
473
+ sqlite_highlights : List [str ] = []
474
+
473
475
if isinstance (self .database_engine , PostgresEngine ):
474
476
search_query = search_term
475
477
sql = """
@@ -486,7 +488,7 @@ async def search_msgs(
486
488
"""
487
489
count_args = [search_query ] + count_args
488
490
elif isinstance (self .database_engine , Sqlite3Engine ):
489
- search_query = _parse_query_for_sqlite (search_term )
491
+ search_query , sqlite_highlights = _parse_query_for_sqlite (search_term )
490
492
491
493
sql = """
492
494
SELECT rank(matchinfo(event_search)) as rank, room_id, event_id
@@ -531,9 +533,11 @@ async def search_msgs(
531
533
532
534
event_map = {ev .event_id : ev for ev in events }
533
535
534
- highlights = None
536
+ highlights : Collection [ str ] = []
535
537
if isinstance (self .database_engine , PostgresEngine ):
536
538
highlights = await self ._find_highlights_in_postgres (search_query , events )
539
+ else :
540
+ highlights = sqlite_highlights
537
541
538
542
count_sql += " GROUP BY room_id"
539
543
@@ -597,6 +601,8 @@ async def search_rooms(
597
601
count_args = list (args )
598
602
count_clauses = list (clauses )
599
603
604
+ sqlite_highlights : List [str ] = []
605
+
600
606
if pagination_token :
601
607
try :
602
608
origin_server_ts_str , stream_str = pagination_token .split ("," )
@@ -647,7 +653,7 @@ async def search_rooms(
647
653
CROSS JOIN events USING (event_id)
648
654
WHERE
649
655
"""
650
- search_query = _parse_query_for_sqlite (search_term )
656
+ search_query , sqlite_highlights = _parse_query_for_sqlite (search_term )
651
657
args = [search_query ] + args
652
658
653
659
count_sql = """
@@ -694,9 +700,11 @@ async def search_rooms(
694
700
695
701
event_map = {ev .event_id : ev for ev in events }
696
702
697
- highlights = None
703
+ highlights : Collection [ str ] = []
698
704
if isinstance (self .database_engine , PostgresEngine ):
699
705
highlights = await self ._find_highlights_in_postgres (search_query , events )
706
+ else :
707
+ highlights = sqlite_highlights
700
708
701
709
count_sql += " GROUP BY room_id"
702
710
@@ -892,19 +900,25 @@ def _tokenize_query(query: str) -> TokenList:
892
900
return tokens
893
901
894
902
895
- def _tokens_to_sqlite_match_query (tokens : TokenList ) -> str :
903
+ def _tokens_to_sqlite_match_query (tokens : TokenList ) -> Tuple [ str , List [ str ]] :
896
904
"""
897
905
Convert the list of tokens to a string suitable for passing to sqlite's MATCH.
898
906
Assume sqlite was compiled with enhanced query syntax.
899
907
908
+ Returns the sqlite-formatted query string and the tokenized search terms
909
+ that can be used as highlights.
910
+
900
911
Ref: https://www.sqlite.org/fts3.html#full_text_index_queries
901
912
"""
902
913
match_query = []
914
+ highlights = []
903
915
for token in tokens :
904
916
if isinstance (token , str ):
905
917
match_query .append (token )
918
+ highlights .append (token )
906
919
elif isinstance (token , Phrase ):
907
920
match_query .append ('"' + " " .join (token .phrase ) + '"' )
921
+ highlights .append (" " .join (token .phrase ))
908
922
elif token == SearchToken .Not :
909
923
# TODO: SQLite treats NOT as a *binary* operator. Hopefully a search
910
924
# term has already been added before this.
@@ -916,11 +930,14 @@ def _tokens_to_sqlite_match_query(tokens: TokenList) -> str:
916
930
else :
917
931
raise ValueError (f"unknown token { token } " )
918
932
919
- return "" .join (match_query )
933
+ return "" .join (match_query ), highlights
920
934
921
935
922
- def _parse_query_for_sqlite (search_term : str ) -> str :
936
+ def _parse_query_for_sqlite (search_term : str ) -> Tuple [ str , List [ str ]] :
923
937
"""Takes a plain unicode string from the user and converts it into a form
924
938
that can be passed to sqllite's matchinfo().
939
+
940
+ Returns the converted query string and the tokenized search terms
941
+ that can be used as highlights.
925
942
"""
926
943
return _tokens_to_sqlite_match_query (_tokenize_query (search_term ))
0 commit comments