I am just getting started with Elasticsearch and would like to use script-based sorting on a field that is mapped as date, format hour_minute. There can be multiple instances of the field in each document.
Before introducing expressions, as a first step I'm trying a simple sort (using the Sense plugin):
POST myIndex/_search
{
"query": {
"match_all": {}
},
"sort": {
"_script": {
"script": "doc[\"someTime\"].value",
"lang": "groovy",
"type": "date",
"order": "asc"
}
}
}
I get this error (fragment):
SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;
shardFailures {[tjWL-zV5QXmGjNlXzLvrzw][myIndex][0]:
SearchParseException[[myIndex][0]:
query[ConstantScore(*:*)],from[-1],size[-1]: Parse Failure [Failed to parse source…
If I post the above query with "type": "number" there is no error, although this of course doesn't sort by date. The following works fine:
POST myIndex/_search
{
"query": {
"match_all": {}
},
"sort": {
"someTime": {
"order": "asc"
}
}
}
Ultimately I'd like to use script-based sorting since I will be trying to query, filter or sort using date and time conditions, like query for documents with today’s date, then sort them by the lowest time that is after the time now, etc.
Any suggestions would be much appreciated.