保留类型分词过滤器

保留类型分词过滤器 #

保留类型(keep_types)分词过滤器是一种用于文本分析的分词过滤器,它可用于控制保留或丢弃哪些词元类型。不同的分词器会产生不同的词元类型,例如 <HOST><NUM><ALPHANUM>

分词器keyword)、简单匹配(simple_pattern)和简单匹配拆分(simple_pattern_split)分词器不支持保留类型分词过滤器,因为这些分词器不支持词元类型属性。

参数说明 #

保留类型分词过滤器可以使用以下参数进行配置。

参数必需/可选数据类型描述
types必需字符串列表要保留或丢弃的词元类型列表(由 mode 参数决定)。
mode可选字符串是要包含(include)还是排除(exclude) types 中指定的词元类型。默认值为 include(包含)。

参考样例 #

以下示例请求创建了一个名为 test_index 的新索引,并配置了一个带有保留类型过滤器的分析器:

PUT /test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "keep_types_filter"]
        }
      },
      "filter": {
        "keep_types_filter": {
          "type": "keep_types",
          "types": ["<ALPHANUM>"]
        }
      }
    }
  }
}

产生的词元 #

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

GET /test_index/_analyze
{
  "analyzer": "custom_analyzer",
  "text": "Hello 2 world! This is an example."
}

返回内容包含产生的词元

{
  "tokens": [
    {
      "token": "hello",
      "start_offset": 0,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "world",
      "start_offset": 8,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "this",
      "start_offset": 15,
      "end_offset": 19,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "is",
      "start_offset": 20,
      "end_offset": 22,
      "type": "<ALPHANUM>",
      "position": 4
    },
    {
      "token": "an",
      "start_offset": 23,
      "end_offset": 25,
      "type": "<ALPHANUM>",
      "position": 5
    },
    {
      "token": "example",
      "start_offset": 26,
      "end_offset": 33,
      "type": "<ALPHANUM>",
      "position": 6
    }
  ]
}