0

How to use multiple aggregation in elasticsearch. My code works for single aggregation but I want to add another aggregation. I use another aggs for that but its parsing an exception.

 { "aggs": {
                "_figures": {
                  "terms": {
                    "field": "id"
                  },
                  "aggs": {
                    "field1": {
                      "stats": {
                        "field": "field1"
                      },
                      "field2":{
                        "stats":{
                          "field":"field2"
                        }
                      }
                    }
                  }
                }
1
  • You need to put field2 parallel to field1 "aggs": { "field1": { "stats": { "field": "field1" } }, "field2": { "stats": { "field": "field2" } } } Commented Oct 23, 2019 at 4:31

1 Answer 1

1

This works for me now. Using below code I can work with multiple aggregations

 {"aggs": {
        "emp_figures": {
          "terms": {
            "field": "id"
          },
          "aggs": {
            "field1": {
              "stats": {
                "field": "field1"
              }
            },
            "field2":{
                "stats":{
                  "field":"field2"
                }
              },
            "field3":{
                "stats":{
                  "field":"field3"
                }
              }
          }
        }
      }
    }

And also this is the way to works with multiple aggregation with java

 SearchResponse getResponse = 
        client.prepareSearch( ElasticSearchConstants.INDEX ).setTypes( ElasticSearchConstants.TBL)
            .addAggregation( AggregationBuilders.terms( FIGURE)
                                    .field( "id" )
            .subAggregation( AggregationBuilders.stats( "stats" ).field( field1 ) )
            .subAggregation( AggregationBuilders.stats( "stats" ).field( field2) ).size( 100 ) )
                            .setQuery( query )
                            .setFrom( 0 )
                            .execute().actionGet();   
Sign up to request clarification or add additional context in comments.

Comments

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.