How to get nested aggregations buckets using java high level REST client Elasticsearch

前端 未结 2 485
挽巷
挽巷 2021-01-27 18:06

I have some nested fields, of which I want to calculate all distinct values, for example:

\"author\":{
  \"type\":\"nested\",
  \"properties\":{
    \"first_name         


        
相关标签:
2条回答
  • 2021-01-27 18:40

    Try this (not tested):

    Nested distinct_authornames=aggregations.get("distinct_authors");
    Terms distinct_first_names=distinct_authornames.getAggregations().get("distinct_first_names");
    
    for (Terms.Bucket bucket : distinct_first_names.getBuckets()) 
    {
        System.out.println((int) bucket.getDocCount());
        System.out.println(bucket.getKeyAsString());
    }
    

    Hope this helps

    0 讨论(0)
  • 2021-01-27 18:59

    Figured out the solution, quite long time back , but didn't realise it was working because I kept getting exception , due to some other reason. The following works well :

    Nested distinct_authorsOuter=aggregations.get("distinct_authors");
                Aggregations distinct_authors_aggs=distinct_authorsOuter.getAggregations();
    
    Terms distinct_firstNames= distinct_authors_aggs.get("distinct_first_names");
    
    0 讨论(0)
提交回复
热议问题