ICU 脚本转换过滤器(ICU Transform)

ICU 脚本转换过滤器 #

icu_transform 词元过滤器使用 ICU 的 Transliterator 引擎将文本从一种脚本转写为另一种脚本。

前提条件 #

需要安装 analysis-icu 插件:

bin/easysearch-plugin install analysis-icu

功能说明 #

此过滤器可以实现:

  • 脚本转写:中文→拉丁、西里尔→拉丁、阿拉伯→拉丁等
  • 大小写转换:Upper、Lower、Title
  • Unicode 归一化:NFC、NFD、NFKC、NFKD
  • 自定义转换规则

使用示例 #

西里尔字母转拉丁字母 #

PUT my-transliterate-index
{
  "settings": {
    "analysis": {
      "filter": {
        "cyrillic_to_latin": {
          "type": "icu_transform",
          "id": "Cyrillic-Latin"
        }
      },
      "analyzer": {
        "my_translit": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["cyrillic_to_latin"]
        }
      }
    }
  }
}

中文转拼音 #

PUT my-pinyin-index
{
  "settings": {
    "analysis": {
      "filter": {
        "han_to_latin": {
          "type": "icu_transform",
          "id": "Han-Latin"
        }
      }
    }
  }
}

链式转换 #

PUT my-chain-index
{
  "settings": {
    "analysis": {
      "filter": {
        "to_ascii": {
          "type": "icu_transform",
          "id": "Cyrillic-Latin; NFD; [:Nonspacing Mark:] Remove; NFC"
        }
      }
    }
  }
}

测试效果 #

GET /_analyze
{
  "tokenizer": "standard",
  "filter": [{"type": "icu_transform", "id": "Cyrillic-Latin"}],
  "text": "Москва"
}

响应:Moskva

参数 #

参数类型必填说明
idstringICU 转换 ID,如 Cyrillic-LatinHan-LatinNFD
dirstring转换方向:forward(默认)或 reverse

常用转换 ID #

ID说明
Cyrillic-Latin西里尔字母 → 拉丁字母
Han-Latin汉字 → 拉丁字母(拼音)
Katakana-Latin片假名 → 拉丁字母
Arabic-Latin阿拉伯字母 → 拉丁字母
Any-Latin任意脚本 → 拉丁字母
NFD / NFCUnicode 归一化
Lower / Upper / Title大小写转换

相关链接 #