Skip to content

Commit

Permalink
MSEARCH-620 Another tenant name is displayed in "Held by" facet when …
Browse files Browse the repository at this point in the history
…location from current tenant is selected
  • Loading branch information
andrei-razumnov committed Nov 13, 2023
1 parent e7c888f commit a60823a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/folio/search/service/FacetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<String> filterNamesToKeep) {
var query = searchSource.query();
if (isBoolQuery(query)) {
((BoolQueryBuilder) query).filter().clear();
if (query instanceof BoolQueryBuilder boolQuery) {
List<QueryBuilder> 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();
}
Expand Down

0 comments on commit a60823a

Please sign in to comment.