Search in sources :

Example 1 with InternalTerms

use of org.elasticsearch.search.aggregations.bucket.terms.InternalTerms in project alien4cloud by alien4cloud.

the class ESGenericSearchDAO method parseAggregations.

/**
 * Parse aggregations and set facets to the given non null FacetedSearchResult instance.
 *
 * @param searchResponse The search response that contains aggregation results.
 * @param facetedSearchResult The instance in which to set facets.
 * @param aggregationQueryManager If not null the data of the FacetedSearchResult will be processed from an aggregation based on the given manager.
 */
private void parseAggregations(SearchResponse searchResponse, FacetedSearchResult facetedSearchResult, IAggregationQueryManager aggregationQueryManager) {
    if (searchResponse.getAggregations() == null) {
        return;
    }
    List<Aggregation> internalAggregationsList = searchResponse.getAggregations().asList();
    if (internalAggregationsList.size() == 0) {
        return;
    }
    Map<String, FacetedSearchFacet[]> facetMap = Maps.newHashMap();
    for (Aggregation aggregation : internalAggregationsList) {
        if (aggregationQueryManager != null && aggregation.getName().equals(aggregationQueryManager.getQueryAggregation().getName())) {
            aggregationQueryManager.setData(getJsonMapper(), getClassFromTypeFunc(), facetedSearchResult, aggregation);
        } else if (aggregation instanceof InternalTerms) {
            InternalTerms internalTerms = (InternalTerms) aggregation;
            List<FacetedSearchFacet> facets = new ArrayList<>();
            for (int i = 0; i < internalTerms.getBuckets().size(); i++) {
                Terms.Bucket bucket = internalTerms.getBuckets().get(i);
                facets.add(new FacetedSearchFacet(bucket.getKey(), bucket.getDocCount()));
            }
            // Find the missing aggregation
            internalAggregationsList.stream().filter(missingAggregation -> missingAggregation instanceof InternalMissing && missingAggregation.getName().equals("missing_" + internalTerms.getName())).findAny().ifPresent(missingAggregation -> {
                InternalMissing internalMissingAggregation = (InternalMissing) missingAggregation;
                if (internalMissingAggregation.getDocCount() > 0) {
                    facets.add(new FacetedSearchFacet(null, internalMissingAggregation.getDocCount()));
                }
            });
            facetMap.put(internalTerms.getName(), facets.toArray(new FacetedSearchFacet[facets.size()]));
        } else {
            log.debug("Aggregation is not a facet aggregation (terms) ignore. Name: {} ,Type: {}", aggregation.getName(), aggregation.getClass().getName());
        }
    }
    facetedSearchResult.setFacets(facetMap);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilders(org.elasticsearch.search.sort.SortBuilders) Array(java.lang.reflect.Array) CountRequestBuilder(org.elasticsearch.action.count.CountRequestBuilder) SneakyThrows(lombok.SneakyThrows) MappingBuilder(org.elasticsearch.mapping.MappingBuilder) TopHitsBuilder(org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder) HashMap(java.util.HashMap) MapUtil(alien4cloud.utils.MapUtil) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) Function(java.util.function.Function) ISearchBuilderAdapter(org.elasticsearch.mapping.ISearchBuilderAdapter) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) SortBuilder(org.elasticsearch.search.sort.SortBuilder) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchType(org.elasticsearch.action.search.SearchType) ElasticSearchClient(org.elasticsearch.mapping.ElasticSearchClient) ElasticSearchUtil(alien4cloud.utils.ElasticSearchUtil) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Resource(javax.annotation.Resource) IOException(java.io.IOException) JsonUtil(alien4cloud.rest.utils.JsonUtil) Field(java.lang.reflect.Field) Maps(com.google.common.collect.Maps) FilterValuesStrategy(org.elasticsearch.mapping.FilterValuesStrategy) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) CombineFunction(org.elasticsearch.common.lucene.search.function.CombineFunction) QueryHelper(org.elasticsearch.mapping.QueryHelper) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryBuilderAdapter(org.elasticsearch.mapping.QueryBuilderAdapter) SortOrder(org.elasticsearch.search.sort.SortOrder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) SourceFetchContext(org.elasticsearch.mapping.SourceFetchContext) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) InternalMissing(org.elasticsearch.search.aggregations.bucket.missing.InternalMissing) FacetedSearchFacet(alien4cloud.dao.model.FacetedSearchFacet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FacetedSearchFacet (alien4cloud.dao.model.FacetedSearchFacet)1 FacetedSearchResult (alien4cloud.dao.model.FacetedSearchResult)1 GetMultipleDataResult (alien4cloud.dao.model.GetMultipleDataResult)1 JsonUtil (alien4cloud.rest.utils.JsonUtil)1 ElasticSearchUtil (alien4cloud.utils.ElasticSearchUtil)1 MapUtil (alien4cloud.utils.MapUtil)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 Array (java.lang.reflect.Array)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Resource (javax.annotation.Resource)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1