If you get error below when running elasticsearch queries that have sort property, you can solve by following solution.


Faulty query


curl -XPOST "http://127.0.0.1:9200/_search?post_dev" -d'
{
"query": {
"bool": {
"must": [
{
"match_all": []
}
]
}
},
"sort": [
{
"id": {
"order": "asc"
}
}
]
}'

Error


{
...
...
...
"took": 9,
"timed_out": false,
"_shards": {
"total": 15,
"successful": 5,
"failed": 10,
"failures": [
{
"index": "post_ix_dev",
"shard": 0,
"status": 400,
"reason": "SearchParseException[[post_ix_dev][0]: query[+ConstantScore(*:*)],from[-1],size[-1]: Parse Failure [Failed to parse source [{
"query": {
"bool": {
"must": [
{
"match_all": []
}
]
}
},
"sort": [
{
"id": {
"order": "asc"
}
}
]
}]]]; nested: SearchParseException[[post_ix_dev][0]: query[+ConstantScore(*:*)],from[-1],size[-1]: Parse Failure [No mapping found for [id] in order to sort on]]; "
}
]
}
...
...
...
}

Correct query


We added "ignore_unmapped": true to solve the problem.


curl -XPOST "http://127.0.0.1:9200/_search?post_dev" -d'
{
"query": {
"bool": {
"must": [
{
"match_all": []
}
]
}
},
"sort": [
{
"id": {
"order": "asc",
"ignore_unmapped": true
}
}
]
}'