I am looking for sorting in Elasticsearch. Before I insert the indices, I execute following
PUT product
{
"mappings": {
"properties": {
"manufacturer": {
"type": "keyword"
}
}
}
}
After I can make a sort with manufacturer. But now I want to sort by brand.
GET product/_search
{
"size": 5,
"query": {
"match": {
"manufacturer": "abc"
}
},
"sort": [
{
"brand": {
"order": "desc"
}
}
]
}
It gives me following error:
"type" : "illegal_argument_exception",
"reason" : "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 [brand] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
Is it possible to make a mapping on already existing indices ?
CodePudding user response:
You can use below command to check current index mapping:
GET product
If your current index mapping is look like below then you can use brand.keyword
.
{
"mappings": {
"properties": {
"manufacturer": {
"type": "keyword"
},
"brand": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
You can use below query to sort on brand field if your current index mapping is same as above:
GET product/_search
{
"size": 5,
"query": {
"match": {
"manufacturer": "abc"
}
},
"sort": [
{
"brand.keyword": {
"order": "desc"
}
}
]
}
Is it possible to make a mapping on already existing indices ?
Yes, you can update mapping of existing index using below command but you need to reindex data if you are updating mapping of existing field.
PUT product/_mapping
{
"properties": {
"brand": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}