Comparing date in same document

Hello Elastic Team,

I have two dates fields with format

2022-02-10T23:00:00.000Z

I want to compare them , but I don't want to compare the time. Only the date part like :

2022-02-10

I tried using Scripted Fields with


LocalDateTime.ofInstant( Instant.ofEpochMilli(doc['date'].value.millis), ZoneId.of('Europe/Paris') ).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
== 
LocalDateTime.ofInstant( Instant.ofEpochMilli(doc['cutoff'].value.millis), ZoneId.of('Europe/Paris') ).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))

But It return True only ...

2nd question, how can I do it using Dev Tools (API) whitout Scripted Fields ?

Any pointers to resolve the issue are really appreciated.

Thank you

The value returned from doc['date'].value is a string value, the call to milis on it makes no sense. I am surprised Painless doesn't throw an exception there.

As to the second part of your question - we have runtime fields on Elasticsearch side, which are not tied to Kibana. This way you can create fields on read time, and access them from the console.

Hello Maja,

Thank you for your help.

I finally used this code to convert/ compare the date and it works pretty well

ZonedDateTime.from(doc['date'].value).withZoneSameInstant(ZoneId.of('Europe/Paris')).getDayOfYear().equals( ZonedDateTime.from(doc['cutoff'].value).withZoneSameInstant(ZoneId.of('Europe/Paris')).getDayOfYear() )

I will take a look about runtime fields, thank you :smiley:

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