ロード & リリース
コレクションのロードは、コレクションでの類似検索およびクエリを実行するための前提条件です。このページでは、コレクションのロードおよびリリース手順について説明します。
コレクションのロード
コレクションをロードすると、Zilliz Cloudはインデックスファイルとすべてのフィールドの生データをメモリにロードし、検索およびクエリに迅速に対応できるようにします。コレクションのロード後に挿入されたエンティティは自動的にインデックス化およびロードされます。
以下のコードスニペットは、コレクションをロードする方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 7. コレクションをロード
client.load_collection(
collection_name="my_collection"
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# 出力
#
# {
# "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. Milvusサーバーに接続
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 6. コレクションをロード
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("my_collection")
.build();
client.loadCollection(loadCollectionReq);
// 7. コレクションのロード状態を取得
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
// 出力:
// 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. コレクションをロード
res = await client.loadCollection({
collection_name: "my_collection"
})
console.log(res.error_code)
// 出力
//
// Success
//
res = await client.getLoadState({
collection_name: "my_collection"
})
console.log(res.state)
// 出力
//
// 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())
// エラー処理
}
defer client.Close(ctx)
loadTask, err := client.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// エラー処理
}
// コレクションがロードされるまで同期待機
err = loadTask.Await(ctx)
if err != nil {
fmt.Println(err.Error())
// エラー処理
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.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" \
-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" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 100,
# "loadState": "LoadStateLoaded",
# "message": ""
# }
# }
特定のフィールドをロード
Zilliz Cloudでは、検索およびクエリに関与するフィールドのみをロードすることで、メモリ使用量を削減し、検索パフォーマンスを向上させることができます。
以下のコードスニペットでは、my_collectionという名前のコレクションが作成済みで、コレクションにはmy_idおよびmy_vectorという名前の2つのフィールドが存在すると仮定しています。
- Python
- Java
- NodeJS
- Go
- cURL
client.load_collection(
collection_name="my_collection",
load_fields=["my_id", "my_vector"] # 指定したフィールドのみをロード
skip_load_dynamic_field=True # ダイナミックフィールドのロードをスキップ
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# 出力
#
# {
# "state": "<LoadState: Loaded>"
# }
// 6. コレクションをロード
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("my_collection")
.loadFields(Arrays.asList("my_id", "my_vector"))
.build();
client.loadCollection(loadCollectionReq);
// 7. コレクションのロード状態を取得
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"], // 指定したフィールドのみをロード
skip_load_dynamic_field: true // ダイナミックフィールドのロードをスキップ
});
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())
// エラー処理
}
// コレクションがロードされるまで同期待機
err = loadTask.Await(ctx)
if err != nil {
fmt.Println(err.Error())
// エラー処理
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// エラー処理
}
fmt.Println(state)
# REST
# 未対応
特定のフィールドをロードすることを選択した場合、load_fieldsに含まれるフィールドのみが検索およびクエリでのフィルターおよび出力フィールドとして使用できることに注意してください。常に、主キーフィールドの名前と少なくとも1つのベクトルフィールドをload_fieldsに含める必要があります。
また、skip_load_dynamic_fieldを使用して、ダイナミックフィールドをロードするかどうかを決定することもできます。ダイナミックフィールドは**$meta**という名前の予約済みJSONフィールドであり、スキーマ定義されていないすべてのフィールドとその値をキーと値のペアで保存します。ダイナミックフィールドをロードする際、フィールド内のすべてのキーがロードされ、フィルターおよび出力で使用可能になります。ダイナミックフィールド内のすべてのキーがメタデータのフィルタリングおよび出力に関与しない場合は、skip_load_dynamic_fieldをTrueに設定してください。
コレクションのロード後にさらに多くのフィールドをロードするには、インデックスの変更によるエラーを回避するために、最初にコレクションをリリースする必要があります。
コレクションのリリース
検索およびクエリはメモリを大量に使用する操作です。コストを節約するために、現在使用していないコレクションをリリースすることをお勧めします。
以下のコードスニペットは、コレクションをリリースする方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
# 8. コレクションをリリース
client.release_collection(
collection_name="my_collection"
)
res = client.get_load_state(
collection_name="my_collection"
)
print(res)
# 出力
#
# {
# "state": "<LoadState: NotLoad>"
# }
import io.milvus.v2.service.collection.request.ReleaseCollectionReq;
// 8. コレクションをリリース
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);
// 出力:
// false
// 8. コレクションをリリース
res = await client.releaseCollection({
collection_name: "my_collection"
})
console.log(res.error_code)
// 出力
//
// Success
//
res = await client.getLoadState({
collection_name: "my_collection"
})
console.log(res.state)
// 出力
//
// LoadStateNotLoad
//
err = client.ReleaseCollection(ctx, milvusclient.NewReleaseCollectionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// エラー処理
}
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection"))
if err != nil {
fmt.Println(err.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" \
-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" \
-d '{
"collectionName": "my_collection"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 0,
# "loadState": "LoadStateNotLoaded",
# "message": ""
# }
# }