diff --git a/services/search/api.py b/services/search/api.py index 6997d3ce4..dce650f45 100644 --- a/services/search/api.py +++ b/services/search/api.py @@ -244,6 +244,9 @@ def build_search_query(query: str): if re.fullmatch(r"'+", and_operand): # Skip any operands that are just repeating single-quotes continue + if not and_operand.strip(): + # Skip empty operands to prevent invalid tsquery syntax like ":*" + continue if expression: expression += f" & {and_operand}:*" else: diff --git a/services/search/tests/test_api.py b/services/search/tests/test_api.py index ff3c7efa2..d76f69500 100644 --- a/services/search/tests/test_api.py +++ b/services/search/tests/test_api.py @@ -262,6 +262,16 @@ def test_search_with_vertical_bar_in_query(api_client, units): ("a, &&& , & b || || |||| |c,,,, d", "a:* & b:* | c:* & d:*"), # Expression with repeating single-quotes ("','','''',a,b'c,d''e,f'''g,','','''", "a:* & b'c:* & d''e:* & f'''g:*"), + # Empty operands should be skipped to prevent invalid ":*" syntax + (" ", ""), + (" | ", ""), + (" & ", ""), + (" , ", ""), + ("a | | b", "a:* | b:*"), + ("a & & b", "a:* & b:*"), + ("a, ,b", "a:* & b:*"), + (" | a", "a:*"), + ("a | ", "a:*"), ], ) def test_build_search_query(query, expected):