Skip to content

Commit

Permalink
fix: handle invalid lucene query term values gracefully (#268)
Browse files Browse the repository at this point in the history
Handle invalid Lucene query term values gracefully to prevent HTTP 500
errors.
fixes: #253
  • Loading branch information
areebahmeddd authored Jan 30, 2025
1 parent 33368c7 commit 7644c29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def status_for_response(result: SearchResponse):
@app.post("/search")
def search(
response: Response, search_parameters: Annotated[PostSearchParameters, Body()]
):
) -> SearchResponse:
"""This is the main search endpoint.
It uses POST request to ensure privacy.
Expand Down
21 changes: 15 additions & 6 deletions app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from . import config
from ._types import (
DebugInfo,
ErrorSearchResponse,
QueryAnalysis,
SearchParameters,
SearchResponse,
SearchResponseDebug,
SearchResponseError,
SuccessSearchResponse,
)
from .charts import build_charts
from .exceptions import QueryCheckError
from .facets import build_facets
from .postprocessing import BaseResultProcessor, load_result_processor
from .query import build_elasticsearch_query_builder, build_search_query, execute_query
Expand Down Expand Up @@ -79,12 +82,18 @@ def search(
params.charts,
)
index_config = params.index_config
query = build_search_query(
params,
# ES query builder is generated from elasticsearch mapping and
# takes ~40ms to generate, build-it before hand to avoid this delay
es_query_builder=get_es_query_builder(params.valid_index_id),
)
try:
query = build_search_query(
params,
# ES query builder is generated from elasticsearch mapping and
# takes ~40ms to generate, build-it before hand to avoid this delay
es_query_builder=get_es_query_builder(params.valid_index_id),
)
except QueryCheckError as e:
return ErrorSearchResponse(
debug=SearchResponseDebug(),
errors=[SearchResponseError(title="QueryCheckError", description=str(e))],
)
(
logger.debug(
"Luqum query: %s\nElasticsearch query: %s",
Expand Down

0 comments on commit 7644c29

Please sign in to comment.