diff --git a/src/dug/core/async_search.py b/src/dug/core/async_search.py index 1f3d5872..6946fb74 100644 --- a/src/dug/core/async_search.py +++ b/src/dug/core/async_search.py @@ -212,15 +212,10 @@ async def search_concepts(self, query, offset=0, size=None, types=None, Changed to a long boolean match query to optimize search results """ query_dict = self._build_concepts_query(query, **kwargs) - total_items = await self.es.count( - body={"query": query_dict}, - index="concepts_index") # Get aggregated counts of biolink types search_body = {"query": query_dict} search_body['aggs'] = {'type-count': {'terms': {'field': 'type'}}} - # Add post_filter on types - if types: - assert isinstance(types, list) + if isinstance(types, list): search_body['post_filter'] = { "bool": { "should": [ @@ -239,6 +234,18 @@ async def search_concepts(self, query, offset=0, size=None, types=None, size=size, explain=True ) + # Aggs/post_filter aren't supported by count + del search_body["aggs"] + if "post_filter" in search_body: + # We'll move the post_filter into the actual filter + search_body["query"]["bool"]["filter"]["bool"].update( + search_body["post_filter"]["bool"] + ) + del search_body["post_filter"] + total_items = await self.es.count( + body=search_body, + index="concepts_index" + ) # Simplify the data structure we get from aggregations to put into the # return value. This should be a count of documents hit for every type