Elasticsearch is not sorting the results Elasticsearch is not sorting the results elasticsearch elasticsearch

Elasticsearch is not sorting the results


The field "title" in your document is an analyzed string field, which is also a multivalued field, which means elasticsearch will split the contents of the field into tokens and stores it separately in the index.You probably want to sort the "title" field alphabetically on the first term, then on the second term, and so forth, but elasticsearch doesn’t have this information at its disposal at sort time.

Hence you can change your mapping of the "title" field from:

{  "title": {    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer": "standard"  }}

into a multifield mapping like this:

{  "title": {    "type": "string", "boost": 9, "analyzer": "autocomplete", "search_analyzer":"standard",    "fields": {      "raw": {        "type": "string",        "index": "not_analyzed"      }    }  }}

Now execute your search based on analyzed "title" field and sort based on the not_analyzed "title.raw" field

{    "sort": [{        "title.raw": {"order": "desc"}    }],    "query":{        "term": { "title": "pagos" }    }}

It is beautifully explained here: String Sorting and Multifields