diff --git a/src/main/java/org/folio/search/service/FacetService.java b/src/main/java/org/folio/search/service/FacetService.java index 0a00acfcb..910a4f3c5 100644 --- a/src/main/java/org/folio/search/service/FacetService.java +++ b/src/main/java/org/folio/search/service/FacetService.java @@ -2,6 +2,9 @@ import static org.folio.search.utils.SearchQueryUtils.isBoolQuery; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.apache.commons.collections4.CollectionUtils; @@ -12,6 +15,8 @@ import org.folio.search.repository.SearchRepository; import org.folio.search.service.converter.ElasticsearchFacetConverter; import org.opensearch.index.query.BoolQueryBuilder; +import org.opensearch.index.query.QueryBuilder; +import org.opensearch.index.query.TermQueryBuilder; import org.opensearch.search.builder.SearchSourceBuilder; import org.springframework.stereotype.Service; @@ -37,17 +42,27 @@ public FacetResult getFacets(CqlFacetRequest request) { searchSource.size(0).from(0).fetchSource(false); facetQueryBuilder.getFacetAggregations(request, searchSource.query()).forEach(searchSource::aggregation); - cleanUpFacetSearchSource(searchSource); + cleanUpFacetSearchSource(searchSource, List.of("items.effectiveLocationId")); var searchResponse = searchRepository.search(request, searchSource); return facetConverter.convert(searchResponse.getAggregations()); } private static void cleanUpFacetSearchSource(SearchSourceBuilder searchSource) { + cleanUpFacetSearchSource(searchSource, Collections.emptyList()); + } + + private static void cleanUpFacetSearchSource(SearchSourceBuilder searchSource, List filterNamesToKeep) { var query = searchSource.query(); - if (isBoolQuery(query)) { - ((BoolQueryBuilder) query).filter().clear(); + if (query instanceof BoolQueryBuilder boolQuery) { + List filtersToKeep = boolQuery.filter().stream() + .filter(TermQueryBuilder.class::isInstance) + .filter(filter -> filterNamesToKeep.contains(((TermQueryBuilder)filter).fieldName())) + .toList(); + boolQuery.filter().clear(); + boolQuery.filter().addAll(filtersToKeep); } + if (CollectionUtils.isNotEmpty(searchSource.sorts())) { searchSource.sorts().clear(); }