Memuat & Melepaskan
Memuat koleksi merupakan prasyarat untuk melakukan pencarian dan kueri kesamaan dalam koleksi. Halaman ini berfokus pada prosedur untuk memuat dan melepaskan koleksi.
Memuat Koleksi
Ketika Anda memuat koleksi, Milvus memuat file indeks dan data mentah dari semua bidang ke dalam memori untuk respons cepat terhadap pencarian dan kueri. Entitas yang dimasukkan setelah koleksi dimuat akan diindeks dan dimuat secara otomatis.
Cuplikan kode berikut ini menunjukkan cara memuat koleksi.
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
# 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 = "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("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 = "http://localhost:19530";
const token = "root:Milvus";
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"
)
defer cli.Close(ctx)
loadTask, err := cli.LoadCollection(ctx, client.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="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": "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": ""
# }
# }
Memuat Bidang Tertentu
Milvus hanya dapat memuat bidang yang terlibat dalam pencarian dan kueri, sehingga mengurangi penggunaan memori dan meningkatkan kinerja pencarian.
Pemuatan sebagian koleksi saat ini masih dalam versi beta dan tidak direkomendasikan untuk penggunaan produksi.
Cuplikan kode berikut ini mengasumsikan bahwa Anda telah membuat koleksi bernama customized_setup_2, dan ada dua field bernama my_id dan my_vector di dalam koleksi tersebut.
client.load_collection(
collection_name="customized_setup_1",
# 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="customized_setup_1"
)
print(res)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
// 6. Load the collection
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("customized_setup_1")
.loadFields(Arrays.asList("my_id", "my_vector"))
.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);
await client.load_collection({
collection_name: "customized_setup_1",
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: "customized_setup_1",
})
console.log(loadState);
import (
"context"
"fmt"
"log"
"github.com/milvus-io/milvus/client/v2"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
loadTask, err := cli.LoadCollection(ctx, client.NewLoadCollectionOption("customized_setup_1").
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 缺失
Jika Anda memilih untuk memuat bidang tertentu, perlu dicatat bahwa hanya bidang yang disertakan dalam load_fields
yang dapat digunakan sebagai filter dan bidang keluaran dalam pencarian dan kueri. Anda harus selalu menyertakan nama bidang utama dan setidaknya satu bidang vektor di load_fields
.
Anda juga dapat menggunakan skip_load_dynamic_field
untuk menentukan apakah akan memuat bidang dinamis. Bidang dinamis adalah bidang JSON yang dicadangkan bernama $meta dan menyimpan semua bidang yang tidak ditentukan skema dan nilainya dalam pasangan kunci-nilai. Saat memuat bidang dinamis, semua kunci dalam bidang dimuat dan tersedia untuk pemfilteran dan keluaran. Jika semua kunci dalam bidang dinamis tidak terlibat dalam pemfilteran dan keluaran metadata, setel skip_load_dynamic_field
ke True
.
Untuk memuat lebih banyak bidang setelah koleksi dimuat, Anda harus melepaskan koleksi terlebih dahulu untuk menghindari kemungkinan kesalahan yang disebabkan oleh perubahan indeks.
Melepaskan Koleksi
Pencarian dan kueri adalah operasi yang memakan banyak memori. Untuk menghemat biaya, Anda disarankan untuk melepaskan koleksi yang sedang tidak digunakan.
Cuplikan kode berikut ini menunjukkan cara melepaskan koleksi.
# 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"
)
err := cli.ReleaseCollection(ctx, client.NewReleaseCollectionOption("custom_quick_setup"))
if err != nil {
// handle error
}
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": "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": ""
# }
# }