def query_elasticsearch(self, filters) -> list: filter_query = QueryWithFilters.generate_awards_elasticsearch_query(filters) s = AwardSearch().filter(filter_query) s.aggs.bucket( "types", "filters", filters={category: Q("terms", type=types) for category, types in all_award_types_mappings.items()}, ) results = s.handle_execute() contracts = results.aggregations.types.buckets.contracts.doc_count idvs = results.aggregations.types.buckets.idvs.doc_count grants = results.aggregations.types.buckets.grants.doc_count direct_payments = results.aggregations.types.buckets.direct_payments.doc_count loans = results.aggregations.types.buckets.loans.doc_count other = results.aggregations.types.buckets.other_financial_assistance.doc_count response = { "contracts": contracts, "direct_payments": direct_payments, "grants": grants, "idvs": idvs, "loans": loans, "other": other, } return response
def query_elasticsearch(self) -> list: filter_query = QueryWithFilters.generate_awards_elasticsearch_query( self.filters) sort_field = self.get_elastic_sort_by_fields() sorts = [{ field: self.pagination["sort_order"] } for field in sort_field] record_num = (self.pagination["page"] - 1) * self.pagination["limit"] # random page jumping was removed due to performance concerns if (self.last_record_sort_value is None and self.last_record_unique_id is not None) or (self.last_record_sort_value is not None and self.last_record_unique_id is None): # malformed request raise Exception( "Using search_after functionality in Elasticsearch requires both last_record_sort_value and last_record_unique_id." ) if record_num >= settings.ES_AWARDS_MAX_RESULT_WINDOW and ( self.last_record_unique_id is None and self.last_record_sort_value is None): raise UnprocessableEntityException( "Page #{page} with limit {limit} is over the maximum result limit {es_limit}. Please provide the 'last_record_sort_value' and 'last_record_unique_id' to paginate sequentially." .format( page=self.pagination["page"], limit=self.pagination["limit"], es_limit=settings.ES_AWARDS_MAX_RESULT_WINDOW, )) # Search_after values are provided in the API request - use search after if self.last_record_sort_value is not None and self.last_record_unique_id is not None: search = ( AwardSearch().filter(filter_query).sort(*sorts).extra( search_after=[ self.last_record_sort_value, self.last_record_unique_id ])[:self.pagination["limit"] + 1] # add extra result to check for next page ) # no values, within result window, use regular elasticsearch else: search = AwardSearch().filter(filter_query).sort( *sorts)[record_num:record_num + self.pagination["limit"]] response = search.handle_execute() return response