Vishnu
Vishnu

Reputation: 4687

elasticsearch 1.7 group by bucket with multi field concat

I have an aggregate statement that groups by firstname and buckets them with necessary fields. But I want to group by concatenation of firstname+lastname. I do not want to use nested aggregates like group by firstname and then group by lastname. How do I change the field to include a string concatenation of multiple fields?

"aggs": {
    "by_name": {
      "terms": {
        "field": "firstname"
      },
      "aggs": {
        "source": {
          "top_hits": {
            "_source": {
              "include": [
                "id","name"
              ]
            }
          }
        }
      }
    }
  }

Upvotes: 0

Views: 817

Answers (1)

Rahul
Rahul

Reputation: 16355

In ES 1.7

You may use script aggregation with terms aggregation

GET _search
{
  "size": 20,
  "aggs": {
    "con": {
      "terms": {
        "script": "doc['firstName'].value + doc['lastName'].value"
      }
    }
  }
}

For current version, ie. ES 5.2, there is bucket script aggregaton for the same purpose

Upvotes: 1

Related Questions