Kibana Scripted Field HourOfDay

I followed documentation to create a simple scripted field to calculate HourOfDay

doc['@timestamp'].date.hourOfDay

error:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 9,
    "successful": 8,
    "skipped": 8,
    "failed": 1,
    "failures": [
      {
        "shard": 0,
        "index": "sanitas-apitsl-000009",
        "node": "OdEMJt__QDK1fSI1zeU3IA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "doc['@timestamp'].date.hourOfDay",
            "                 ^---- HERE"
          ],
          "script": "doc['@timestamp'].date.hourOfDay",
          "lang": "painless",
          "position": {
            "offset": 17,
            "start": 0,
            "end": 32
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Illegal list shortcut value [date]."
          }
        }
      }
    ]
  },
  "hits": {
    "max_score": null,
    "hits": []
  }
}

What I'm doing wrong?

Elasticsearch/Kibana: 8.1.3

Could you check if correcting it to doc['@timestamp'].value.getHour() helps?

1 Like

@Marta_Bondyra is correct that, for "lang": "painless", doc['@timestamp'].value.getHour() is the way to go.

GET ts/_search
{
  "script_fields": {
    "hod": {
      "script": {
        "source": "doc['@timestamp'].value.getHour()",
        "lang": "painless"
      }
    }
  }
}

The docs you are referencing are for "lang": "expression" which has a slightly different API.

GET ts/_search
{
  "script_fields": {
    "hod": {
      "script": {
        "source": "doc['@timestamp'].date.hourOfDay",
        "lang": "expression"
      }
    }
  }
}
1 Like

Thanks for you replies.

I solved "my" problem as follows:

return doc['@timestamp'].value.withZoneSameInstant(ZoneId.of('Europe/Berlin')).getHour();

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.