Elasticsearch throws a SearchParseException
while parsing query if there are some documents found not containing field used in sort criteria.
Let me try to help in the case that the original answer doesn't work for you. For ES 6.x try the code below, ignore_unmapped is deprecated.
"sort" : [
{ "rating": {"order" : "desc" , "unmapped_type" : "long"} },
{ "price": {"order" : "asc" , "missing" : "_last" , "unmapped_type" : "long"} }
]
More about sorting you can find on:
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html
Apparently ElasticSearch won't sort on null values. I was assuming it would treat null as being at the start or end (as with SQL ordering) but I believe it also triggers this error.
So if you see this error, you may need to ensure the sort attribute has a default value when it's sent to ElasticSearch.
I had this error with Rails+ElasticSearch+Tire because the sort column didn't have a default value, so was being sent to ES as null.
This issue indicates null values are handling, but it wasn't my experience. It's something worth trying anyway.
When we use below code, where added_on is date, what happens !! attribute text is analyzed, meaning it is broken up into distinct words when stored, and allows for free-text searches on one or more words in the field
so there is "text" and "keyword" associated with fields, so if we need to use aggregation in the query, we need the field value in general the keyword.
BEFORE
"_source":{....}
"query" : {...}
"sort": [
{
"added_on": {
"order": "desc"
}
}
]
AFTER
"_source":{....}
"query" : {...}
"sort": [
{
"added_on.keyword": {
"order": "desc"
}
}
]