Load 和 Release
对 Collection 执行 Load 操作是在 Collection 中进行 Search 和 Query 的前提条件。本节主要介绍如何 Load 和 Release Collection。
Load Collection
在加载 Collection 时,Zilliz Cloud 会将所有向量列的索引文件和所有标量列的数据加载到QueryNode,从而快速响应搜索和查询请求。在 Collection 加载后插入的数据会自动完成索引和加载。
您可以参考如下代码 Load Collection。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 7. Load the collection
client.load_collection(
collection_name="customized_setup_1"
)
res = client.get_load_state(
collection_name="customized_setup_1"
)
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("customized_setup_1")
.build();
client.loadCollection(loadCollectionReq);
// 7. Get load state of the collection
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("customized_setup_1")
.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: "customized_setup_1"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.getLoadState({
collection_name: "customized_setup_1"
})
console.log(res.state)
// Output
//
// LoadStateLoaded
//
import (
"context"
"fmt"
"log"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
loadTask, err := cli.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("customized_setup_1"))
if err != nil {
// handle error
}
// sync wait collection to be loaded
err = loadTask.Await(ctx)
if err != nil {
// handle error
}
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": "customized_setup_1"
}'
# {
# "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": "customized_setup_1"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 100,
# "loadState": "LoadStateLoaded",
# "message": ""
# }
# }
Load 部分字段
为了节约内存资源,您可以选择让 Zilliz Cloud 仅加载参与 Search 和 Query 的部分字段,提升搜索性能。
如下代码中假设名为 customized_setup_1 的 Collection 中有名为 my_id 和 my_vector 两个字段。
- Python
- Java
- NodeJS
- Go
- cURL
client.load_collection(
collection_name="custom_quick_setup",
# highlight-next-line
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="custom_quick_setup"
)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
// 6. Load the collection
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("custom_quick_setup")
.loadFields(Arrays.asList("my_id", "my_vector"))
.build();
client.loadCollection(loadCollectionReq);
// 7. Get load state of the collection
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("custom_quick_setup")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
await client.load_collection({
collection_name: "custom_quick_setup",
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: "custom_quick_setup",
})
console.log(loadState);
import (
"context"
"fmt"
"log"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
loadTask, err := cli.LoadCollection(ctx, milvusclient.NewLoadCollectionOption("custom_quick_setup").
WithLoadFields("my_id", "my_vector"))
if err != nil {
// handle error
}
// sync wait collection to be loaded
err = loadTask.Await(ctx)
if err != nil {
// handle error
}
# REST 缺失
需要注意的是,当您选择只加载部分字段时,只有在 load_fields
中的字段可以做为过滤条件(filter)和输出字段(Output Fields)使用。请务必在 load_fields
包含主键和至少一个已创建索引的向量字段。
您还可以使用 skip_load_dynamic_field
来决定是否加载 Dynamic Field。Dynamic Field 是一个名为 $meta 的预留 JSON 字段,用于以键值对的形式存放各 Entity 中所有未在 Schema 中定义的字段及其值。在加载 Dynamic Field 时,字段中所有键都会被加载,并可用于过滤和输出。如果 Dynamic Field 中的键无须参与过滤或输出,您可以将 skip_load_dynamic_field
设置为 True
。
如果在执行本操作后需要 Load 更多字段,请务必先对 Collection 执行 Release 操作,避免因索引结构发生变化而引发系统报错。
Release Collection
由于 Search 和 Query 操作会占用较多的内存资源。为了减少资源消耗,您可以对暂时不需要使用的 Collection 执行 Release 操作,将相关数据从内存中释放出来。
如下示例演示了如何 Release Collection。
- Python
- Java
- NodeJS
- Go
- cURL
# 8. Release the collection
client.release_collection(
collection_name="custom_quick_setup"
)
res = client.get_load_state(
collection_name="custom_quick_setup"
)
print(res)
# Output
#
# {
# "state": "<LoadState: NotLoad>"
# }
import io.milvus.v2.service.collection.request.ReleaseCollectionReq;
// 8. Release the collection
ReleaseCollectionReq releaseCollectionReq = ReleaseCollectionReq.builder()
.collectionName("custom_quick_setup")
.build();
client.releaseCollection(releaseCollectionReq);
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("custom_quick_setup")
.build();
Boolean res = client.getLoadState(loadStateReq);
System.out.println(res);
// Output:
// false
// 8. Release the collection
res = await client.releaseCollection({
collection_name: "custom_quick_setup"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.getLoadState({
collection_name: "custom_quick_setup"
})
console.log(res.state)
// Output
//
// LoadStateNotLoad
//
import (
"context"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
err := cli.ReleaseCollection(ctx, milvusclient.NewReleaseCollectionOption("custom_quick_setup"))
if err != nil {
// handle error
}
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": "custom_quick_setup"
}'
# {
# "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": "custom_quick_setup"
}'
# {
# "code": 0,
# "data": {
# "loadProgress": 0,
# "loadState": "LoadStateNotLoaded",
# "message": ""
# }
# }