截断 Token 过滤器

编辑

截断超过指定字符限制的 Token。此限制默认为10,但可以使用length参数自定义。

例如,您可以使用truncate过滤器将所有 Token 缩短至3个字符或更少,将jumping fox更改为jum fox

此过滤器使用 Lucene 的TruncateTokenFilter

示例

编辑

以下分析 API请求使用truncate过滤器缩短the quinquennial extravaganza carried on中超过 10 个字符的 Token。

resp = client.indices.analyze(
    tokenizer="whitespace",
    filter=[
        "truncate"
    ],
    text="the quinquennial extravaganza carried on",
)
print(resp)
response = client.indices.analyze(
  body: {
    tokenizer: 'whitespace',
    filter: [
      'truncate'
    ],
    text: 'the quinquennial extravaganza carried on'
  }
)
puts response
const response = await client.indices.analyze({
  tokenizer: "whitespace",
  filter: ["truncate"],
  text: "the quinquennial extravaganza carried on",
});
console.log(response);
GET _analyze
{
  "tokenizer" : "whitespace",
  "filter" : ["truncate"],
  "text" : "the quinquennial extravaganza carried on"
}

过滤器生成以下 Token:

[ the, quinquenni, extravagan, carried, on ]

添加到分析器

编辑

以下创建索引 API请求使用truncate过滤器配置新的自定义分析器

resp = client.indices.create(
    index="custom_truncate_example",
    settings={
        "analysis": {
            "analyzer": {
                "standard_truncate": {
                    "tokenizer": "standard",
                    "filter": [
                        "truncate"
                    ]
                }
            }
        }
    },
)
print(resp)
response = client.indices.create(
  index: 'custom_truncate_example',
  body: {
    settings: {
      analysis: {
        analyzer: {
          standard_truncate: {
            tokenizer: 'standard',
            filter: [
              'truncate'
            ]
          }
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "custom_truncate_example",
  settings: {
    analysis: {
      analyzer: {
        standard_truncate: {
          tokenizer: "standard",
          filter: ["truncate"],
        },
      },
    },
  },
});
console.log(response);
PUT custom_truncate_example
{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "standard_truncate" : {
        "tokenizer" : "standard",
        "filter" : ["truncate"]
        }
      }
    }
  }
}

可配置参数

编辑
length
(可选,整数) 每个 Token 的字符限制。超过此限制的 Token 将被截断。默认为10

自定义

编辑

要自定义truncate过滤器,请复制它以创建新自定义 Token 过滤器的基础。您可以使用其可配置参数修改过滤器。

例如,以下请求创建了一个自定义truncate过滤器5_char_trunc,它将 Token 缩短到5个或更少字符的length

resp = client.indices.create(
    index="5_char_words_example",
    settings={
        "analysis": {
            "analyzer": {
                "lowercase_5_char": {
                    "tokenizer": "lowercase",
                    "filter": [
                        "5_char_trunc"
                    ]
                }
            },
            "filter": {
                "5_char_trunc": {
                    "type": "truncate",
                    "length": 5
                }
            }
        }
    },
)
print(resp)
response = client.indices.create(
  index: '5_char_words_example',
  body: {
    settings: {
      analysis: {
        analyzer: {
          "lowercase_5_char": {
            tokenizer: 'lowercase',
            filter: [
              '5_char_trunc'
            ]
          }
        },
        filter: {
          "5_char_trunc": {
            type: 'truncate',
            length: 5
          }
        }
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "5_char_words_example",
  settings: {
    analysis: {
      analyzer: {
        lowercase_5_char: {
          tokenizer: "lowercase",
          filter: ["5_char_trunc"],
        },
      },
      filter: {
        "5_char_trunc": {
          type: "truncate",
          length: 5,
        },
      },
    },
  },
});
console.log(response);
PUT 5_char_words_example
{
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase_5_char": {
          "tokenizer": "lowercase",
          "filter": [ "5_char_trunc" ]
        }
      },
      "filter": {
        "5_char_trunc": {
          "type": "truncate",
          "length": 5
        }
      }
    }
  }
}