停止
stop 过滤器可从分词后的文本中移除指定的停用词,从而帮助剔除常见且意义较小的词汇。您可以通过stop_words 参数配置停用词列表。
配置
stop 过滤器支持通过stop_words 参数内联指定停用词列表,或通过stop_words_file 参数从已注册的文件资源中读取停用词列表。
内联停用词列表
若要使用内联列表来配置stop 过滤器,请在过滤器配置中指定"type": "stop" ,并配合stop_words 参数提供停用词列表。
analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stop", # Specifies the filter type as stop
"stop_words": ["of", "to", "_english_"], # Defines custom stop words and includes the English stop word list
}],
}
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "standard");
analyzerParams.put("filter",
Collections.singletonList(
new HashMap<String, Object>() {{
put("type", "stop");
put("stop_words", Arrays.asList("of", "to", "_english_"));
}}
)
);
analyzerParams := map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "stop",
"stop_words": []string{"of", "to", "_english_"},
}}}
use milvus::v2::prelude::*;
use serde_json::json;
let analyzer_params = json!({
"tokenizer": "standard",
"filter": [{
"type": "stop",
"stop_words": ["of", "to", "_english_"]
}]
});
nlohmann::json analyzer_params = {
{"tokenizer", "standard"},
{"filter", {
{{"type", "stop"}, {"stop_words", {"of", "to", "_english_"}}}
}}
};
const analyzer_params = {
tokenizer: "standard",
filter: [{
type: "stop", // Specifies the filter type as stop
stop_words: ["of", "to", "_english_"], // Custom stop words + built-in English list
}],
};
using System.Collections.Generic;
Dictionary<string, object> analyzerParams = new()
{
["tokenizer"] = "standard",
["filter"] = new List<object>
{
new Dictionary<string, object>
{
["type"] = "stop", // Specifies the filter type as stop
["stop_words"] = new List<object> { "of", "to", "_english_" } // Defines custom stop words and includes the English stop word list
}
}
};
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "stop",
"stop_words": [
"of",
"to",
"_english_"
]
}
]
}'
stop 过滤器支持以下可配置参数。
参数 |
描述 |
|---|---|
|
要从分词过程中移除的词汇列表。默认情况下,该过滤器使用内置的
|
stop 过滤器作用于分词器生成的术语,因此必须与分词器配合使用。有关 Milvus 中可用分词器的列表,请参阅“标准分词器”及其相关页面。
定义analyzer_params 后,您可以在定义Collection模式时将其应用于VARCHAR 字段。这使Milvus能够使用指定的分析器处理该字段中的文本,从而实现高效的分词和过滤。有关详细信息,请参阅“使用示例”。
从文件资源加载停用词Compatible with Milvus 3.0.x
对于大型自定义停用词列表(如特定语言的列表、领域词汇表,或希望在多个Collection中共享的列表),请将词汇存储在文件中,并将该文件注册为远程文件资源,然后通过stop_words_file 参数在过滤器中引用该文件。 您可以单独使用stop_words_file ,也可以与内联的stop_words 配合使用;当两者都设置时,过滤器会将这两个来源合并为一个停用词列表。
该文件应为纯 UTF‑8 文本,每行包含一个停用词。例如:
the
of
for
将该文件上传至 Milvus 集群配置中指定的对象存储,然后进行注册:
from pymilvus import MilvusClient
client = MilvusClient(uri="http://localhost:19530")
# Register the uploaded file under a name you'll reference from analyzer configs.
client.add_file_resource(
name="en_stop_words",
path="file/stop_words.txt", # full S3 object key, including the rootPath prefix
)
通过 `stop_words_file` 在过滤器中引用已注册的资源:
analyzer_params = {
"tokenizer": "standard",
"filter": [{
"type": "stop",
"stop_words_file": {
"type": "remote",
"resource_name": "en_stop_words",
"file_name": "stop_words.txt",
},
}],
}
stop_words_file 参数接受包含以下字段的对象:
字段 |
描述 |
|---|---|
|
资源类型。对于通过 |
|
在向 |
|
已注册资源的对象存储路径中的文件名部分(例如,如果资源是在 |
示例
在将分析器配置应用到 Collection Schema 之前,请使用run_analyzer 方法验证其行为。
分析器配置
analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stop", # Specifies the filter type as stop
"stop_words": ["of", "to", "_english_"], # Defines custom stop words and includes the English stop word list
}],
}
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "standard");
analyzerParams.put("filter",
Collections.singletonList(
new HashMap<String, Object>() {{
put("type", "stop");
put("stop_words", Arrays.asList("of", "to", "_english_"));
}}
)
);
analyzerParams := map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "stop",
"stop_words": []string{"of", "to", "_english_"},
}}}
use milvus::v2::prelude::*;
use serde_json::json;
let analyzer_params = json!({
"tokenizer": "standard",
"filter": [{
"type": "stop",
"stop_words": ["of", "to", "_english_"]
}]
});
nlohmann::json analyzer_params = {
{"tokenizer", "standard"},
{"filter", {
{{"type", "stop"}, {"stop_words", {"of", "to", "_english_"}}}
}}
};
const analyzer_params = {
tokenizer: "standard",
filter: [{
type: "stop", // Specifies the filter type as stop
stop_words: ["of", "to", "_english_"], // Custom stop words + built-in English list
}],
};
using System.Collections.Generic;
Dictionary<string, object> analyzerParams = new()
{
["tokenizer"] = "standard",
["filter"] = new List<object>
{
new Dictionary<string, object>
{
["type"] = "stop", // Specifies the filter type as stop
["stop_words"] = new List<object> { "of", "to", "_english_" } // Defines custom stop words and includes the English stop word list
}
}
};
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "stop",
"stop_words": [
"of",
"to",
"_english_"
]
}
]
}'
使用以下方式进行验证run_analyzer
from pymilvus import (
MilvusClient,
)
client = MilvusClient(uri="http://localhost:19530", token="root:Milvus")
# Sample text to analyze
sample_text = "The stop filter allows control over common stop words for text processing."
# Run the analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Analyzer output:", result)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.RunAnalyzerReq;
import io.milvus.v2.service.vector.response.RunAnalyzerResp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
ConnectConfig config = ConnectConfig.builder()
.uri("http://localhost:19530")
.token("root:Milvus")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
List<String> texts = new ArrayList<>();
texts.add("The stop filter allows control over common stop words for text processing.");
RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
.texts(texts)
.analyzerParams(analyzerParams)
.build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
ctx := context.Background()
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "http://localhost:19530",
APIKey: "root:Milvus",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
texts := []string{"The stop filter allows control over common stop words for text processing."}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParams(analyzerParams)
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(result)
use milvus::v2::prelude::*;
use serde_json::json;
let config = ConnectConfig::new().uri("http://localhost:19530").token("root:Milvus");
let client = ClientV2::new(&config).await?;
let analyzer_params = json!({
"tokenizer": "standard",
"filter": [{
"type": "stop",
"stop_words": ["of", "to", "_english_"]
}]
});
let result = client
.run_analyzer(
RunAnalyzerRequest::builder()
.texts(vec!["The stop filter allows control over common stop words for text processing."])
.analyzer_params(analyzer_params)
.build()?,
)
.await?;
#include "milvus/MilvusClientV2.h"
#include <iostream>
#include <string>
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"http://localhost:19530", "root:Milvus"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::string text = "The stop filter allows control over common stop words for text processing.";
auto request = milvus::RunAnalyzerRequest()
.AddText(text)
.WithAnalyzerParams(analyzer_params);
milvus::RunAnalyzerResponse response;
status = client->RunAnalyzer(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({
address: "http://localhost:19530",
token: "root:Milvus",
});
const sample_text = "The stop filter allows control over common stop words for text processing.";
// Run the analyzer with the defined configuration
const result = await client.runAnalyzer({
text: sample_text,
analyzer_params,
});
console.log("Analyzer output:", result);
using Milvus.Client.V2;
using Milvus.Client.V2.Requests.Utility;
using Milvus.Client.V2.Responses.Utility;
using Milvus.Client.V2.Types;
MilvusClientV2 client = new(new ConnectConfig
{
Uri = "http://localhost:19530",
ApiKey = "root:Milvus"
});
// Sample text to analyze
string sampleText = "The stop filter allows control over common stop words for text processing.";
// Run the analyzer with the defined configuration
RunAnalyzerResp result = await client.RunAnalyzerAsync(new RunAnalyzerReq
{
AnalyzerParams = analyzerParams,
Texts = [sampleText]
});
foreach (AnalyzerToken t in result.Results.SelectMany(r => r.Tokens))
{
Console.WriteLine(t.Token);
}
# restful
curl --request POST \
--url "http://localhost:19530/v2/vectordb/common/run_analyzer" \
--header "Authorization: Bearer root:Milvus" \
--header "Content-Type: application/json" \
-d '{
"text": ["The stop filter allows control over common stop words for text processing."],
"analyzerParams": "{\"tokenizer\": \"standard\", \"filter\": [{\"type\": \"stop\", \"stop_words\": [\"of\", \"to\", \"_english_\"]}]}"
}'
预期输出
['The', 'stop', 'filter', 'allows', 'control', 'over', 'common', 'stop', 'words', 'text', 'processing']