我正在创建以下my_cars
索引
PUT my_cars
{
"settings": {
"analysis": {
"analyzer": {
"sortable": {
"tokenizer": "keyword",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"analyzer": "sortable"
}
}
}
}
当我检查映射时,它看起来很好:
{
"my_cars" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
},
"analyzer" : "sortable"
}
}
}
}
}
但是现在,当我运行查询进行搜索和排序时
GET my_cars/_search
{
"query": {
"match_all": {}
},
"sort": {
"name.keyword": {
"order": "asc"
}
}
}
首先显示大写/大写的结果,因此我认为分析器不能正常工作。我得到的结果如下:
{
"took" : 163,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "my_cars",
"_type" : "_doc",
"_id" : "f1RLUnoBZEZpPd-TeK9L",
"_score" : null,
"_source" : {
"name" : "Apples",
"price" : 250
},
"sort" : [
"Apples"
]
},
{
"_index" : "my_cars",
"_type" : "_doc",
"_id" : "H7JLUnoBh60DJePfnpGB",
"_score" : null,
"_source" : {
"name" : "Brocoli",
"price" : 250
},
"sort" : [
"Brocoli"
]
},
{
"_index" : "my_cars",
"_type" : "_doc",
"_id" : "gFRLUnoBZEZpPd-Tyq9A",
"_score" : null,
"_source" : {
"name" : "azus",
"price" : 110
},
"sort" : [
"azus"
]
},
{
"_index" : "my_cars",
"_type" : "_doc",
"_id" : "gVRMUnoBZEZpPd-TAq-A",
"_score" : null,
"_source" : {
"name" : "botpzus",
"price" : 80
},
"sort" : [
"botpzus"
]
}
]
}
}
正如你所看到的,小写的名字排在最后,我该如何解决这个问题呢?我已经基于THIS问题构建了我的分析器。但与这个问题的答案不同的是,我无法直接在keyword
映射中添加分析器字段。我如何修复我的字母搜索,而不考虑大小写?
发布于 2021-06-28 19:22:13
解决方案是在name.keyword
上使用use a normalizer
PUT my_cars
{
"settings": {
"analysis": {
"normalizer": {
"sortable": {
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"normalizer": "sortable"
}
}
}
}
}
}
https://stackoverflow.com/questions/68162310
复制相似问题