标准分析器

standard 分析器是 Milvus 中的默认分析器,若未指定分析器,系统会自动将其应用于文本字段。它采用基于语法的分词方式,因此对大多数语言都十分有效。

standard 分析器适用于那些依赖分隔符(如空格、标点符号)来确定词界限的语言。然而,中文、阿拉伯语、泰语、日语和韩语等语言则需要特定于该语言的词法分析或规范化处理。在这种情况下,请使用特定于该语言的分析器,例如 chinesearabic,或 thai,或使用带有专用分词器的自定义分析器,例如 linderaicu

定义

standard 分析器由以下部分组成:

  • 分词器:使用standard 分词器,根据语法规则将文本拆分为离散的单词单元。有关详细信息,请参阅“标准分词器”

  • 过滤器:使用lowercase 过滤器将所有词元转换为小写,从而支持不区分大小写的搜索。有关详细信息,请参阅“小写转换”

standard 分析器的功能等同于以下自定义分析器配置:

analyzer_params = {
    "tokenizer": "standard",
    "filter": ["lowercase"]
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "standard");
analyzerParams.put("filter", Collections.singletonList("lowercase"));
const analyzer_params = {
    "tokenizer": "standard",
    "filter": ["lowercase"]
};
analyzerParams := map[string]any{"tokenizer": "standard", "filter": []any{"lowercase"}}
# restful
analyzerParams='{
  "tokenizer": "standard",
  "filter": [
    "lowercase"
  ]
}'

配置

要将standard 分析器应用于字段,只需在analyzer_params 中将type 设置为standard ,并根据需要包含可选参数。

analyzer_params = {
    "type": "standard", # Specifies the standard analyzer type
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("type", "standard");
const analyzer_params = {
    "type": "standard", // Specifies the standard analyzer type
}
analyzerParams = map[string]any{"type": "standard"}
# restful
analyzerParams='{
  "type": "standard"
}'

standard 分析器支持以下可选参数:

参数

描述

stop_words

包含停用词列表的数组,这些词将在分词过程中被移除。默认值为_english_ ,即一组内置的常见英语停用词。

自定义停用词的配置示例:

analyzer_params = {
    "type": "standard", # Specifies the standard analyzer type
    "stop_words", ["of"] # Optional: List of words to exclude from tokenization
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("type", "standard");
analyzerParams.put("stop_words", Collections.singletonList("of"));
analyzer_params = {
    "type": "standard", // Specifies the standard analyzer type
    "stop_words", ["of"] // Optional: List of words to exclude from tokenization
}
analyzerParams = map[string]any{"type": "standard", "stop_words": []string{"of"}}
# restful

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

示例

在将分析器配置应用到Collection Schema之前,请使用run_analyzer 方法验证其行为。

分析器配置

analyzer_params = {
    "type": "standard",  # Standard analyzer configuration
    "stop_words": ["for"] # Optional: Custom stop words parameter
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("type", "standard");
analyzerParams.put("stop_words", Collections.singletonList("for"));
// javascript
analyzerParams = map[string]any{"type": "standard", "stop_words": []string{"for"}}
# restful
analyzerParams='{
  "type": "standard",
  "stop_words": [
    "of"
  ]
}'

使用以下方式进行验证run_analyzer

from pymilvus import (
    MilvusClient,
)

client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus"
)

# Sample text to analyze
sample_text = "The Milvus vector database is built for scale!"

# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard 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;

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 Milvus vector database is built for scale!");

RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
        .texts(texts)
        .analyzerParams(analyzerParams)
        .build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
// javascript
import (
    "context"
    "encoding/json"
    "fmt"

    "github.com/milvus-io/milvus/client/v2/milvusclient"
)

client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: "localhost:19530",
    APIKey:  "root:Milvus",
})
if err != nil {
    fmt.Println(err.Error())
    // handle error
}

bs, _ := json.Marshal(analyzerParams)
texts := []string{"The Milvus vector database is built for scale!"}
option := milvusclient.NewRunAnalyzerOption(texts).
    WithAnalyzerParams(string(bs))

result, err := client.RunAnalyzer(ctx, option)
if err != nil {
    fmt.Println(err.Error())
    // handle error
}
# restful

预期输出

Standard analyzer output: ['the', 'milvus', 'vector', 'database', 'is', 'built', 'scale']

翻译自DeepL

想要更快、更简单、更好用的 Milvus SaaS服务 ?

Zilliz Cloud是基于Milvus的全托管向量数据库,拥有更高性能,更易扩展,以及卓越性价比

免费试用 Zilliz Cloud
反馈

此页对您是否有帮助?