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