Link Search Menu Expand Document Documentation Menu

Hunspell 词元过滤器

Hunspell hunspell 词元过滤器用于对特定语言的单词进行词干提取和形态分析。该过滤器应用了 Hunspell 字典,这些字典广泛用于拼写检查器。它通过将单词分解为它们的词根形式(词干提取)来工作。

Hunspell 字典文件在启动时自动从 <OS_PATH_CONF>/hunspell/<locale> 目录加载。例如,en_GB 语言环境必须在 <OS_PATH_CONF>/hunspell/en_GB/ 目录中至少包含一个 .aff 文件和一个或多个 .dic 文件。

您可以从 LibreOffice 字典下载这些文件。

参数

可以通过以下参数配置 hunspell 词元过滤器。

参数 必需/可选 数据类型 描述
language/lang/locale 三者中至少需要一个 字符串 指定 Hunspell 字典的语言。
dedup 可选 布尔型 确定是否删除同一词元的多个重复词干提取术语。默认值为 true
dictionary 可选 字符串数组 配置用于 Hunspell 字典的字典文件。默认值为 <OS_PATH_CONF>/hunspell/<locale> 目录中的所有文件。
longest_only 可选 布尔型 指定是否只返回词元最长的词干提取版本。默认值为 false

示例

以下示例请求创建了一个名为 my_index 的新索引,并使用 hunspell 过滤器配置了一个分析器

PUT /my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "my_hunspell_filter": {
          "type": "hunspell",
          "lang": "en_GB",
          "dedup": true,
          "longest_only": true
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_hunspell_filter"
          ]
        }
      }
    }
  }
}

生成的词元

使用以下请求检查使用该分析器生成的词元

POST /my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "the turtle moves slowly"
}

响应包含生成的词元

{
  "tokens": [
    {
      "token": "the",
      "start_offset": 0,
      "end_offset": 3,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "turtle",
      "start_offset": 4,
      "end_offset": 10,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "move",
      "start_offset": 11,
      "end_offset": 16,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "slow",
      "start_offset": 17,
      "end_offset": 23,
      "type": "<ALPHANUM>",
      "position": 3
    }
  ]
}
剩余 350 字符

有问题?

想要贡献?