Home > OS >  How to sort string in elasticsearch
How to sort string in elasticsearch

Time:04-27

I want to get a result sorted by name(string), and this is what I do:

1.create index named as metadata PUT http://IP:9200/metadata/ 2. create type name as objects POST http://ip:9200/metadata/objects

{
"mappings":{
    "objects":{
        "properties":{
            "name":{
                "type":"string",
                "index": "not_analyzed"
            },
            "version":{
                "type":"integer"
            },
            "size":{
                "type":"integer"
            },
            "hash":{
                "type":"string"
            }
        }   
    }
}

}

  1. insert data

POST http://ip:9200/metadata/objects/test3_1?op_type=create

{
  "name":"test3",
  "version":1,
   "size":13,
   "hash":"2oUvHeq7jQ27Va2y/usI1kSX4cETY9LuevZU9RT Fuc="
}
  1. get result sorted by name

GET http://101.43.155.248:9200/metadata//_search?sort=name,version

and the error is

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index. Note that this can use significant memory

CodePudding user response:

You either need to use the .keyword for your sort field, if it's created dynamically or define additional field as .keyword on which you need sorting.

As you created index mapping manually, you need to add the .keyword manually on the name field, on the price there is no issue as on numeric field it works.

  • Related