ElasticSearch - How to get the current date and time?

Is it possible to get the current date and time from Elastic Search (preferably in the Java API)?

I searched everywhere for quite a while, but found only that range queries can do the math in some date fields, and you can use script fields, but I could not find a script to return the current date. All I ask is like SQL now ().

Thank you for your time.

+4
source share
1 answer

In Sense (you did not specify the ES version, this works against 2.3.2):

GET your_index/_search
{
  "size": 1, 
  "script_fields": {
    "now": {
      "script": "new Date().getTime()"
    }
  }
}

In the Java API, this will be something like (not verified):

yourSearchRequestBuilder.addScriptField("now", "new Date().getTime()");

, your_index 1 , - script; .

+1

Source: https://habr.com/ru/post/1662296/


All Articles