Cardinality aggregation on several fields

Hi,

I would like to count the discinct elements on the combination of two disctinct fields but the cardinality aggregation only accepts one field. Do you know how I could work my way around this without reindexing the whole index?

Thanks

You could use a script in the cardinality aggregation to add the values for each of the fields to an array and return the array. See the following for a simple example of this:

PUT test/doc/1
{
  "field1": "value1",
  "field2": "value2"
}
PUT test/doc/2
{
  "field1": "value2",
  "field2": "value3"
}
PUT test/doc/3
{
  "field1": "value1",
  "field2": "value3"
}
PUT test/doc/4
{
  "field1": "value3",
  "field2": "value4"
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "cardinality": {
      "cardinality": {
        "script": {
          "inline": "[ doc['field1'].value, doc['field2'].value ]"
        }
      }
    }
  }
}

Worked great, thanks!