Skip to content

Commit

Permalink
fix: better behavior for search filters (amundsen-io#1852)
Browse files Browse the repository at this point in the history
* fix: better behavior for application of filters

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>

* explicit comparison to None

Signed-off-by: Allison Suarez Miranda <asuarezmiranda@lyft.com>
  • Loading branch information
allisonsuarez authored and Hans Adriaans committed Jun 30, 2022
1 parent de8e5ab commit b7d1872
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions search/search_service/proxy/es_search_proxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0

import json
import logging
from typing import (
Any, Dict, List, Union,
Expand Down Expand Up @@ -198,21 +199,24 @@ def _build_filters(self, resource: Resource, filters: List[Filter]) -> List:
filter_queries: List = []

for filter in filters:
filter_name = mapping.get(filter.name) if mapping is not None \
and mapping.get(filter.name) is not None else filter.name
if mapping is not None and mapping.get(filter.name) is not None:
# only apply filter to query if field exists for the given resource
filter_name = mapping.get(filter.name)

queries_per_term = [Q(WILDCARD_QUERY, **{filter_name: term}) for term in filter.values]
queries_per_term = [Q(WILDCARD_QUERY, **{filter_name: term}) for term in filter.values]

if filter.operation == 'OR':
filter_queries.append(Q(BOOL_QUERY, should=queries_per_term, minimum_should_match=1))
if filter.operation == 'OR':
filter_queries.append(Q(BOOL_QUERY, should=queries_per_term, minimum_should_match=1))

elif filter.operation == 'AND':
for q in queries_per_term:
filter_queries.append(q)
elif filter.operation == 'AND':
for q in queries_per_term:
filter_queries.append(q)

else:
msg = f"Invalid operation {filter.operation} for filter {filter_name} with values {filter.values}"
raise ValueError(msg)
else:
msg = f"Invalid operation {filter.operation} for filter {filter_name} with values {filter.values}"
raise ValueError(msg)
LOGGER.info("Filter {filter.name} does not apply to {resource}")

return filter_queries

Expand Down Expand Up @@ -301,13 +305,13 @@ def execute_queries(self, queries: Dict[Resource, Q],
for resource in queries.keys():
query_for_resource = queries.get(resource)
search = Search(index=self.get_index_for_resource(resource_type=resource)).query(query_for_resource)
LOGGER.info(search.to_dict())

# pagination
start_from = page_index * results_per_page
end = results_per_page * (page_index + 1)

search = search[start_from:end]

LOGGER.info(json.dumps(search.to_dict()))
multisearch = multisearch.add(search)
try:
response = multisearch.execute()
Expand Down

0 comments on commit b7d1872

Please sign in to comment.