You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Elasticsearch icu_collation_keyword字段编写字母优先的排序规则

自定义Elasticsearch icu_collation_keyword排序规则实现字母优先排序

你当前使用带language参数的icu_collation_keyword字段时,排序结果为$1232、£7232、87343、Abi、Karthik,不符合「字母→空格→标点→符号→货币→数字→其他」的优先级需求。以下是通过rules参数自定义排序规则的解决方案,无需使用locale选项:

修改后的索引Mapping

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "name": {   
        "type": "text",
        "fields": {
          "sort": {  
            "type": "icu_collation_keyword",
            "index": false,
            "rules": "&[:letter:] < [:space:] < [:punctuation:] < [:symbol:] < [:currency:] < [:digit:]"
          }
        }
      }
    }
  }
}

规则说明

  • [:letter:]:匹配所有字母字符,设置为最高优先级,排在最前面
  • [:space:]:匹配空格类字符,优先级次之
  • [:punctuation:]:匹配标点符号,优先级第三
  • [:symbol:]:匹配通用符号,优先级第四
  • [:currency:]:匹配货币符号(如$、£),优先级第五
  • [:digit:]:匹配数字字符,优先级第六

验证步骤

  1. 插入测试数据:
POST my-index-000001/_bulk
{"index":{}}
{"name":"$1232"}
{"index":{}}
{"name":"Abi"}
{"index":{}}
{"name":"£7232"}
{"index":{}}
{"name":"87343"}
{"index":{}}
{"name":"Karthik"}
  1. 执行排序查询:
GET my-index-000001/_search
{
  "sort": [{"name.sort": "asc"}],
  "query": {"match_all": {}}
}

返回的排序结果将符合预期:Abi、Karthik、$1232、£7232、87343

内容的提问来源于stack exchange,提问作者Karthik Amar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 03:42:48