Search code examples
elasticsearchelasticsearch-painless

Elastic Search, use scripted field for an aggregation result


I would like to know if it is possible to have a scripted_field in the result of an aggregation:

An example query:

GET /index/_search
{
  "query": {
    "match_all": {}
  },
  "agg": {
    "test": {
      "terms": {
        "field": "key"
      }
    }
  }
}

The result I would like to archive is the following:

{
    "aggregations": {
        "test": {
            "buckets": [
                {
                    "key": "key1",
                    "doc_count": 10,
                    "new value": // can be filled via a plainless script.
                }
            ]
        }
    }
}

Solution

  • No, scripted fields are not accessible from within the aggregations because they constitute separate contexts/parts of the API.

    What you can use instead is script terms aggregations (what you're most probably looking for) or scripted metric aggregations.


    EDIT

    You cannot modify the structure/syntax of the response but you can have globally shared agg metadata. If you need more info, expound on your question w/ a concrete use case, mapping & data.