截断词元过滤器

编辑

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

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

此过滤器使用 Lucene 的 TruncateTokenFilter

示例

编辑

以下 analyze API 请求使用 truncate 过滤器来缩短 the quinquennial extravaganza carried on 中超过 10 个字符的词元。

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"
}

该过滤器产生以下词元:

[ the, quinquenni, extravagan, carried, on ]

添加到分析器

编辑

以下 create index 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
(可选,整数)每个词元的字符限制。超过此限制的词元将被截断。默认为 10

自定义

编辑

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

例如,以下请求创建一个自定义 truncate 过滤器 5_char_trunc,该过滤器将词元缩短为 length5 个或更少的字符。

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
        }
      }
    }
  }
}