Ray Wu
Ray Wu

Reputation: 1033

Using ElasticSearch painless scripting to compute dayOfWeek or timeOfDay

I use ES 5.1.2 and I'm trying to compute day of week and time of day from a date field and consider timezone at the same time.

my first script is def d = doc['my_field'].date; d.addHours(10); d.getDayOfWeek();

The error message is can't find addHours() method

"caused_by": {
    "type": "illegal_argument_exception",
    "reason": "Unable to find dynamic method [addHours] with [1] arguments for class [org.joda.time.MutableDateTime]."
},
"script_stack": [
    "d.addHours(10); ",
    " ^---- HERE"
],

If I change script to MutableDateTime d = doc['my_field'].date; d.addHours(10); d.getDayOfWeek(); The error message becomes

"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "unexpected token ['d'] was expecting one of [{<EOF>, ';'}]."
},
"script_stack": [
  "MutableDateTime d = doc['relation_denstu. ...",
  "                ^---- HERE"
],

Without addHours to adjust timezone, everything is fine. But if I try to adjust timezone dynamically, everything failed. Any help?

Upvotes: 0

Views: 2003

Answers (1)

Paulo Henrique PH
Paulo Henrique PH

Reputation: 361

I've been struggling with it as well. This works in Elastic 5:

GET /unittesttg1_tg1_fq1/_search
{
  "size": 0,
  "aggs": {
    "groupby": {
      "terms": {
        "script": "ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['LAST_MODIFIED_DATE'].value), ZoneId.of('+10:00')).getDayOfWeek()"          
      }
    }
  }
}

Upvotes: 3

Related Questions