ICU 排序过滤器(ICU Collation)

ICU 排序过滤器 #

icu_collation_keyword 词元过滤器使用 ICU 排序规则将词元转换为排序键(collation key),实现语言感知的排序和范围查询。

前提条件 #

需要安装 analysis-icu 插件:

bin/easysearch-plugin install analysis-icu

功能说明 #

此过滤器将文本词元转换为 ICU CollationKey 的字节表示。转换后的词元可用于:

  • 语言感知排序:按特定语言的排序规则排列结果
  • 范围查询:在 keyword 字段上执行符合语言习惯的范围过滤
  • 重音/大小写不敏感匹配:通过调整排序强度控制匹配精度

使用示例 #

基本用法 — 德语排序 #

PUT my-german-sort
{
  "settings": {
    "analysis": {
      "filter": {
        "german_collation": {
          "type": "icu_collation_keyword",
          "language": "de",
          "country": "DE"
        }
      },
      "analyzer": {
        "german_sort": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": ["german_collation"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "sort": {
            "type": "keyword",
            "analyzer": "german_sort"
          }
        }
      }
    }
  }
}

忽略重音的匹配 #

PUT my-accent-insensitive
{
  "settings": {
    "analysis": {
      "filter": {
        "collation_primary": {
          "type": "icu_collation_keyword",
          "language": "en",
          "strength": "primary"
        }
      }
    }
  }
}

参数 #

参数类型说明
languagestringICU 语言代码(如 de, fr, zh
countrystringICU 国家代码(如 DE, FR
strengthstring排序强度:primary(忽略重音+大小写)、secondary(区分重音)、tertiary(区分大小写)、quaternaryidentical
decompositionstringUnicode 分解模式:nocanonical
alternatestring空白/标点处理:shifted(忽略)、non-ignorable
caseLevelboolean是否在 primary 强度下区分大小写
caseFirststring大小写优先:lowerupper
numericboolean是否按数字值排序(true 使 “2” < “10”)
rulesstring自定义 ICU 排序规则字符串

相关链接 #