Memodifikasi Koleksi
Anda dapat mengganti nama koleksi atau mengubah pengaturannya. Halaman ini berfokus pada cara mengubah koleksi.
Mengganti Nama Koleksi
Anda dapat mengganti nama koleksi sebagai berikut.
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
client.rename_collection(
old_name="my_collection",
new_name="my_new_collection"
)
import io.milvus.v2.service.collection.request.RenameCollectionReq;
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);
RenameCollectionReq renameCollectionReq = RenameCollectionReq.builder()
.collectionName("my_collection")
.newCollectionName("my_new_collection")
.build();
client.renameCollection(renameCollectionReq);
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "http://localhost:19530";
const token = "root:Milvus";
const client = new MilvusClient({address, token});
const res = await client.renameCollection({
oldName: "my_collection",
newName: "my_new_collection"
});
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "localhost:19530"
token := "root:Milvus"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
err = client.RenameCollection(ctx, milvusclient.NewRenameCollectionOption("my_collection", "my_new_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/rename" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"newCollectionName": "my_new_collection"
}'
Mengatur Properti Koleksi
Anda dapat memodifikasi properti tingkat koleksi setelah koleksi dibuat.
Semua properti yang tercantum di bagian ini hanya berlaku untuk koleksi yang dikelola.
Properti yang didukung
Properti |
Deskripsi |
|---|---|
|
Jika data koleksi perlu dihapus setelah periode tertentu, pertimbangkan untuk mengatur Time-To-Live (TTL) dalam hitungan detik. Setelah TTL habis, Milvus akan menghapus semua entitas dari koleksi. Penghapusan ini bersifat asinkron, yang mengindikasikan bahwa pencarian dan kueri masih dapat dilakukan sebelum penghapusan selesai. Untuk detailnya, lihat Mengatur TTL Koleksi. |
|
Pemetaan memori (Mmap) memungkinkan akses memori langsung ke berkas besar pada disk, sehingga Milvus dapat menyimpan indeks dan data dalam memori dan hard drive. Pendekatan ini membantu mengoptimalkan kebijakan penempatan data berdasarkan frekuensi akses, memperluas kapasitas penyimpanan untuk koleksi tanpa memengaruhi kinerja pencarian. Untuk detailnya, lihat Gunakan mmap. |
|
Dengan Isolasi Kunci Partisi diaktifkan, Milvus mengelompokkan entitas berdasarkan nilai Kunci Partisi dan membuat indeks terpisah untuk masing-masing kelompok ini. Setelah menerima permintaan pencarian, Milvus mencari indeks berdasarkan nilai Kunci Partisi yang ditentukan dalam kondisi pemfilteran dan membatasi cakupan pencarian di dalam entitas yang termasuk dalam indeks, sehingga menghindari pemindaian entitas yang tidak relevan selama pencarian dan meningkatkan kinerja pencarian. Untuk detailnya, lihat Menggunakan Isolasi Kunci Partisi. |
|
Mengaktifkan bidang dinamis untuk koleksi yang dibuat tanpa mengaktifkannya. Setelah diaktifkan, Anda dapat menyisipkan entitas dengan bidang yang tidak ditentukan dalam skema asli. Untuk detailnya, lihat Bidang Dinamis. |
|
Apakah mengizinkan koleksi untuk menerima nilai kunci utama yang disediakan pengguna ketika AutoID telah diaktifkan untuk koleksi tersebut.
|
|
Menentukan zona waktu default untuk koleksi ini saat menangani operasi yang sensitif terhadap waktu, terutama bidang |
Contoh 1: Mengatur TTL tingkat koleksi
Cuplikan kode berikut ini menunjukkan cara menyetel TTL koleksi.
from pymilvus import MilvusClient
client.alter_collection_properties(
collection_name="my_collection",
properties={"collection.ttl.seconds": 60}
)
import io.milvus.param.Constant;
import io.milvus.v2.service.collection.request.AlterCollectionPropertiesReq;
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property(Constant.TTL_SECONDS, "60")
.build();
client.alterCollectionProperties(alterCollectionReq);
res = await client.alterCollection({
collection_name: "my_collection",
properties: {
"collection.ttl.seconds": 60
}
})
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.CollectionTTLConfigKey, 60))
if err != nil {
fmt.Println(err.Error())
// handle error
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"properties": {
"collection.ttl.seconds": 60
}
}'
Contoh 2: Tetapkan TTL tingkat entitasCompatible with Milvus 3.0.x
Cuplikan kode berikut ini menunjuk bidang TIMESTAMPTZ yang sudah ada (expire_at) sebagai bidang TTL untuk TTL tingkat entitas. Koleksi harus sudah berisi bidang TIMESTAMPTZ dengan nama tersebut, dan collection.ttl.seconds tidak boleh disetel - kedua mode TTL tersebut saling terpisah.
Untuk alur kerja TTL tingkat entitas secara lengkap (penyiapan skema, penyisipan, kueri, penyegaran, pelepasan), lihat Mengatur TTL tingkat entitas.
from pymilvus import MilvusClient
client.alter_collection_properties(
collection_name="my_collection",
properties={"ttl_field": "expire_at"}
)
// java
// nodejs
// go
# restful
Contoh 3: Mengaktifkan mmap
Cuplikan kode berikut ini menunjukkan cara mengaktifkan mmap.
from pymilvus import MilvusClient
client.alter_collection_properties(
collection_name="my_collection",
properties={"mmap.enabled": True}
)
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property(Constant.MMAP_ENABLED, "True")
.build();
client.alterCollectionProperties(alterCollectionReq);
await client.alterCollectionProperties({
collection_name: "my_collection",
properties: {
"mmap.enabled": true
}
});
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.MmapEnabledKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
curl -X POST "http://localhost:19530/v2/vectordb/collections/alter_properties" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"properties": {
"mmap.enabled": "true"
}
}'
Contoh 4: Mengaktifkan kunci partisi
Potongan kode berikut menunjukkan cara mengaktifkan kunci partisi.
from pymilvus import MilvusClient
client.alter_collection_properties(
collection_name="my_collection",
properties={"partitionkey.isolation": True}
)
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property("partitionkey.isolation", "True")
.build();
client.alterCollectionProperties(alterCollectionReq);
await client.alterCollectionProperties({
collection_name: "my_collection",
properties: {
"partitionkey.isolation": true
}
});
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.PartitionKeyIsolationKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
curl -X POST "http://localhost:19530/v2/vectordb/collections/alter_properties" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "my_collection",
"properties": {
"partitionkey.isolation": "true"
}
}'
Contoh 5: Mengaktifkan bidang dinamis
Cuplikan kode berikut ini menunjukkan cara mengaktifkan bidang dinamis.
from pymilvus import MilvusClient
client.alter_collection_properties(
collection_name="my_collection",
properties={"dynamicfield.enabled": True}
)
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property("dynamicfield.enabled", "True")
.build();
client.alterCollectionProperties(alterCollectionReq);
await client.alterCollectionProperties({
collection_name: "my_collection",
properties: {
"dynamicfield.enabled": true
}
});
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.EnableDynamicSchemaKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
curl -X POST "http://localhost:19530/v2/vectordb/collections/alter_properties" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "my_collection",
"properties": {
"dynamicfield.enabled": "true"
}
}'
Contoh 6: Mengaktifkan allow_insert_auto_id
Properti allow_insert_auto_id memungkinkan koleksi dengan AutoID diaktifkan untuk menerima nilai kunci utama yang disediakan pengguna selama penyisipan, peng-update-an, dan impor massal. Ketika diatur ke "true", Milvus menggunakan nilai kunci utama yang disediakan pengguna jika ada; jika tidak, maka akan dibuat secara otomatis. Nilai defaultnya adalah "false".
Contoh di bawah ini menunjukkan cara mengaktifkan allow_insert_auto_id:
client.alter_collection_properties(
collection_name="my_collection",
properties={"allow_insert_auto_id": "true"}
)
# After enabling, inserts with a PK column will use that PK; otherwise Milvus auto-generates.
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property("allow_insert_auto_id", "True")
.build();
client.alterCollectionProperties(alterCollectionReq);
await client.alterCollectionProperties({
collection_name: "my_collection",
properties: {
"allow_insert_auto_id": true
}
});
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.AllowInsertAutoIDKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
curl -X POST "http://localhost:19530/v2/vectordb/collections/alter_properties" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "my_collection",
"properties": {
"allow_insert_auto_id": "true"
}
}'
Contoh 7: Mengatur zona waktu pengumpulan
Anda dapat mengatur zona waktu default untuk koleksi Anda menggunakan properti timezone. Hal ini menentukan bagaimana data yang terkait dengan waktu ditafsirkan dan ditampilkan untuk semua operasi di dalam koleksi, termasuk penyisipan data, kueri, dan presentasi hasil.
Nilai timezone harus berupa pengenal zona waktu IANA yang valid, seperti Asia/Shanghai, America/Chicago, atau UTC. Menggunakan nilai yang tidak valid atau tidak standar akan mengakibatkan kesalahan saat memodifikasi properti koleksi.
Contoh di bawah ini menunjukkan cara menyetel zona waktu koleksi ke Asia/Shanghai:
client.alter_collection_properties(
collection_name="my_collection",
properties={"timezone": "Asia/Shanghai"}
)
AlterCollectionPropertiesReq alterCollectionReq = AlterCollectionPropertiesReq.builder()
.collectionName("my_collection")
.property("timezone", "Asia/Shanghai")
.build();
client.alterCollectionProperties(alterCollectionReq);
// js
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").WithProperty(common.CollectionDefaultTimezone, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
curl -X POST "http://localhost:19530/v2/vectordb/collections/alter_properties" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "my_collection",
"properties": {
"timezone": "Asia/Shanghai"
}
}'
Menghapus Properti Koleksi
Anda juga dapat mengatur ulang properti koleksi dengan cara menjatuhkannya sebagai berikut.
client.drop_collection_properties(
collection_name="my_collection",
property_keys=[
"collection.ttl.seconds"
]
)
client.dropCollectionProperties(DropCollectionPropertiesReq.builder()
.collectionName("my_collection")
.propertyKeys(Collections.singletonList("collection.ttl.seconds"))
.build());
client.dropCollectionProperties({
collection_name:"my_collection",
properties: ['collection.ttl.seconds'],
});
err = client.DropCollectionProperties(ctx, milvusclient.NewDropCollectionPropertiesOption("my_collection", common.CollectionTTLConfigKey))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/drop_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"propertyKeys": [
"collection.ttl.seconds"
]
}'