ロードと解放
collection のロードは、その中で類似性検索やクエリを実行するための前提条件です。このページでは、collection のロードと解放の手順に焦点を当てます。
前提条件
collection をロードする前に、次の点を確認してください。
-
外部 collection の場合、index を作成する前に、collection とボリューム間でデータを同期するためにサブ秒リフレッシュを呼び出していることを確認してください。
-
少なくともすべての vector フィールドに index を作成しており、必要に応じて一部の scalar フィールドにも index を作成しています。
ロード動作
同じロードリクエストが外部 collection とマネージド collection の両方に適用されますが、外部 collection のロード戦略はターゲットアーキテクチャによって異なります。
| 環境 | メモリ動作 | 実行の詳細 |
|---|---|---|
| サービング cluster 内のマネージド collection | フルロード | 高パフォーマンスなアクセスのために、すべての index とデータ(vector および scalar フィールド)を直接メモリにロードします。 |
| スタンドアロンデータベース内の外部 collection | index のみロード | index のみをメモリにロードします。生データは、アクティブな検索やクエリ中に必要に応じてディスクから取得されます。 |
Collection のロード
collection をロードすると、Zilliz Cloud は検索やクエリに迅速に応答するために、index ファイルとすべてのフィールドの生データをメモリにロードします。collection のロード後に挿入されたエンティティは、自動的に index 作成およびロードされます。
次のコードスニペットは、collection をロードする方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
- C++
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 7. Load the collection
client.load_collection(
collection_name="my_collection"
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
import io.milvus.v2.service.collection.request.LoadCollectionReq;
import io.milvus.v2.service.collection.request.GetLoadStateReq;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
String CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT";
String TOKEN = "YOUR_CLUSTER_TOKEN";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 6. Load the collection
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("my_collection")
.build();
client.loadCollection(loadCollectionReq);
// 7. Get load state of the collection
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
// Output:
// true
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
// 7. Load the collection
res = await client.loadCollection({
collection_name: "my_collection"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.getLoadState({
collection_name: "my_collection"
})
console.log(res.state)
// Output
//
// LoadStateLoaded
//
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
loadTask, err := client.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle err
}
// sync wait collection to be loaded
err = loadTask.Await(ctx)
if err != nil {
fmt.Println(err.Error())
// handle error
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state)
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/load" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 100,
# "loadState": "LoadStateLoaded",
# "message": ""
# }
# }
#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;
}
status = client->LoadCollection(milvus::LoadCollectionRequest()
.WithCollectionName("my_collection"));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::GetLoadStateResponse response;
status = client->GetLoadState(milvus::GetLoadStateRequest()
.WithCollectionName("my_collection"),
response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::cout << std::to_string(response.State()) << std::endl;
特定フィールドのロード
Zilliz Cloud は、検索やクエリに関与するフィールドのみをロードできるため、メモリ使用量を削減し、検索パフォーマンスを向上させます。
次のコードスニペットは、my_collection という名前の collection を作成済みで、その collection に my_id と my_vector という 2 つのフィールドがあることを前提としています。
- Python
- Java
- NodeJS
- Go
- cURL
- C++
client.load_collection(
collection_name="my_collection",
load_fields=["my_id", "my_vector"] # Load only the specified fields
skip_load_dynamic_field=True # Skip loading the dynamic field
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
// 6. Load the collection
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("my_collection")
.loadFields(Arrays.asList("my_id", "my_vector"))
.skipLoadDynamicField(true)
.build();
client.loadCollection(loadCollectionReq);
// 7. Get load state of the collection
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
await client.load_collection({
collection_name: "my_collection",
load_fields: ["my_id", "my_vector"], // Load only the specified fields
skip_load_dynamic_field: true //Skip loading the dynamic field
});
const loadState = client.getCollectionLoadState({
collection_name: "my_collection",
})
console.log(loadState);
loadTask, err := client.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("my_collection").
WithLoadFields("my_id", "my_vector"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
// sync wait collection to be loaded
err = loadTask.Await(ctx)
if err != nil {
fmt.Println(err.Error())
// handle error
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state)
# REST
# Not supported yet
auto status = client->LoadCollection(milvus::LoadCollectionRequest()
.WithCollectionName("my_collection")
.AddLoadField("my_id")
.AddLoadField("my_vector")
.WithSkipDynamicField(true));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::GetLoadStateResponse response;
status = client->GetLoadState(milvus::GetLoadStateRequest()
.WithCollectionName("my_collection"),
response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::cout << std::to_string(response.State()) << std::endl;
特定のフィールドをロードすることを選択した場合、load_fields に含まれるフィールドのみを、検索やクエリのフィルターおよび出力フィールドとして使用できる点に注意してください。load_fields には、常にプライマリフィールドの名前と少なくとも 1 つの vector フィールドを含める必要があります。
また、skip_load_dynamic_field を使用して、dynamic field をロードするかどうかを決定できます。dynamic field は $meta という名前の予約済み JSON フィールドであり、スキーマで定義されていないすべてのフィールドとその値をキーと値のペアで保存します。dynamic field をロードすると、フィールド内のすべてのキーがロードされ、フィルタリングと出力に使用できるようになります。dynamic field 内のすべてのキーがメタデータのフィルタリングと出力に関与しない場合は、skip_load_dynamic_field を True に設定します。
collection のロード後にさらにフィールドをロードするには、index の変更によって発生する可能性のあるエラーを避けるため、まず collection を解放する必要があります。
Collection の解放
検索とクエリはメモリを大量に消費する操作です。コストを節約するため、現在使用していない collection を解放することをお勧めします。
次のコードスニペットは、collection を解放する方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
- C++
# 8. Release the collection
client.release_collection(
collection_name="my_collection"
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# Output
#
# {
# "state": "<LoadState: NotLoad>"
# }
import io.milvus.v2.service.collection.request.ReleaseCollectionReq;
// 8. Release the collection
ReleaseCollectionReq releaseCollectionReq = ReleaseCollectionReq.builder()
.collectionName("my_collection")
.build();
client.releaseCollection(releaseCollectionReq);
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
// Output:
// false
// 8. Release the collection
res = await client.releaseCollection({
collection_name: "my_collection"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.getLoadState({
collection_name: "my_collection"
})
console.log(res.state)
// Output
//
// LoadStateNotLoad
//
err = client.ReleaseCollection(ctx, milvusclient.NewReleaseCollectionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state)
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/release" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 0,
# "loadState": "LoadStateNotLoaded",
# "message": ""
# }
# }
auto status = client->ReleaseCollection(milvus::ReleaseCollectionRequest()
.WithCollectionName("my_collection"));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::GetLoadStateResponse response;
status = client->GetLoadState(milvus::GetLoadStateRequest()
.WithCollectionName("my_collection"),
response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::cout << std::to_string(response.State()) << std::endl;