Reputation: 170
Let's say I have documents with the following fields:
{field1, field2, startdate, enddate}
I need to run some queries where some of the conditions will require the difference between the startdate
and the enddate
In standard SQL, an example could be:
SELECT *,(2010-01-01-startdate) as diffstartdate, (2010-01-01-enddate) as diffenddate FROM Table1
How can I achieve this in elasticsearch?
Upvotes: 0
Views: 1017
Reputation: 7649
Script Fields
is what you need.
{
"query" : {
"match_all": {}
},
"script_fields" : {
"difference_between_two_dates" : {
"script" : "groovy.time.TimeCategory.minus(new java.util.Date(2010-01-01),new java.util.Date(doc['dateCreated'].value))"
}
}
}
Make sure that dynamic script is enabled in elasticsearch.yml
.
script.disable_dynamic: false
You can refer to https://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-request-script-fields.html for more information. Hope it helps.
Upvotes: 1