Reputation: 672
I want to get year from date in elastic search.My mapping is like -
"timeModified":
{
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
And my data store in elastic search is -
"timeModified": "2015-03-12T13:18:50.000+05:30"
I want to get year from the date.When I execute the below query -
{
"query" : {
"match_all": {}
},
"script_fields" : {
"left_field" : {
"script" : {
"inline": "doc.timeModified.value.substring(0, length)",
"params": {
"length": 4
}
}
}
}
}
It give me exception -
"No signature of method: java.lang.Long.substring() is applicable for argument types: (java.lang.Integer, java.lang.Integer) values: [0, 4]\nPossible solutions: toString(), toString(), toString(), toString(long, int)"
If I have option to convert the field value date to string without change in my actual mapping type, I get the correct value. So please suggest how to convert in string in elastic search query. Or if any other option to get year from date then also suggest.
Upvotes: 1
Views: 3094
Reputation: 52368
The script is as simple as doc['timeModified'].date.year
. Knowing that field is of type date
, ES exposes the .date
shortcut where things like .year
can be used.
Upvotes: 1
Reputation: 217424
The error you're getting is because the field value is a Long
not a String
. However, you can also access the date
value directly by doing it like this:
{
"query" : {
"match_all": {}
},
"script_fields" : {
"left_field" : {
"script" : {
"inline": "doc.timeModified.date.getYear()"
}
}
}
}
Upvotes: 3