Type to search.

Datetime now

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Under most Painless contexts the current datetime, now, is not supported. There are two primary reasons for this. The first is that scripts are often run once per document, so each time the script is run a different now is returned. The second is that scripts are often run in a distributed fashion without a way to appropriately synchronize now. Instead, pass in a user-defined parameter with either a string datetime or numeric datetime for now. A numeric datetime is preferred as there is no need to parse it for comparison.

  • Use a numeric datetime as now

    • Assumptions:

      • The field input_datetime exists in all indexes as part of the query
      • All indexed documents contain the field input_datetime
    • Mappings:

      JSON
      {
        "mappings": {
            ...
            "properties": {
              ...
              "input_datetime": {
              "type": "date"
            }
            ...
          }
          ...
        }
      }
      
    • Input:

      JSON
      ...
      "script": {
          ...
          "params": {
              "now": <generated numeric datetime in milliseconds since epoch>
          }
      }
      ...
      
    • Script:

      Java
      long now = params['now'];
      ZonedDateTime inputDateTime = doc['input_datetime'];
      long millisDateTime = inputDateTime.toInstant().toEpochMilli();
      long elapsedTime = now - millisDateTime;
      
  • Use a string datetime as now

    • Assumptions:

      • The field input_datetime exists in all indexes as part of the query
      • All indexed documents contain the field input_datetime
    • Mappings:

      JSON
      {
        "mappings": {
          ...
          "properties": {
            ...
            "input_datetime": {
              "type": "date"
            }
            ...
          }
          ...
        }
      }
      
    • Input:

      JSON
      ...
      "script": {
          ...
          "params": {
              "now": "<generated string datetime in ISO-8601>"
          }
      }
      ...
      
    • Script:

      Java
      String nowString = params['now'];
      ZonedDateTime nowZdt = ZonedDateTime.parse(nowString);
      long now = ZonedDateTime.toInstant().toEpochMilli();
      ZonedDateTime inputDateTime = doc['input_datetime'];
      long millisDateTime = zdt.toInstant().toEpochMilli();
      long elapsedTime = now - millisDateTime;
      
      1. This parses the same string datetime every time the script runs. Use a numeric datetime to avoid a significant performance hit.