跳到主要内容

加载和释放

加载 collection 是在 collection 中进行相似性搜索和查询的前提。本页重点介绍加载和释放 collection 的流程。

加载 Collection

当您加载 collection 时,Milvus 会将所有 field 的 index 文件和原始数据加载到内存中,以便快速响应搜索和查询。在 collection 加载后插入的 entity 会自动建立 index 并加载。

以下代码片段演示如何加载 collection。

from pymilvus import MilvusClient

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

# 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 = "http://localhost:19530";
String TOKEN = "root:Milvus";

// 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 = "http://localhost:19530";
const token = "root:Milvus";
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 := "localhost:19530"
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="http://localhost:19530"
export TOKEN="root:Milvus"

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": ""
# }
# }

加载特定 Field

Milvus 可以仅加载搜索和查询中涉及的 field,从而减少内存使用并提高搜索性能。

部分 collection 加载目前处于 beta 阶段,不建议在生产环境中使用。

以下代码片段假设您已经创建了一个名为 my_collection 的 collection,并且 collection 中有两个名为 my_idmy_vector 的 field。

client.load_collection(
collection_name="my_collection",
load_fields=["my_id", "my_vector"], # 仅加载指定的 field
skip_load_dynamic_field=True # 跳过加载动态 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"))
.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"], // 仅加载指定的 field
skip_load_dynamic_field: true //跳过加载动态 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 support yet

如果您选择加载特定 field,值得注意的是,只有包含在 load_fields 中的 field 才能在搜索和查询中用作过滤器和输出 field。您应该始终在 load_fields 中包含 primary field 的名称和至少一个向量 field。

您还可以使用 skip_load_dynamic_field 来确定是否加载动态 field。动态 field 是一个名为 $meta 的保留 JSON field,以键值对的形式保存所有非 schema 定义的 field 及其值。加载动态 field 时,field 中的所有键都会被加载并可用于过滤和输出。如果动态 field 中的所有键都不涉及元数据过滤和输出,请将 skip_load_dynamic_field 设置为 True

要在 collection 加载后加载更多 field,您需要先释放 collection,以避免因 index 更改而可能出现的错误。

释放 Collection

搜索和查询是内存密集型操作。为了节省成本,建议您释放当前未使用的 collection。

以下代码片段演示如何释放 collection。

# 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="http://localhost:19530"
export TOKEN="root:Milvus"

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": ""
# }
# }