分解器

decompounder 过滤器会根据指定的词典将复合词拆分为各个组成部分,从而更方便地搜索复合词的组成部分。该过滤器对于经常使用复合词的语言(如德语)特别有用。

组件词典可通过word_list 参数内联提供,也可通过word_list_file 参数从已注册的文件资源中加载。

配置

decompounder 过滤器既可以通过word_list 参数内联提供其成分词典,也可以通过word_list_file 参数从已注册的文件资源中加载。

内联词表

decompounder 过滤器是Milvus中的自定义过滤器。要使用它,请在过滤器配置中指定"type": "decompounder" ,并配合word_list 参数提供待识别的词素字典。

analyzer_params = {
    "tokenizer": "standard",
    "filter":[{
        "type": "decompounder", # Specifies the filter type as decompounder
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
    }],
}
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", "decompounder");
                    put("word_list", Arrays.asList("dampf", "schiff", "fahrt", "brot", "backen", "automat"));
                }}
        )
);
analyzerParams := map[string]any{"tokenizer": "standard",
    "filter": []any{map[string]any{
        "type":       "decompounder",
        "word_list": []string{"dampf", "schiff", "fahrt", "brot", "backen", "automat"},
    }}}
let analyzer_params = serde_json::json!({
    "tokenizer": "standard",
    "filter": [{
        "type": "decompounder",
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"]
    }]
});
nlohmann::json analyzer_params = {
    {"tokenizer", "standard"},
    {"filter", {
        {
            {"type", "decompounder"},
            {"word_list", {"dampf", "schiff", "fahrt", "brot", "backen", "automat"}}
        }
    }}
};
const analyzer_params = {
    "tokenizer": "standard",
    "filter":[{
        "type": "decompounder", // Specifies the filter type as decompounder
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
    }],
};
using System.Collections.Generic;

var analyzerParams = new Dictionary<string, object>
{
    ["tokenizer"] = "standard",
    ["filter"] = new object[]
    {
        new Dictionary<string, object>
        {
            ["type"] = "decompounder", // Specifies the filter type as decompounder
            ["word_list"] = new[] { "dampf", "schiff", "fahrt", "brot", "backen", "automat" }
        }
    }
};
# restful
analyzerParams='{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "decompounder",
      "word_list": [
        "dampf",
        "schiff",
        "fahrt",
        "brot",
        "backen",
        "automat"
      ]
    }
  ]
}'

decompounder 过滤器支持以下可配置参数。

参数

描述

word_list

用于拆分复合词的词素列表。该字典决定复合词如何被分解为单个词项。

decompounder 过滤器对分词器生成的词进行处理,因此必须与分词器配合使用。有关Milvus中可用分词器的列表,请参阅“标准分词器”及其相关页面。

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

从文件资源加载单词组件Compatible with Milvus 3.0.x

对于大型词素词典(尤其是全语言词表),请将词素存储在文件中,并将该文件注册为远程文件资源,然后通过word_list_file 参数在过滤器中引用该文件。您可以单独使用word_list_file ,也可以与内联的word_list 配合使用;当两者均设置时,过滤器会将这两个来源合并为一个词素列表。

该文件为纯 UTF‑8 文本,每行包含一个组件词。例如:

dampf
schiff
fahrt
brot
backen
automat

将文件上传至 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="de_components",
    path="file/decompounder.txt",    # full S3 object key, including the rootPath prefix
)

通过 `word_list_file` 在过滤器中引用已注册的资源:

analyzer_params = {
    "tokenizer": "standard",
    "filter": [{
        "type": "decompounder",
        "word_list_file": {
            "type": "remote",
            "resource_name": "de_components",
            "file_name": "decompounder.txt",
        },
    }],
}

word_list_file 参数接受包含以下字段的对象:

字段

描述

type

资源类型。对于通过add_file_resource 注册的文件,请使用"remote" 。对于自托管部署中使用的"local" 变体,请参阅《管理文件资源》。

resource_name

在add_file_resource 上注册文件时使用的名称。

file_name

已注册资源的对象存储路径中的文件名部分(例如,如果资源是在path="file/decompounder.txt" 上注册的,则为"decompounder.txt" )。

示例

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

分析器配置

analyzer_params = {
    "tokenizer": "standard",
    "filter":[{
        "type": "decompounder", # Specifies the filter type as decompounder
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
    }],
}
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", "decompounder");
                    put("word_list", Arrays.asList("dampf", "schiff", "fahrt", "brot", "backen", "automat"));
                }}
        )
);
analyzerParams := map[string]any{"tokenizer": "standard",
    "filter": []any{map[string]any{
        "type":       "decompounder",
        "word_list": []string{"dampf", "schiff", "fahrt", "brot", "backen", "automat"},
    }}}
let analyzer_params = serde_json::json!({
    "tokenizer": "standard",
    "filter": [{
        "type": "decompounder",
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"]
    }]
});
nlohmann::json analyzer_params = {
    {"tokenizer", "standard"},
    {"filter", {
        {
            {"type", "decompounder"},
            {"word_list", {"dampf", "schiff", "fahrt", "brot", "backen", "automat"}}
        }
    }}
};
const analyzer_params = {
    "tokenizer": "standard",
    "filter":[{
        "type": "decompounder", // Specifies the filter type as decompounder
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
    }],
};
using System.Collections.Generic;

var analyzerParams = new Dictionary<string, object>
{
    ["tokenizer"] = "standard",
    ["filter"] = new object[]
    {
        new Dictionary<string, object>
        {
            ["type"] = "decompounder", // Specifies the filter type as decompounder
            ["word_list"] = new[] { "dampf", "schiff", "fahrt", "brot", "backen", "automat" }
        }
    }
};
# restful
analyzerParams='{
  "tokenizer": "standard",
  "filter": [
    {
      "type": "decompounder",
      "word_list": [
        "dampf",
        "schiff",
        "fahrt",
        "brot",
        "backen",
        "automat"
      ]
    }
  ]
}'

使用以下方式进行验证run_analyzer

from pymilvus import (
    MilvusClient,
)

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

# Sample text to analyze
sample_text = "dampfschifffahrt brotbackautomat"

# 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("dampfschifffahrt brotbackautomat");

RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
        .texts(texts)
        .analyzerParams(analyzerParams)
        .build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
import (
    "context"
    "encoding/json"
    "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
}

bs, _ := json.Marshal(analyzerParams)
texts := []string{"dampfschifffahrt brotbackautomat"}
option := milvusclient.NewRunAnalyzerOption(texts...).
    WithAnalyzerParamsStr(string(bs))

result, err := client.RunAnalyzer(ctx, option)
if err != nil {
    fmt.Println(err.Error())
    // handle error
}
fmt.Println(result)
use milvus::v2::error::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": "decompounder",
        "word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"]
    }]
});

let result = client.run_analyzer(
    RunAnalyzerRequest::builder()
        .texts(["dampfschifffahrt brotbackautomat".to_string()])
        .analyzer_params(analyzer_params)
        .build()?,
).await?;
println!("{:?}", result.results());
#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 = "dampfschifffahrt brotbackautomat";
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;
}

for (const auto& result : response.Results()) {
    for (const auto& token : result.Tokens()) {
        std::cout << token.token_ << " ";
    }
    std::cout << std::endl;
}
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
  address: 'http://localhost:19530',
  token: 'root:Milvus'
});

const res = await client.runAnalyzer({
  text: 'dampfschifffahrt brotbackautomat',
  analyzer_params: analyzer_params
});

console.log(res);
using Milvus.Client.V2;
using Milvus.Client.V2.Requests.Utility;
using Milvus.Client.V2.Types;

using var client = new MilvusClientV2(new ConnectConfig { Uri = "http://localhost:19530", ApiKey = "root:Milvus" });
await client.ConnectAsync();

// Sample text to analyze
var sampleText = "dampfschifffahrt brotbackautomat";

var result = await client.RunAnalyzerAsync(new RunAnalyzerReq
{
    AnalyzerParams = analyzerParams,
    Texts = new[] { sampleText }
});
foreach (var r in result.Results)
{
    Console.WriteLine("Analyzer output: " + string.Join(", ", r.Tokens.Select(t => t.Token)));
}
# restful
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" \
  --data '{
    "analyzerParams": "{\"tokenizer\":\"standard\",\"filter\":[{\"type\":\"decompounder\",\"word_list\":[\"dampf\",\"schiff\",\"fahrt\",\"brot\",\"backen\",\"automat\"]}]}",
    "text": ["dampfschifffahrt brotbackautomat"]
  }'

预期输出

['dampf', 'schiff', 'fahrt', 'brotbackautomat']

翻译自DeepL

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

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

免费试用 Zilliz Cloud
反馈

此页对您是否有帮助?