Skip to content

Commit

Permalink
extras exists implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Nov 16, 2020
1 parent ac20ab0 commit 3d95033
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,19 @@ def should_include_retired(self):
return self.request.query_params.get(INCLUDE_RETIRED_PARAM, False) in ['true', True]

def get_extras_searchable_fields_from_query_params(self):
return {k: v for k, v in self.request.query_params.dict().items() if k.startswith('extras.')}
query_params = self.request.query_params.dict()

return {
k: v for k, v in query_params.items() if k.startswith('extras.') and not k.startswith('extras.exists')
}

def get_extras_fields_exists_from_query_params(self):
extras_exists_fields = self.request.query_params.dict().get('extras.exists', None)

if extras_exists_fields:
return extras_exists_fields.split(',')

return []

def get_search_results(self):
results = None
Expand All @@ -227,10 +239,12 @@ def get_search_results(self):
results = results.filter("match", **{field: value})

faceted_criterion = self.get_faceted_criterion()
extras_fields = self.get_extras_searchable_fields_from_query_params()
extras_fields_exists = self.get_extras_fields_exists_from_query_params()

if faceted_criterion:
results = results.query(faceted_criterion)

extras_fields = self.get_extras_searchable_fields_from_query_params()
if self.is_exact_match_on():
results = results.query(self.get_exact_search_criterion())
else:
Expand All @@ -243,6 +257,11 @@ def get_search_results(self):
results = results.filter(
"query_string", query=value, fields=[field]
)
if extras_fields_exists:
for field in extras_fields_exists:
results = results.query(
"exists", field="extras.{}".format(field)
)
results = results[0:ES_RESULTS_MAX_LIMIT]

sort_field = self.get_sort_attr()
Expand All @@ -265,7 +284,9 @@ def should_perform_es_search(self):
SEARCH_PARAM in self.request.query_params and
self.document_model and
self.get_searchable_fields()
) or bool(self.get_extras_searchable_fields_from_query_params())
) or bool(
self.get_extras_searchable_fields_from_query_params()
) or bool(self.get_extras_fields_exists_from_query_params())


class SourceChildCommonBaseView(BaseAPIView):
Expand Down

0 comments on commit 3d95033

Please sign in to comment.