Scripted field in Elastic aggregation using High level Client


(Varun) #1

How do i create a aggregation with scripted field using Elastic High level client ?

Kibana query like below

GET itil_incidents/_search
{
  "size": 0,
  "aggs": {
    "create_date": {
      "date_histogram": {
        "field": "Incident_Create_Date",
        "interval": "1d",
        "format": "MM-dd-yyyy"
      },
      "aggs": {
        "avg_date_diff": {
          "avg": {
            "script": {
              "lang": "painless",
              "source": "Math.round((doc.Incident_Close_Date.value.millis-doc.Incident_Create_Date.value.millis))"
            }
          }
        }
      }
    }
  }
}

(Varun) #2

Please Help. I dont know how to create scripted field in High Level client


(David Pilato) #3

Did you look at this page?

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-aggregation-builders.html

And this method: https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.5.2/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.html#script()