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.

Properti yang didukung

Properti

Deskripsi

collection.ttl.seconds

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.

mmap.enabled

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.

partitionkey.isolation

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.

dynamicfield.enabled

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.

allow_insert_auto_id

Apakah mengizinkan koleksi untuk menerima nilai kunci utama yang disediakan pengguna ketika AutoID telah diaktifkan untuk koleksi tersebut.

  • Bila diatur ke "true": Sisipan, peng-update-an, dan impor massal menggunakan kunci utama yang disediakan pengguna jika ada; jika tidak, nilai kunci utama dibuat secara otomatis.

  • Bila diatur ke "false": Nilai kunci utama yang disediakan pengguna ditolak atau diabaikan dan nilai kunci utama selalu dibuat secara otomatis. Nilai defaultnya adalah "false".

timezone

Menentukan zona waktu default untuk koleksi ini saat menangani operasi yang sensitif terhadap waktu, terutama bidang TIMESTAMPTZ. Stempel waktu disimpan secara internal dalam UTC, dan Milvus mengonversi nilai untuk tampilan dan perbandingan menurut pengaturan ini. Jika disetel, zona waktu koleksi akan menggantikan zona waktu default basis data; parameter zona waktu kueri dapat menggantikan keduanya untuk sementara. Nilainya harus berupa pengenal zona waktu IANA yang valid (misalnya, Asia/Shanghai, Amerika/Chicago, atau UTC). Untuk detail tentang cara menggunakan bidang TIMESTAMPTZ, lihat Bidang TIMESTAMPTZ.

Contoh 1: Mengatur koleksi TTL

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.v2.service.collection.request.AlterCollectionReq;
import java.util.HashMap;
import java.util.Map;

Map<String, String> properties = new HashMap<>();
properties.put("collection.ttl.seconds", "60");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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": "test_collection",
    "properties": {
        "collection.ttl.seconds": 60
    }
}'

Contoh 2: Mengaktifkan mmap

Potongan kode berikut menunjukkan cara mengaktifkan mmap.

from pymilvus import MilvusClient

client.alter_collection_properties(
    collection_name="my_collection",
    properties={"mmap.enabled": True}
)
Map<String, String> properties = new HashMap<>();
properties.put("mmap.enabled", "True");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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 3: 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}
)
Map<String, String> properties = new HashMap<>();
properties.put("partitionkey.isolation", "True");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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 4: Mengaktifkan bidang dinamis

Potongan kode berikut ini menunjukkan cara mengaktifkan bidang dinamis.

from pymilvus import MilvusClient

client.alter_collection_properties(
    collection_name="my_collection",
    properties={"dynamicfield.enabled": True}
)
Map<String, String> properties = new HashMap<>();
properties.put("dynamicfield.enabled", "True");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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 5: 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.
Map<String, String> properties = new HashMap<>();
properties.put("allow_insert_auto_id", "True");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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 6: 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"}
)
Map<String, String> properties = new HashMap<>();
properties.put("timezone", "Asia/Shanghai");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
        .collectionName("my_collection")
        .properties(properties)
        .build();

client.alterCollection(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"
    ]
}'