Script to compare dates

I would like to compare an elastic search date with a format YYY-MM-DD HH:MM:SS as to compare the results of the past 30 days. So if the given date is equal to NOW()-30D it should give me the result wanted. I have tried comparing it this way with a script but it doesn't work :

getMillis(doc["date"].value) >= (new Date().getTime() - 30 * 24 * 60 * 60 * 1000) ? 1 : 0

Another test not working :

doc["date"].value >= (new Date().getTime() - 30) ? 1 : 0

This should do this : If doc["date"].value is higher or equal to the last 30 days it should give me 1, else it would give me 2.

How can I achieve that?

What kind of script are you trying to use?

@rjernst I'm using groovy scripting :slight_smile:

What version of elasticsearch? I recommend switching to elasticsearch 5+, as that has painless, which has the java 8 time api available, and is much easier to work with IMO.

@rjernst I'm limited to Elasticsearch 1.9 as the server of my company won't handle a higher version. There isn't a way to do it with groovy? As I suppose comparing an elasticsearch date with the date of the last 30 days should be doable.

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