EmilyAnonymous
EmilyAnonymous

Reputation: 21

find elasticsearch aggregation count

searchRequestBuilder.addAggregation(AggregationBuilders.terms("topCategoryFilter").field("category_id").size(AGGREGATION_SIZE) .subAggregation(AggregationBuilders.terms("subCategoryFilter").field("sub_category_id").size(AGGREGATION_SIZE)));

above is the code I have done

I need to find out elasticsearch aggregation count. for example, I searched for shoes and filters are generated based on it. In those filter eg there is 'Adidas', 'Nike' brand filters are there. I want to find out how many products are there for adidas or for Nike and want to show those results along with filter. That is Filter should show me Adidas(100) Nike(300) I am generating filters using aggregation of Elastic search. Also I am using Java API how do I achieve this?

Upvotes: 0

Views: 2308

Answers (1)

Richa
Richa

Reputation: 7649

Use this:

SearchResponse searchResponse = searchRequestBuilder.execute().actionGet()
    StringTerms stringTerms = searchResponse?.getAggregations()?.getAsMap()?.get("topCategoryFilter")
    List<StringTerms.Bucket> bucketList = stringTerms?.getBuckets()
    for(StringTerms.Bucket bucket :bucketList){
        String key = bucket.getKey()
        Integer doc_count = (Integer)bucket.getDocCount()
    }

Hope this heps!!

Upvotes: 1

Related Questions