Elasticsearch中的意外字符串排序(不区分大小写) [英] Unexpected (case-insensitive) string sorting in Elasticsearch

查看:141
本文介绍了Elasticsearch中的意外字符串排序(不区分大小写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of console platforms that I'm sorting in Elasticsearch.

以下是名称字段的映射:

Here is the mapping for the "name" field:

{
    "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之后)-名称中的 / 为什么对排序有影响?

Everything is sorted as expected except the iPhone/iPod result is at the end (instead of after GameBoy Advance) - why does the / in the name have an effect on the sorting?

谢谢

推荐答案

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

Okay so I discovered the reason wasn't anything to do with the /. ES will sort by capital letters then lower case letters.

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

I added a custom analyzer to the settings of the index creation:

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

然后在字段映射中添加' analyser':'sortable' sort_name 多字段。

Then in the field mapping I added 'analyzer': 'sortable' to the sort_name multi field.

这篇关于Elasticsearch中的意外字符串排序(不区分大小写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆