Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] tsquery: check for multiple operators #851

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/deploy-to-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ jobs:
# Wait for PostgreSQL to be ready
until docker compose exec store_pgsql pg_isready; do sleep 5; done

docker compose exec store flask db migrate
docker compose exec store flask db upgrade
docker compose exec neurostore flask db migrate
docker compose exec neurostore flask db upgrade
'

- name: Run Docker commands in compose directory if changes detected
Expand Down
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
Loading