Elasticsearch is not sorting the results

安稳与你 提交于 2019-12-04 22:53:23
Vinu Dominic

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!