Skip to content

Commit 36fe961

Browse files
committed
OpenConceptLab/ocl_issues#947 | Handling ES error of max pagination
1 parent a8adb32 commit 36fe961

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

core/common/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ class Http409(APIException):
77
status_code = status.HTTP_409_CONFLICT
88
default_detail = _('Conflict.')
99
default_code = 'conflict'
10+
11+
12+
class Http400(APIException):
13+
status_code = status.HTTP_400_BAD_REQUEST
14+
default_detail = _('Bad Request.')
15+
default_code = 'bad_request'

core/common/views.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.shortcuts import get_object_or_404
1111
from drf_yasg import openapi
1212
from drf_yasg.utils import swagger_auto_schema
13+
from elasticsearch import RequestError
1314
from elasticsearch_dsl import Q
1415
from pydash import get
1516
from rest_framework import response, generics, status
@@ -22,6 +23,7 @@
2223
from core.common.constants import SEARCH_PARAM, LIST_DEFAULT_LIMIT, CSV_DEFAULT_LIMIT, \
2324
LIMIT_PARAM, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY, INCLUDE_RETIRED_PARAM, VERBOSE_PARAM, HEAD, LATEST, \
2425
BRIEF_PARAM
26+
from core.common.exceptions import Http400
2527
from core.common.mixins import PathWalkerMixin
2628
from core.common.serializers import RootSerializer
2729
from core.common.utils import compact_dict_by_values, to_snake_case, to_camel_case, parse_updated_since_param, \
@@ -535,8 +537,13 @@ def get_search_results_qs(self):
535537
page = int(self.request.GET.get('page', '1'))
536538
start = (page - 1) * self.limit
537539
end = start + self.limit
538-
539-
return search_results[start:end].to_queryset()
540+
try:
541+
return search_results[start:end].to_queryset()
542+
except RequestError as ex:
543+
if get(ex, 'info.error.caused_by.reason', '').startswith('Result window is too large'):
544+
raise Http400(detail='Only 10000 results are available. Please apply additional filters'
545+
' or fine tune your query to get more accurate results.')
546+
raise ex
540547

541548
def is_head(self):
542549
return self.request.method.lower() == 'head'

0 commit comments

Comments
 (0)