How to do sort in using on particular field

后端 未结 2 1060
一个人的身影
一个人的身影 2021-01-26 01:35

I have a document

How to do sort in using on particular field on elasticsearch

My query is below

{
   "sort":{
      "name":&qu         


        
相关标签:
2条回答
  • 2021-01-26 02:17

    you can make a PUT request on the index to set fielddata as true:

    curl --location --request PUT 'http://localhost:9200/index_name/info/_mapping' \
    --header 'Content-Type: application/json' \
    --data-raw '{
      "properties": {
         "desc": { 
           "type":     "text",
           "fielddata": true
         }
      }
    }'
    

    where desc is the column name

    0 讨论(0)
  • 2021-01-26 02:19

    Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.

    A text field is analyzed before indexing so that a value like New York can be found by searching for new or for york. A terms aggregation on this field will return a new bucket and a york bucket, when you probably want a single bucket called New York

    Instead, you should have a text field for full text searches, and an unanalyzed keyword field with doc_values enabled for aggregations, as follows:

    PUT data_new
    {
      "mappings": {
        "properties": {
          "name": { 
            "type": "text",
            "fields": {
              "keyword": { 
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    I guess you can already treat name as keyword using name.keyword like below,

    GET /data_new/_search
    {
      "sort" : [
         { "name.keyword" : {"order" : "asc"}}
      ],
     "from":10,
     "size":149,
     "query":{
       "match_all":{
         
         }
       }
    }
    

    See:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html

    0 讨论(0)
提交回复
热议问题