Access current time in kibana scripted field

Hi,

We are trying to visualize some data based on a timespan between two dates date1 and date2. I'm trying to create a scripted field to return the amount of days between the two dates. This works as expected however date2 can in certain cases be in the future in which case we need to use the span between date1 and current time.

Expression I would like to run:
doc['date2'].value < now) ? doc['date2'] - doc['date1'] : now - doc['date1']

I cannot find any way to access current time in a scripted field expression. Is this possible?

We are running Kibana 4.5.0

Thanks!

This isn't possible with Lucene, which is the default query language. Elasticsearch will be getting a query language in the future that should allow this, but that doesn't really help you right now...

Based on some other replies here, it sounds like you should be able to do this with groovy scripting, something you can do today, though it's a little trickier to use. See the scripting docs to learn more.

Hi Joe,

Thanks for your reply. I have tried the following but neither seem to work:

(new Date()).getTime()
System.currentTimeMillis()

Maybe they are limited due to security?

If you've enabled Groovy scripting, I believe they should work. Note that this won't work by default in scripted fields in Kibana, since those are currently limited to Lucene query expressions, which apparently don't have a way to query the current date. You have to go out of your way to enable and use Groovy scripts.

Yeah, it seems I'm trying to do something that is not supported yet. Will try to find another way of doing it. Thanks for your help.