词干提取器
stemmer 过滤器可将单词还原为其基本形式或词根(即词干化),从而更轻松地匹配不同词形变化中含义相近的单词。stemmer 过滤器支持多种语言,可在各种语言环境中实现高效的搜索和索引。
配置
stemmer 过滤器是Milvus中的自定义过滤器。要使用它,请在过滤器配置中指定"type": "stemmer" ,并配合language 参数来选择所需的词干化语言。
analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stemmer", # Specifies the filter type as stemmer
"language": "english", # Sets the language for stemming to English
}],
}
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", "stemmer");
put("language", "english");
}}
)
);
analyzerParams := map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "stemmer",
"language": "english",
}}}
let analyzer_params = serde_json::json!({
"tokenizer": "standard",
"filter": [
{ "type": "stemmer", "language": "english" }
]
});
nlohmann::json analyzer_params = {
{"tokenizer", "standard"},
{"filter", {
{
{"type", "stemmer"},
{"language", "english"}
}
}}
};
const analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stemmer", // Specifies the filter type as stemmer
"language": "english", // Sets the language for stemming to English
}],
};
using System.Collections.Generic;
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = "standard",
["filter"] = new object[]
{
new Dictionary<string, object> { ["type"] = "stemmer", ["language"] = "english" } // Specifies the filter type as stemmer and the language to English
},
};
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "stemmer",
"language": "english"
}
]
}'
stemmer 过滤器支持以下可配置参数。
| 参数 | 描述 |
|---|---|
language | 指定词干化处理所使用的语言。支持的语言包括:"arabic" 、"danish" 、"dutch" 、"english" 、"finnish" 、"french" 、"german" 、"greek" 、"hungarian" 、"italian" 、"norwegian" 、"portuguese" 、"romanian" 、"russian" 、"spanish" 、"swedish" 、"tamil" 、"turkish" |
stemmer 过滤器作用于分词器生成的词项,因此必须与分词器配合使用。
对于阿拉伯语文本,在大多数情况下请使用内置的 arabic 分析器。该内置分析器包含阿拉伯语词干提取功能,以及阿拉伯语规范化、小数位规范化和阿拉伯语停用词过滤功能。仅当需要构建自定义分析器管道时,才应直接使用stemmer 过滤器。
定义analyzer_params 后,您可以在定义Collection Schema时将其应用于VARCHAR 字段。这使Milvus能够使用指定的分析器处理该字段中的文本,从而实现高效的分词和过滤。有关详细信息,请参阅“使用示例”。
示例
在将分析器配置应用到 Collection Schema 之前,请使用run_analyzer 方法验证其行为。
分析器配置
analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stemmer", # Specifies the filter type as stemmer
"language": "english", # Sets the language for stemming to English
}],
}
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", "stemmer");
put("language", "english");
}}
)
);
analyzerParams := map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "stemmer",
"language": "english",
}}}
let analyzer_params = serde_json::json!({
"tokenizer": "standard",
"filter": [
{ "type": "stemmer", "language": "english" }
]
});
nlohmann::json analyzer_params = {
{"tokenizer", "standard"},
{"filter", {
{
{"type", "stemmer"},
{"language", "english"}
}
}}
};
const analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "stemmer", // Specifies the filter type as stemmer
"language": "english", // Sets the language for stemming to English
}],
};
using System.Collections.Generic;
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = "standard",
["filter"] = new object[]
{
new Dictionary<string, object> { ["type"] = "stemmer", ["language"] = "english" } // Specifies the filter type as stemmer and the language to English
},
};
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "stemmer",
"language": "english"
}
]
}'
使用以下方式进行验证run_analyzer
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
# Sample text to analyze
sample_text = "running runs looked ran runner"
# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard analyzer output:", result)
import java.util.*;
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;
ConnectConfig config = ConnectConfig.builder()
.uri("http://localhost:19530")
.token("root:Milvus")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
List<String> texts = new ArrayList<>();
texts.add("running runs looked ran runner");
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{"running runs looked ran runner"}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParams(analyzerParams)
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
for _, r := range result {
for _, token := range r.Tokens {
fmt.Println("Standard analyzer output:", token.Text)
}
}
use milvus::v2::error::Result;
use milvus::v2::prelude::*;
let config = ConnectConfig::new().uri("http://localhost:19530").token("root:Milvus");
let client = ClientV2::new(&config).await?;
let sample_text = "running runs looked ran runner";
let result = client
.run_analyzer(
RunAnalyzerRequest::builder()
.texts(vec![sample_text])
.analyzer_params(analyzer_params)
.build()?,
)
.await?;
println!("Standard analyzer output: {:?}", result);
#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 = "running runs looked ran runner";
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",
});
// Sample text to analyze
const sample_text = "running runs looked ran runner";
// Run the standard analyzer with the defined configuration
const result = await client.runAnalyzer({
text: sample_text,
analyzer_params: analyzer_params
});
console.log("Standard analyzer output:", result);
using Milvus.Client.V2;
using Milvus.Client.V2.Requests.Utility;
using Milvus.Client.V2.Responses.Utility;
using Milvus.Client.V2.Types;
var client = new MilvusClientV2(new ConnectConfig
{
Uri = "http://localhost:19530",
ApiKey = "root:Milvus"
});
await client.ConnectAsync();
// Sample text to analyze
var sampleText = "running runs looked ran runner";
// Run the standard analyzer with the defined configuration
RunAnalyzerResp result = await client.RunAnalyzerAsync(new RunAnalyzerReq
{
AnalyzerParams = analyzerParams,
Texts = new[] { sampleText }
});
Console.WriteLine("Standard analyzer output: " + string.Join(", ", result.Results.SelectMany(r => r.Tokens).Select(t => t.Token)));
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/common/run_analyzer" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"text": ["running runs looked ran runner"],
"analyzerParams": "{\"tokenizer\": \"standard\", \"filter\": [{\"type\": \"stemmer\", \"language\": \"english\"}]}"
}'
预期输出
['run', 'run', 'look', 'ran', 'runner']