Skip to content

Commit 7b3ac22

Browse files
committed
fix(elasticsearch): Catch APIError on multi-search requests due to bad query syntax
1 parent 51b2f7a commit 7b3ac22

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cl/lib/elasticsearch_utils.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from django.http import HttpRequest
2020
from django.http.request import QueryDict
2121
from django_elasticsearch_dsl.search import Search
22-
from elasticsearch.exceptions import RequestError, TransportError
22+
from elasticsearch.exceptions import ApiError, RequestError, TransportError
2323
from elasticsearch_dsl import A, MultiSearch, Q
2424
from elasticsearch_dsl import Search as SearchDSL
2525
from elasticsearch_dsl.connections import connections
@@ -1640,19 +1640,26 @@ def fetch_es_results(
16401640
child_total = child_doc_count_response.hits.total.value
16411641

16421642
query_time = main_response.took
1643-
error = False
16441643
search_type = get_params.get("type", SEARCH_TYPES.OPINION)
16451644
if (
16461645
main_response.aggregations
16471646
and search_type == SEARCH_TYPES.PARENTHETICAL
16481647
):
16491648
main_response = main_response.aggregations.groups.buckets
1649+
error = False
16501650
return main_response, query_time, error, parent_total, child_total
16511651
except (TransportError, ConnectionError, RequestError) as e:
1652-
logger.warning(f"Error loading search page with request: {get_params}")
1653-
logger.warning(f"Error was: {e}")
1652+
logger.warning(
1653+
"Error loading search page with request: %s", dict(get_params)
1654+
)
1655+
logger.warning("Error was: %s", e)
16541656
if settings.DEBUG is True:
16551657
traceback.print_exc()
1658+
except ApiError as e:
1659+
if "Failed to parse query" in str(e):
1660+
logger.warning("Failed to parse query: %s", e)
1661+
else:
1662+
logger.error("Multi-search API Error: %s", e)
16561663
return [], 0, error, None, None
16571664

16581665

0 commit comments

Comments
 (0)