Reputation: 5001
Elasticsearch throws a SearchParseException
while parsing query if there are some documents found not containing field used in sort criteria.
SearchParseException: Parse Failure [No mapping found for [price] in order to sort on]
How can I successfully search these documents, even if some are missing the price
field?
Upvotes: 149
Views: 140792
Reputation: 1269
For unmapped_type
, there is one notable distinction that I hit between Elasticsearch 7 and Elasticsearch 8 which prevented cross-index results from coming back for one of my indices.
I had an extremely complex query, and for whatever reason I was not getting error messaging back from the @elastic/elasticsearch library. I ended up writing a short integration test to see if the Elasticsearch syntax parser was handling it my query correctly in version 8 and it was not.
This was the change that I had to make between the two versions:
Elasticsearch 7:
"sort": [ { "treeMap": { "unmapped_type": "string" } } ]
Elasticsearch 8:
"sort": [ { "treeMap": { "unmapped_type": "keyword" } } ]
Upvotes: 0
Reputation: 1049
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
Upvotes: 3
Reputation: 498
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"
}
}
]
Upvotes: 0
Reputation: 3184
You could also use script which gives you some flexibility:
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"lang": "painless",
"source": "return !doc['price'].empty ? doc['price'].value : 0"
},
"order" : "desc"
}
}
Upvotes: 0
Reputation: 133
if you are using es 6.7
try this one
sort : ["title.keyword:desc"]
Upvotes: 7
Reputation: 918
Elasticsearch 6.4
simply specify the index and that's it in Kibana
BEFORE
GET /_search
{
"query": {
"exists": {
"field": "document_id"
}
},
"sort": [
{
"document_id": { "order": "asc" },
"created_at": { "order": "desc" }
}
]
}
AFTER
GET /document-index/contact/_search (here)
{
"query": {
"exists": {
"field": "document_id"
}
},
"sort": [
{
"document_id": { "order": "asc" },
"created_at": { "order": "desc" }
}
]
}
Upvotes: 2
Reputation: 5001
After digging more, I found the solution as given below. ignore_unmapped
should be explicitly set to true
in the sort clause.
"sort" : [
{ "rating": {"order" : "desc" , "ignore_unmapped" : true} },
{ "price": {"order" : "asc" , "missing" : "_last" , "ignore_unmapped" : true} }
]
For further information have a look at the Elasticsearch references for:
Upvotes: 137
Reputation: 929
For those looking for an example of both
ignore_unmapped
andunmapped_type
please see my response here.
Note that "ignore_unmapped" is now deprecated in favor of "unmapped_type". This was done as part of #7039
From documentation: Before 1.4.0 there was the ignore_unmapped boolean parameter, which was not enough information to decide on the sort values to emit, and didn’t work for cross-index search. It is still supported but users are encouraged to migrate to the new unmapped_type instead.
By default, the search request will fail if there is no mapping associated with a field. The unmapped_type option allows to ignore fields that have no mapping and not sort by them. The value of this parameter is used to determine what sort values to emit. Here is an example of how it can be used:
{
"sort" : [
{ "price" : {"unmapped_type" : "long"} },
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
If any of the indices that are queried doesn’t have a mapping for price then Elasticsearch will handle it as if there was a mapping of type long, with all documents in this index having no value for this field.
Upvotes: 62
Reputation: 46509
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.
Upvotes: 4
Reputation: 2613
I experienced the same problem (sorta; would get some errors, but some results), but in my case my search was being issued at the root (no index specified), and the errors I was getting were because the search/order was also looking to a Kibana index.
Stupid error, but maybe this'll help someone else who ends up here.
Upvotes: 2