我正在尋找在 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."
是否可以對已經存在的索引進行映射?
uj5u.com熱心網友回復:
您可以使用以下命令檢查當前索引映射:
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
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/467157.html
標籤:弹性搜索
