Wijayanga Wijekoon
Wijayanga Wijekoon

Reputation: 99

Aggregation on multiple fields - elasticsearch

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"
                        }
                      }
                    }
                  }
                }

Upvotes: 0

Views: 1492

Answers (1)

Wijayanga Wijekoon
Wijayanga Wijekoon

Reputation: 99

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();   

Upvotes: 1

Related Questions