Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renshuki/6fddce0771f0ca477114e67c015e0501 to your computer and use it in GitHub Desktop.
Save renshuki/6fddce0771f0ca477114e67c015e0501 to your computer and use it in GitHub Desktop.
Elasticsearch - Painless script to compare the current date with a date already indexed into a document

Elasticsearch Painless script which aims to calculate the difference in days between a date indexed into a document and the current date.

GET days_compare/_search
{
  "script_fields": {
    "diffdate": {
      "script": {
        "lang":   "painless",
        "source": """
        Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());
        Instant Startdate = Instant.ofEpochMilli(doc['date'].value.getMillis());
        ChronoUnit.DAYS.between(Startdate, Currentdate);
        """
      }
    }
  }
}

# The result will be shown in days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment