標準分析器
standard 分析器是 Milvus 的預設分析器,如果沒有指定分析器,它會自動套用到文字欄位。它使用基於文法的標記化,對大多數語言都很有效。
standard 分析器適用於依賴分隔符(如空格、標點符號)作為字詞邊界的語言。然而,中文、日文和韓文等語言需要以字典為基礎的標記化。在這種情況下,使用特定語言的分析器,例如 chinese或具有專門標記化器的自訂分析器 (例如 lindera, icu)和過濾器,以確保正確的標記化和更好的搜尋結果。
定義
standard 分析器包括
標記化器:使用
standardtokenizer 根據文法規則將文字分割成離散的單字單位。如需詳細資訊,請參閱Standard Tokenizer。過濾器:使用
lowercase過濾器,將所有記號轉換為小寫,使搜尋不區分大小寫。如需詳細資訊,請參閱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 分析器接受下列可選參數:
參數 |
說明 |
|---|---|
|
一個包含停滯字清單的陣列,這些停滯字會從標記化中移除。預設為 |
自訂停止詞配置範例:
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 之後,您可以在定義集合模式時,將它們套用到VARCHAR 欄位。這可讓 Milvus 使用指定的分析器處理該欄位中的文字,以進行有效的標記化和過濾。如需更多資訊,請參考範例使用。
範例
在應用分析器配置到您的收集模式之前,請使用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']