henry blake
henry blake

Reputation: 63

Break the values inside a field and aggregate

I have indexed some documents like below

{
"height" : "165"
"weight": "102-kgs"
}

Now I need to aggregate the weight field,but since it is given as "102-kgs",results in erroneous aggregation. Any way to break and get the weight only?

Upvotes: 1

Views: 37

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19273

You should be employing scripts for these kind of operations. In this case the following script would get you the required results:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "weights": {
      "terms": {
        "field": "weight",
        "script": "_value.split('-')[0]"
      }
    }
  }
}

Upvotes: 1

Related Questions