截断词元过滤器

截断词元过滤器 #

截断(truncate)词元过滤器用于缩短超过指定长度的词元。它会将词元修剪至最大字符数,确保超过该限制的词元被截断。

参数说明 #

截断词元过滤器可以使用以下参数进行配置:

参数必需/可选数据类型描述
length可选整数指定生成的词元的最大长度。默认值为 10。

参考样例 #

以下示例请求创建了一个名为 truncate_example 的新索引,并配置了一个带有截断过滤器的分词器。

PUT /truncate_example
{
  "settings": {
    "analysis": {
      "filter": {
        "truncate_filter": {
          "type": "truncate",
          "length": 5
        }
      },
      "analyzer": {
        "truncate_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "truncate_filter"
          ]
        }
      }
    }
  }
}

产生的词元 #

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

GET /truncate_example/_analyze
{
  "analyzer": "truncate_analyzer",
  "text": "Easysearch is powerful and scalable"
}

返回内容包含产生的词元

{
  "tokens": [
    {
      "token": "easys",
      "start_offset": 0,
      "end_offset": 10,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "is",
      "start_offset": 11,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "power",
      "start_offset": 14,
      "end_offset": 22,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "and",
      "start_offset": 23,
      "end_offset": 26,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "scala",
      "start_offset": 27,
      "end_offset": 35,
      "type": "<ALPHANUM>",
      "position": 4
    }
  ]
}