gpt4 book ai didi

sorting - Elasticsearch 中的意外(不区分大小写)字符串排序

转载 作者:行者123 更新时间:2023-11-29 02:50:48 24 4
gpt4 key购买 nike

我有一个在 Elasticsearch 中排序的控制台平台列表。

这是“名称”字段的映射:

{
"name": {
"type": "multi_field",
"fields": {
"name": {
"type": "string",
"index": "analyzed"
},
"sort_name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}

当我执行下面的查询时

{
"query": {
"match_all": {}
},
"sort": [
{
"name.sort_name": { "order": "asc" }
}
],
"fields": ["name"]
}

我得到这些结果:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 17,
"max_score": null,
"hits": [
{
"_index": "platforms",
"_type": "platform",
"_id": "1393602489",
"_score": null,
"fields": {
"name": "GameCube"
},
"sort": [
"GameCube"
]
},
{
"_index": "platforms",
"_type": "platform",
"_id": "1393602490",
"_score": null,
"fields": {
"name": "Gameboy Advance"
},
"sort": [
"Gameboy Advance"
]
},


{
"_index": "platforms",
"_type": "platform",
"_id": "1393602498",
"_score": null,
"fields": {
"name": "Nintendo 3DS"
},
"sort": [
"Nintendo 3DS"
]
},

...remove for brevity ...

{
"_index": "platforms",
"_type": "platform",
"_id": "1393602493",
"_score": null,
"fields": {
"name": "Xbox 360"
},
"sort": [
"Xbox 360"
]
},
{
"_index": "platforms",
"_type": "platform",
"_id": "1393602502",
"_score": null,
"fields": {
"name": "Xbox One"
},
"sort": [
"Xbox One"
]
},
{
"_index": "platforms",
"_type": "platform",
"_id": "1393602497",
"_score": null,
"fields": {
"name": "iPhone/iPod"
},
"sort": [
"iPhone/iPod"
]
}
]
}

除了 iPhone/iPod 结果在末尾(而不是在 GameBoy Advance 之后)之外,所有内容都按预期排序 - 为什么名称中的 / 会产生影响关于排序?

谢谢

最佳答案

好吧,我发现原因与 / 无关。 ES 将按大写字母排序,然后按小写字母排序。

我在索引创建的 settings 中添加了自定义分析器:

{
"analysis": {
"analyzer": {
"sortable": {
"tokenizer": "keyword",
"filter": [
"lowercase"
]
}
}
}
}

然后在字段映射中,我将 'analyzer': 'sortable' 添加到 sort_name 多字段。

关于sorting - Elasticsearch 中的意外(不区分大小写)字符串排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22099906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com