修改 Collection
您可以重命名 collection 或修改其设置。本页面重点介绍如何修改 collection。
重命名 Collection
您可以按以下方式重命名 collection。
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" \
-d '{
"collectionName": "my_collection",
"newCollectionName": "my_new_collection"
}'
设置 Collection 属性
以下代码片段演示了如何设置 collection TTL。
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" \
-d '{
"collectionName": "test_collection",
"properties": {
"collection.ttl.seconds": 60
}
}'
适用的 collection 属性如下:
属性 | 使用场景 |
|---|---|
| 如果需要在特定时间段后删除 collection 中的数据,请考虑设置其生存时间(TTL),单位为秒。TTL 过期后,Milvus 会从 collection 中删除所有实体。删除操作是异步的,这意味着在删除完成之前,搜索和查询仍然是可能的。有关详细信息,请参阅设置 Collection TTL。 |
| 内存映射(Mmap)允许直接内存访问磁盘上的大文件,使 Milvus 能够将索引和数据存储在内存和硬盘中。这种方法有助于根据访问频率优化数据放置策略,在不影响搜索性能的情况下扩展 collection 的存储容量。 有关详细信息,请参阅使用 mmap。 |
| 启用 Partition Key 隔离后,Milvus 根据 Partition Key 值对实体进行分组,并为每个组创建单独的索引。接收到搜索请求时,Milvus 根据过滤条件中指定的 Partition Key 值定位索引,并将搜索范围限制在索引包含的实体内,从而避免在搜索过程中扫描不相关的实体,大大提高搜索性能。有关详细信息,请参阅使用 Partition Key 隔离。 |
删除 Collection 属性
您也可以通过删除 collection 属性来重置它,如下所示。
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" \
-d '{
"collectionName": "my_collection",
"propertyKeys": [
"collection.ttl.seconds"
]
}'