BM25 関数
BM25 関数は、生テキストをスパースベクトルに変換し、語彙的関連性に基づいてドキュメントをスコアリングすることで、全文検索を可能にします。用語ベースのマッチングと頻度を考慮した重み付けを適用し、クエリ語に密接に一致するテキストドキュメントを効率的に取得できるようにします。
ローカルテキスト関数として、BM25 関数は Zilliz Cloud 内で実行され、モデル推論や外部連携を必要としません。これにより、テキストベースの検索シナリオに対して、決定論的で透過的な取得メカニズムを提供します。
BM25 の仕組み
BM25 アルゴリズムは、全文検索で広く使用されている用語ベースの関連性スコアリングアルゴリズムです。Zilliz Cloud では、BM25 はテキストを用語重み表現に変換し、分散スパースインデックスを使用して上位 K 件のドキュメントを取得するスパース検索パイプラインとして実装されています。
全体のワークフローは、ドキュメント取り込みとクエリテキスト処理という 2 つの対称的な経路で構成され、どちらも同じテキスト解析ロジックを共有します。
ドキュメント取り込み: テキストからスパース表現へ
ドキュメントが挿入されると、その生テキストはまず アナライザー によって処理され、テキストが個々の用語にトークン化されます。
たとえば、次のドキュメント:
"We are loving Milvus!"
は、次の用語に解析されます:
["we", "love", "milvus"]
その後、各ドキュメントは、ドキュメント内で各用語が何回出現するかを記録する用語頻度(TF)表現として表されます。たとえば:
{
"we": 1,
"love": 1,
"milvus": 1
}
同時に、Zilliz Cloud は次のようなコーパスレベルの統計情報を更新します:
-
各用語のドキュメント頻度(DF)
-
ドキュメントの平均長
-
各用語を、それを含むドキュメントに対応付けるポスティングリスト
ドキュメントの TF 表現は スパース埋め込み に挿入され、用語のポスティングはスケーラブルな検索のためにノード間で分割されます。
クエリテキスト処理: IDF の重み付けを適用する
テキストベースのクエリが発行されると、ドキュメント取り込み時に使用された同じアナライザーで処理され、一貫した用語分割が保証されます。
たとえば、次のクエリ:
"who loves Milvus?"
は、次のように解析されます:
["who", "love", "milvus"]
各クエリ用語に対して、Zilliz Cloud はコーパス統計からその 逆文書頻度(IDF)を参照します。IDF は、データセット全体にわたってその用語がどれほど情報量を持つかを反映します。出現頻度の低い用語ほど高い重みを受け、一般的な用語ほど低い重みを受けます。
概念的には、これにより次のような IDF 重み付きクエリ用語のセットが生成されます:
{
"who": 0.1,
"love": 0.5,
"milvus": 1.2
}
BM25 スコアリングと Top K 検索
BM25 は、一致したクエリ用語に基づいて関連性スコアを計算することでドキュメントを順位付けします。スコアリングは用語レベルで行われ、ドキュメントレベルで集約されます。
用語レベルのスコアリング
ドキュメント内に出現する各クエリ用語について、BM25 は次の用語レベルスコアを計算します:
term_score =
IDF(term) ×
TF_boost(term, document, k1) ×
length_normalization(document, b)
ここで:
-
IDF(term) は、その用語がコレクション内でどれだけ希少かを反映します
-
TF_boost(…, k1) は用語頻度とともに増加しますが、頻度が増えるにつれて飽和します
-
length_normalization(…, b) はドキュメント長に基づいてスコアを調整します
ドキュメントレベルのスコアリングと Top-K 検索
最終的なドキュメントスコアは、一致したすべてのクエリ用語に対する用語レベルスコアの合計です:
document_score =
sum of term_score over all matched query terms
ドキュメントは最終スコアによって順位付けされ、最も高スコアの上位 K 件のドキュメントが返されます。
始める前に
BM25 関数を使用する前に、語彙ベースの全文検索をサポートできるようにコレクションスキーマを計画してください:
-
生コンテンツ用のテキストフィールド
コレクションには、生テキストを格納するための
VARCHARフィールドを含める必要があります。このフィールドは、全文検索のために処理されるテキストのソースです。 -
テキストフィールド用のアナライザー
テキストフィールドではアナライザーを有効にする必要があります。アナライザーは、BM25 関数によって語彙的関連性が計算される前に、テキストがどのようにトークン化および正規化されるかを定義します。
デフォルトでは、Zilliz Cloud は空白と句読点に基づいてテキストをトークン化する組み込みアナライザーを提供します。アプリケーションでカスタムのトークン化や正規化の動作が必要な場合は、カスタムアナライザーを定義できます。詳細については、ユースケースに適したアナライザーを選ぶ を参照してください。
-
BM25 出力用のスパースベクトル
コレクションには、BM25 関数によって生成されるスパース表現を格納するための
SPARSE_FLOAT_VECTORフィールドを含める必要があります。このフィールドは、全文検索中のインデックス作成と検索に使用されます。
これらのスキーマレベルの考慮事項を整理したら、コレクションを作成し、BM25 関数を使用してください。
ステップ 1: BM25 関数付きのコレクションを作成する
BM25 関数を使用するには、コレクションの作成時にそれを定義する必要があります。この関数はコレクションスキーマの一部となり、データ挿入時および検索時に自動的に適用されます。
SDK 経由
スキーマフィールドを定義する
コレクションスキーマには、少なくとも次の 3 つの必須フィールドを含める必要があります:
-
プライマリフィールド: コレクション内の各エンティティを一意に識別します。
-
テキストフィールド (
VARCHAR): 生のテキストドキュメントを格納します。Zilliz Cloud が BM25 関連性ランキングのためにテキストを処理できるように、enable_analyzer=Trueを設定する必要があります。デフォルトでは、Zilliz Cloud はテキスト解析にstandardアナライザー を使用します。別のアナライザーを設定するには、アナライザーの概要 を参照してください。 -
スパースベクトルフィールド (
SPARSE_FLOAT_VECTOR): BM25 関数によって自動生成されるスパース埋め込みを格納します。
- Python
- Java
- Go
- NodeJS
- cURL
- C++
from pymilvus import MilvusClient, DataType, Function, FunctionType
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
schema = client.create_schema()
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True, auto_id=True) # Primary field
schema.add_field(field_name="text", datatype=DataType.VARCHAR, max_length=1000, enable_analyzer=True) # Text field
schema.add_field(field_name="sparse", datatype=DataType.SPARSE_FLOAT_VECTOR) # Sparse vector field; no dim required for sparse vectors
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
CreateCollectionReq.CollectionSchema schema = CreateCollectionReq.CollectionSchema.builder()
.build();
schema.addField(AddFieldReq.builder()
.fieldName("id")
.dataType(DataType.Int64)
.isPrimaryKey(true)
.autoID(true)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("text")
.dataType(DataType.VarChar)
.maxLength(1000)
.enableAnalyzer(true)
.build());
schema.addField(AddFieldReq.builder()
.fieldName("sparse")
.dataType(DataType.SparseFloatVector)
.build());
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/column"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("id").
WithDataType(entity.FieldTypeInt64).
WithIsPrimaryKey(true).
WithIsAutoID(true),
).WithField(entity.NewField().
WithName("text").
WithDataType(entity.FieldTypeVarChar).
WithEnableAnalyzer(true).
WithMaxLength(1000),
).WithField(entity.NewField().
WithName("sparse").
WithDataType(entity.FieldTypeSparseVector),
)
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
const schema = [
{
name: "id",
data_type: DataType.Int64,
is_primary_key: true,
},
{
name: "text",
data_type: "VarChar",
enable_analyzer: true,
enable_match: true,
max_length: 1000,
},
{
name: "sparse",
data_type: DataType.SparseFloatVector,
},
];
console.log(res.results)
export schema='{
"autoId": true,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "text",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 1000,
"enable_analyzer": true
}
},
{
"fieldName": "sparse",
"dataType": "SparseFloatVector"
}
]
}'
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"YOUR_CLUSTER_ENDPOINT", "YOUR_CLUSTER_TOKEN"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::CollectionSchemaPtr schema = std::make_shared<milvus::CollectionSchema>();
schema->AddField({"id", milvus::DataType::INT64, "", true, true});
schema->AddField(milvus::FieldSchema("text", milvus::DataType::VARCHAR).WithMaxLength(1000).EnableAnalyzer(true));
schema->AddField(milvus::FieldSchema("sparse", milvus::DataType::SPARSE_FLOAT_VECTOR));
BM25 関数を定義する
BM25 関数は、トークン化されたテキストを、BM25 スコアリングをサポートするスパースベクトルに変換します。
関数を定義し、スキーマに追加します:
- Python
- Java
- Go
- NodeJS
- cURL
- C++
bm25_function = Function(
name="text_bm25_emb", # Function name
input_field_names=["text"], # Name of the VARCHAR field containing raw text data
output_field_names=["sparse"], # Name of the SPARSE_FLOAT_VECTOR field reserved to store generated embeddings
function_type=FunctionType.BM25, # Set to `BM25`
)
schema.add_function(bm25_function)
import io.milvus.common.clientenum.FunctionType;
import io.milvus.v2.service.collection.request.CreateCollectionReq.Function;
import java.util.*;
schema.addFunction(Function.builder()
.functionType(FunctionType.BM25)
.name("text_bm25_emb")
.inputFieldNames(Collections.singletonList("text"))
.outputFieldNames(Collections.singletonList("sparse"))
.build());
function := entity.NewFunction().
WithName("text_bm25_emb").
WithInputFields("text").
WithOutputFields("sparse").
WithType(entity.FunctionTypeBM25)
schema.WithFunction(function)
const functions = [
{
name: 'text_bm25_emb',
description: 'bm25 function',
type: FunctionType.BM25,
input_field_names: ['text'],
output_field_names: ['sparse'],
params: {},
},
];
export schema='{
"autoId": true,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "text",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 1000,
"enable_analyzer": true
}
},
{
"fieldName": "sparse",
"dataType": "SparseFloatVector"
}
],
"functions": [
{
"name": "text_bm25_emb",
"type": "BM25",
"inputFieldNames": ["text"],
"outputFieldNames": ["sparse"],
"params": {}
}
]
}'
milvus::FunctionPtr function = std::make_shared<milvus::Function>("text_bm25_emb", milvus::FunctionType::BM25);
function->AddInputFieldName("text");
function->AddOutputFieldName("sparse");
schema->AddFunction(function);
インデックスを設定する
必要なフィールドと組み込み関数を含むスキーマを定義したら、コレクションのインデックスを設定します。このプロセスを簡素化するために、index_type として AUTOINDEX を使用します。これは、データ構造に基づいて最適なインデックスタイプを Zilliz Cloud が選択して設定できるオプションです。
- Python
- Java
- Go
- NodeJS
- cURL
- C++
index_params = client.prepare_index_params()
index_params.add_index(
field_name="sparse",
index_type="AUTOINDEX",
metric_type="BM25"
)
import io.milvus.v2.common.IndexParam;
Map<String,Object> params = new HashMap<>();
params.put("inverted_index_algo", "DAAT_MAXSCORE");
params.put("bm25_k1", 1.2);
params.put("bm25_b", 0.75);
List<IndexParam> indexes = new ArrayList<>();
indexes.add(IndexParam.builder()
.fieldName("sparse")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.BM25)
.extraParams(params)
.build());
indexOption := milvusclient.NewCreateIndexOption("my_collection", "sparse",
index.NewAutoIndex(entity.MetricType(entity.BM25)))
.WithExtraParam("inverted_index_algo", "DAAT_MAXSCORE")
.WithExtraParam("bm25_k1", 1.2)
.WithExtraParam("bm25_b", 0.75)
const index_params = [
{
field_name: "sparse",
metric_type: "BM25",
index_type: "SPARSE_INVERTED_INDEX",
params: {
"inverted_index_algo": "DAAT_MAXSCORE",
"bm25_k1": 1.2,
"bm25_b": 0.75
}
},
];
export indexParams='[
{
"fieldName": "sparse",
"metricType": "BM25",
"indexType": "AUTOINDEX",
"params":{
"inverted_index_algo": "DAAT_MAXSCORE",
"bm25_k1": 1.2,
"bm25_b": 0.75
}
}
]'
auto index_params = milvus::IndexDesc("sparse", "", milvus::IndxType::SPARSE_INVERTED_INDEX, milvus::MetricType::BM25);
index_params.AddExtraParam("inverted_index_algo", "DAAT_MAXSCORE");
index_params.AddExtraParam("bm25_k1", "1.2");
index_params.AddExtraParam("bm25_b", "0.75");
collection を作成する
次に、定義した schema と index パラメータを使用して collection を作成します。
- Python
- Java
- Go
- NodeJS
- cURL
- C++
client.create_collection(
collection_name='my_collection',
schema=schema,
index_params=index_params
)
import io.milvus.v2.service.collection.request.CreateCollectionReq;
CreateCollectionReq requestCreate = CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(schema)
.indexParams(indexes)
.build();
client.createCollection(requestCreate);
err = client.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption("my_collection", schema).
WithIndexOptions(indexOption))
if err != nil {
fmt.Println(err.Error())
// handle error
}
await client.create_collection(
collection_name: 'my_collection',
schema: schema,
index_params: index_params,
functions: functions
);
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d "{
\"collectionName\": \"my_collection\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
auto status = client->CreateCollection(milvus::CreateCollectionRequest()
.WithCollectionName("my_collection")
.WithCollectionSchema(schema))
.AddIndex(std::move(index_params));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
Web コンソール経由
または、Zilliz Cloud console で BM25 関数付きの collection を作成することもできます。
BM25 関数付きの collection を作成したら、テキストを挿入し、テキストクエリに基づく lexical search を実行できます。
ステップ 2: collection にテキストデータを挿入する
collection と index の設定が完了したら、テキストデータを挿入できます。このプロセスでは、生のテキストのみを指定すれば十分です。先ほど定義した BM25 関数が、各テキストエントリに対する sparse vector を自動的に生成します。
- Python
- Java
- Go
- NodeJS
- cURL
- C++
client.insert('my_collection', [
{'text': 'information retrieval is a field of study.'},
{'text': 'information retrieval focuses on finding relevant information in large datasets.'},
{'text': 'data mining and information retrieval overlap in research.'},
])
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.milvus.v2.service.vector.request.InsertReq;
Gson gson = new Gson();
List<JsonObject> rows = Arrays.asList(
gson.fromJson("{\"text\": \"information retrieval is a field of study.\"}", JsonObject.class),
gson.fromJson("{\"text\": \"information retrieval focuses on finding relevant information in large datasets.\"}", JsonObject.class),
gson.fromJson("{\"text\": \"data mining and information retrieval overlap in research.\"}", JsonObject.class)
);
client.insert(InsertReq.builder()
.collectionName("my_collection")
.data(rows)
.build());
// go
await client.insert({
collection_name: 'my_collection',
data: [
{'text': 'information retrieval is a field of study.'},
{'text': 'information retrieval focuses on finding relevant information in large datasets.'},
{'text': 'data mining and information retrieval overlap in research.'},
]);
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/insert" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"data": [
{"text": "information retrieval is a field of study."},
{"text": "information retrieval focuses on finding relevant information in large datasets."},
{"text": "data mining and information retrieval overlap in research."}
],
"collectionName": "my_collection"
}'
milvus::EntityRows data = {
{{"text", "information retrieval is a field of study."}},
{{"text", "information retrieval focuses on finding relevant information in large datasets."}},
{{"text", "data mining and information retrieval overlap in research."}}
};
milvus::InsertResponse response;
auto status = client->Insert(milvus::InsertRequest()
.WithCollectionName("my_collection")
.WithRowsData(std::move(data))
, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
ステップ 3: テキストクエリで検索する
collection にデータを挿入したら、生のテキストクエリを使って全文検索を実行できます。Zilliz Cloud はクエリを自動的に sparse vector に変換し、BM25 アルゴリズムを使用して一致した検索結果をランク付けしたうえで、上位 topK (limit) 件の結果を返します。
- Python
- Java
- Go
- NodeJS
- cURL
- C++
search_params = {
'params': {'level': 10},
}
res = client.search(
collection_name='my_collection',
data=['whats the focus of information retrieval?'],
anns_field='sparse',
output_fields=['text'], # Fields to return in search results; sparse field cannot be output
limit=3,
search_params=search_params
)
print(res)
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.EmbeddedText;
import io.milvus.v2.service.vector.response.SearchResp;
Map<String,Object> searchParams = new HashMap<>();
searchParams.put("level", 10);
SearchResp searchResp = client.search(SearchReq.builder()
.collectionName("my_collection")
.data(Collections.singletonList(new EmbeddedText("whats the focus of information retrieval?")))
.annsField("sparse")
.topK(3)
.searchParams(searchParams)
.outputFields(Collections.singletonList("text"))
.build());
annSearchParams := index.NewCustomAnnParam()
resultSets, err := client.Search(ctx, milvusclient.NewSearchOption(
"my_collection", // collectionName
3, // limit
[]entity.Vector{entity.Text("whats the focus of information retrieval?")},
).WithConsistencyLevel(entity.ClStrong).
WithANNSField("sparse").
WithAnnParam(annSearchParams).
WithOutputFields("text"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
for _, resultSet := range resultSets {
fmt.Println("IDs: ", resultSet.IDs.FieldData().GetScalars())
fmt.Println("Scores: ", resultSet.Scores)
fmt.Println("text: ", resultSet.GetColumn("text").FieldData().GetScalars())
}
await client.search(
collection_name: 'my_collection',
data: ['whats the focus of information retrieval?'],
anns_field: 'sparse',
output_fields: ['text'],
limit: 3,
params: {'level': 10},
)
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/search" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
--data-raw '{
"collectionName": "my_collection",
"data": [
"whats the focus of information retrieval?"
],
"annsField": "sparse",
"limit": 3,
"outputFields": [
"text"
],
"searchParams":{
"params":{}
}
}'
auto request = milvus::SearchRequest()
.WithCollectionName("my_collection")
.AddEmbeddedText("whats the focus of information retrieval?")
.WithLimit(3)
.WithAnnsField("sparse")
.AddOutputField("text");
milvus::SearchResponse response;
auto status = client->Search(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}