如何在现有索引上映射排序?



我正在寻找Elasticsearch排序。在插入索引之前,执行以下命令

PUT product
{
"mappings": {
"properties": {
"manufacturer": {
"type": "keyword"
}
}
}
}

之后我可以用制造商进行排序。但是现在我想按品牌排序。

GET product/_search
{
"size": 5, 
"query": {
"match": {
"manufacturer": "abc"
}
},
"sort": [
{
"brand": {
"order": "desc"
}
}
]
}

显示如下错误:

"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."

是否可以在已经存在的索引上进行映射?

您可以使用下面的命令来检查当前的索引映射:

GET product

如果您当前的索引映射如下所示,那么您可以使用brand.keyword

{
"mappings": {
"properties": {
"manufacturer": {
"type": "keyword"
},
"brand": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}

如果您当前的索引映射与上面相同,可以使用下面的查询对品牌字段进行排序:

GET product/_search
{
"size": 5, 
"query": {
"match": {
"manufacturer": "abc"
}
},
"sort": [
{
"brand.keyword": {
"order": "desc"
}
}
]
}

是否可以在已经存在的索引上进行映射?

是的,您可以使用下面的命令更新现有索引的映射,但是如果您正在更新现有字段的映射,则需要重新索引数据。

PUT product/_mapping
{
"properties": {
"brand": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}

最新更新