0

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?

4
  • 2
    Did you try some code. Please add your attempt in question. Commented Mar 28, 2016 at 6:19
  • @Sanjeev no I have not attempted anything. I am not getting any API for it Commented Mar 28, 2016 at 6:40
  • What is the issue with your code. It looks fine. Commented Mar 28, 2016 at 6:52
  • how do I get doc_count in this? Commented Mar 28, 2016 at 7:34

1 Answer 1

1

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!!

Sign up to request clarification or add additional context in comments.

1 Comment

What should I do for nested terms?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.