标准分词器

标准分词器 #

标准(standard)分词器是在未指定其他分词器时默认使用的分词器。它旨在为通用文本处理提供一种基础且高效的方法。

该分词器由以下词元生成器和词元过滤器组成:

  • 标准(standard)词元生成器:去除大部分标点符号,并依据空格和其他常见分隔符对文本进行分割。
  • 小写(lowercase)词元过滤器:将所有词元转换为小写,以确保匹配时不区分大小写。
  • 停用词(stop)词元过滤器:从分词后的输出中移除常见的停用词,例如 “the”、“is” 和 “and”。

参考样例 #

以下命令创建一个名为 my_standard_index 并使用标准分词器的索引:

PUT /my_standard_index
{
  "mappings": {
    "properties": {
      "my_field": {
        "type": "text",
        "analyzer": "standard"
      }
    }
  }
}

参数说明 #

你可以使用以下参数来配置标准分词器。

参数必填/可选数据类型描述
max_token_length可选整数设置生成的词元的最大长度。如果超过此长度,词元将按照 max_token_length 中配置的长度拆分为多个词元。默认值为 255
stopwords可选字符串或字符串列表一个包含自定义停用词列表(如 _english)的字符串,或者一个自定义停用词列表的数组。默认值为 _none_
stopwords_path可选字符串包含停用词列表的文件的路径(相对于配置目录的绝对路径或相对路径)。

配置自定义分词器 #

以下命令配置一个索引,该索引带有一个等同于标准分词器的自定义分词器:

PUT /my_custom_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "stop"
          ]
        }
      }
    }
  }
}

产生的词元 #

以下请求用来检查分词器生成的词元:

POST /my_custom_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "The slow turtle swims away"
}

返回内容中包含了产生的词元

{
  "tokens": [
    {"token": "slow","start_offset": 4,"end_offset": 8,"type": "<ALPHANUM>","position": 1},
    {"token": "turtle","start_offset": 9,"end_offset": 15,"type": "<ALPHANUM>","position": 2},
    {"token": "swims","start_offset": 16,"end_offset": 21,"type": "<ALPHANUM>","position": 3},
    {"token": "away","start_offset": 22,"end_offset": 26,"type": "<ALPHANUM>","position": 4}
  ]
}