# Hour Scripted Field

**URL:** https://discuss.elastic.co/t/hour-scripted-field/263416
**Category:** Kibana
**Tags:** painless
**Created:** [February 5, 2021, 3:23pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416 "2021-02-05T15:23:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![stevezemlicka](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevezemlicka](https://discuss.elastic.co/u/stevezemlicka)
#### Post date: [February 5, 2021, 3:23pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/1 "2021-02-05T15:23:22Z")

</div>

I am trying to create a scripted field that is the hourly value of each document. The ultimate goal is to filter by specific hours of the day for multiple days (hence why I can't just use the date range on the dashboards).

After a bit of research, the best resource I found was the following which seems to indicate that this is possible:

> [@Scripted field HourOfDay()](https://discuss.elastic.co/t/scripted-field-hourofday/87520):
>
> Hi, I want a field containing the hour of the day for each elasticsearch document. The easiest way would be to parse @timestamp into a hour\_of\_day field but that means I would have to reindex all my data. I saw another solution by using Scripted Fields with: doc["@timestamp"].getHourOfDay() That works to get the hour ('HH') in format 00-23 and that's exactly what I was expecting. But, the hour\_of\_day field isn't correct as it shows a different hour value (probably due to timezone difference…

However I cannot seem to get it to work. When I try to preview the results, I receive the following:

> doc['@timestamp'].date.hourOfDay

produces

> {  
> "root\_cause": [  
> {  
> "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  
> }  
> }  
> ],  
> "type": "search\_phase\_execution\_exception",  
> "reason": "all shards failed",  
> "phase": "query",  
> "grouped": true,  
> "failed\_shards": [  
> {  
> "shard": 0,  
> "index": "san-isabel",  
> "node": "erbPD7dEQPKwU5NNOXVB9g",  
> "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]."  
> }  
> }  
> }  
> ]  
> }

and this:

> LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value),ZoneId.of('America/Chicago')).getHour()

produces

> {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> " ^---- HERE"  
> ],  
> "script": "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> "lang": "painless",  
> "position": {  
> "offset": 62,  
> "start": 0,  
> "end": 110  
> }  
> }  
> ],  
> "type": "search\_phase\_execution\_exception",  
> "reason": "all shards failed",  
> "phase": "query",  
> "grouped": true,  
> "failed\_shards": [  
> {  
> "shard": 0,  
> "index": "san-isabel",  
> "node": "erbPD7dEQPKwU5NNOXVB9g",  
> "reason": {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> " ^---- HERE"  
> ],  
> "script": "LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('America/Chicago')).getHour()",  
> "lang": "painless",  
> "position": {  
> "offset": 62,  
> "start": 0,  
> "end": 110  
> },  
> "caused\_by": {  
> "type": "wrong\_method\_type\_exception",  
> "reason": "cannot convert MethodHandle(Dates)JodaCompatibleZonedDateTime to (Object)long"  
> }  
> }  
> }  
> ]  
> }

What might I be doing wrong and how might I accomplish this?

---

<div class="post-metadata">

### Author: ![stevezemlicka](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevezemlicka](https://discuss.elastic.co/u/stevezemlicka)
#### Post date: [February 5, 2021, 3:27pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/2 "2021-02-05T15:27:35Z")

</div>

It might be helpful to provide a bit more context on my data. When ingesting, I am mapping a "date" field to @timestamp. So while I do have @timestamp, I also have a simpler date field in each document in the following format:

> MM/DD/YYYY HH:mm

Perhaps I can pull it from that consistently formated field without relying on any fancy get functions?

---

<div class="post-metadata">

### Author: ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)
#### Post date: [February 5, 2021, 3:46pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/3 "2021-02-05T15:46:12Z")

</div>

`return doc['@timestamp'].value.getHour();`

This works for me.

---

<div class="post-metadata">

### Author: ![stevezemlicka](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevezemlicka](https://discuss.elastic.co/u/stevezemlicka)
#### Post date: [February 5, 2021, 4:57pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/4 "2021-02-05T16:57:29Z")

</div>

> [@aaron-nimocks](#):
>
> return doc['@timestamp'].value.getHour();

YES!!! This worked for me as well. Thank you!

---

<div class="post-metadata">

### Author: ![stevezemlicka](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stevezemlicka](https://discuss.elastic.co/u/stevezemlicka)
#### Post date: [February 9, 2021, 10:53pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/5 "2021-02-09T22:53:33Z")

</div>

For those that come across this in the future, I had an issue with the previous solution and timezone. Seems this was matching UTC which didn't align with the expected output due to how timezones are handled with scripted fields. What ultimately worked was:

> return LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value.millis),ZoneId.of('America/Chicago')).getHour()

> [@ES 6.0 - Scripted Field HourOfDay with Timezone - error](https://discuss.elastic.co/t/es-6-0-scripted-field-hourofday-with-timezone-error/107792):
>
> Hello, First Post Here, I will try to do my best to make a clear presentation. I recently upgraded ELK on my UAT node from 5.6 to 6.0 OS : 2012r2 ; single node. ELK : 6.0.0 Java : 1.8.0\_151 I was running a painless scripted field pretty well on 5.6.0 to deduce HourOfDay, as follow : Name : HourOfDay Script : LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value),ZoneId.of('Europe/Paris')).getHour() I used this script because of the Timezone I'm using : Europe/Paris ; it…

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 9, 2021, 10:54pm UTC](https://discuss.elastic.co/t/hour-scripted-field/263416/6 "2021-03-09T22:54:17Z")

</div>

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