Skip to content

Commit

Permalink
[FIX] tsquery: check for multiple operators (#851)
Browse files Browse the repository at this point in the history
* add func.to_tsquery

* fix service name

* check for multiple operators

* test with lowercase
  • Loading branch information
jdkent authored Nov 18, 2024
1 parent dd34d7a commit 92d3826
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions store/neurostore/resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def validate_search_query(query: str) -> bool:
Returns:
bool: True if the query is valid, False otherwise.
"""
query = query.upper()

# Check for valid parentheses
if not validate_parentheses(query):
raise errors.SyntaxError("Unmatched parentheses")
Expand All @@ -65,6 +67,9 @@ def validate_search_query(query: str) -> bool:
if not validate_query_end(query):
raise errors.SyntaxError("Query cannot end with an operator")

if not validate_multiple_operators(query):
raise errors.SyntaxError("Consecutive operators are not allowed")

return True


Expand Down Expand Up @@ -98,6 +103,16 @@ def validate_query_end(query: str) -> bool:
return True


def validate_multiple_operators(query: str) -> bool:
"""Validate that there are no consecutive operators in a query."""
operators = ("AND", "OR", "NOT", "&", "|", "&!")
query = query.strip().split(" ")
for i in range(len(query) - 1):
if query[i] in operators and query[i + 1] in operators:
return False
return True


def count_chars(target, query: str) -> int:
"""Count the number of chars in a query string.
Excluding those in quoted phrases."""
Expand Down
4 changes: 4 additions & 0 deletions store/neurostore/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,15 @@ def simple_neurosynth_annotation(session, ingest_neurosynth):
"Unmatched parentheses",
),
('"autism" OR "ASD" OR "autistic" OR ', "Query cannot end with an operator"),
("memory and", "Query cannot end with an operator"),
(
'(("Autism Spectrum Disorder" OR "autism spectrum disorder") OR ("Autism" OR "autism") '
'OR ("ASD")) AND (("decision*" OR "Dec',
"Unmatched parentheses",
),
(
'smoking AND NOT memory', "Consecutive operators are not allowed"
)
]

valid_queries = [
Expand Down

0 comments on commit 92d3826

Please sign in to comment.