Sorting Elasticsearch indices based on a field that is not found nor mapped

889 0 0 0

Last Updated : 2024-04-28 00:48:23

Im my case, I wanted to sort results of the general search based on the most recent parliament number, then the most recent session number. This is fine as far as both fields are already mapped for all indices. But when the field is missing, such as in my case the parliament number, then you shall direct the elasticsearch to know how he will be doing with these missing fields in other indices, So here is how to tell it :

"sort" : [
{
"parlNumberAsNumber" :
{
"order" : "desc",
"missing" : "_last", //This will tell it that this field when missing , show the results at the end
"unmapped_type" : "integer" //This is the main field here, that will direct the elasticsearch to consider if the field is there and mapped as integer or any other this
}
} ,
{
"session" :
{
"order" : "desc"
}
}
],

 

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .