albert muller
albert muller

Reputation: 13

How can I perform an aggregation on multiple fields?

How can I perform an aggregation on multiple fields? That is I would like to list out the results of a terms aggregation on "field1" and "field2", not separately, but in the same bucket. Is it possible in elasticsearch?

Upvotes: 1

Views: 151

Answers (3)

Vineeth Mohan
Vineeth Mohan

Reputation: 19253

As of now there is no direct way to do multi field aggregation in Elasticsearch. But you can achieve the required results using scripts as below:

{  
    "aggs": {    
        "mixed_aggs": {      
            "terms": {        
                "script": "doc['field1'].values + doc['field2'].values"      
            }    
        }  
    }
}

You can read more about the above case in this blog

Upvotes: 1

Shalin LK
Shalin LK

Reputation: 190

This might be accomplished using script as follows :

{
  "aggs": {
    "my_agg": {
      "terms": {
        "script": "doc['field_1'].values + doc['field_2'].values"
      }
    }
  }
}

Note : Performance should be considered as combining multiple fields for aggregation comes at a cost.

read here

Upvotes: 0

Richa
Richa

Reputation: 7649

Use sub-aggregation as mentioned below:

{
"size" : 0,

"aggs": {
  "group_By_field1": {
     "terms": {
        "field": "field1"
     },
     "aggs" : {
       "group_By_Field2" : { 
            "terms": {
        "field": "field2"
     }
       }
     }
   }

 }
}

Upvotes: 0

Related Questions