跳到主要内容

Length

length 过滤器删除不符合指定长度要求的词项,使您能够控制在文本处理过程中保留的词项长度。

配置

length 过滤器是 Zilliz Cloud 中的自定义过滤器,通过在过滤器配置中设置 "type": "length" 来指定。您可以将其配置为分析器参数中的字典,以定义长度限制。

analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "length", # Specifies the filter type as length
"max": 10, # Sets the maximum token length to 10 characters
}],
}

length 过滤器接受以下可选参数。

参数

描述

max

设置词元的最大长度。长度大于 max 的词元将会被移除,不会出现在分词结果中。

length 过滤器作用于分词器生成的词项,因此必须与分词器结合使用。有关 Zilliz Cloud 中可用的分词器列表,请参阅分词器参考

定义 analyzer_params 后,您可以在定义 Collection Schema 时将其应用于 VARCHAR 字段。这使得 Zilliz Cloud 能够使用指定的分析器处理该字段中的文本,以实现高效的分词和过滤。更多信息,请参阅使用示例

示例输出

在完成 Analyzer 配置后,您可以使用 run_analyzer 方法来验证分词效果是否符合预期。

Analyzer 配置

analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "length", # Specifies the filter type as length
"max": 10, # Sets the maximum token length to 10 characters
}],
}

使用 run_analyzer 验证效果

from pymilvus import (
MilvusClient,
)

client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")

# Sample text to analyze
sample_text = "The length filter allows control over token length requirements for text processing."

# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard analyzer output:", result)

预期结果

['The', 'length', 'filter', 'allows', 'control', 'over', 'token', 'length', 'for', 'text', 'processing']