コレクションの変更

コレクションの名前を変更したり、設定を変更したりできます。このページでは、コレクションを変更する方法を説明します。

コレクションの名前の変更

コレクション名は以下のように変更できます。

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"
}'

コレクションプロパティの設定

コレクションが作成された後、コレクションレベルのプロパティを変更できます。

このセクションに記載されているすべてのプロパティは、管理されたコレクションにのみ適用されます。

サポートされるプロパティ

プロパティ

説明

collection.ttl.seconds

特定の期間後にコレクションのデータを削除する必要がある場合は、TTL(Time-To-Live)を秒単位で設定することを検討してください。TTLがタイムアウトすると、Milvusはコレクションからすべてのエンティティを削除します。

削除は非同期で行われるため、削除が完了するまで検索やクエリが可能です。

詳細については、Set Collection TTLを参照してください。

mmap.enabled

メモリマッピング(Mmap)はディスク上の大きなファイルへの直接メモリアクセスを可能にし、Milvusがメモリとハードディスクの両方にインデックスとデータを格納することを可能にします。このアプローチは、アクセス頻度に基づいてデータ配置ポリシーを最適化し、検索パフォーマンスに影響を与えることなくコレクションのストレージ容量を拡張するのに役立ちます。

詳細については、mmapの使用を参照してください。

partitionkey.isolation

パーティションキーの分離を有効にすると、Milvusはパーティションキーの値に基づいてエンティティをグループ化し、これらのグループごとに個別のインデックスを作成します。検索要求を受信すると、Milvusはフィルタリング条件に指定されたパーティションキー値に基づいてインデックスを検索し、インデックスに含まれるエンティティ内で検索範囲を制限する。

詳細については、「パーティション・キーの分離を使用する」を参照してください。

dynamicfield.enabled

ダイナミックフィールドを有効にする ダイナミックフィールドを有効にせずに作成されたコレクションに対して、ダイナミックフィールドを有効にします。有効にすると、元のスキーマで定義されていないフィールドを持つエンティティを挿入できます。詳細は、"ダイナミック・フィールド" を参照してください。

allow_insert_auto_id

AutoID がコレクションで有効になっている場合に、コレクションがユーザ提供の主キー値を受け入れることを許可するかどうか。

  • true "に設定した場合:そうでない場合、プライマリ・キー値は自動生成されます。

  • false "に設定した場合:ユーザーが提供した主キー値は拒否または無視され、主キー値は常に自動生成されます。デフォルトは"false "です。

timezone

時間の影響を受けやすい操作、特にTIMESTAMPTZ フィールドを処理するときに、このコレクションのデフォルトのタイムゾーンを指定します。タイムスタンプは内部的にUTCで保存され、Milvusはこの設定に従って表示と比較のために値を変換します。設定された場合、コレクションのタイムゾーンはデータベースのデフォルトタイムゾーンを上書きします。クエリのタイムゾーンパラメータは一時的に両方を上書きすることができます。この値は有効なIANAタイムゾーン識別子でなければなりません(例えば、Asia/ShanghaiAmerica/Chicago、またはUTC)。TIMESTAMPTZ フィールドの使用方法の詳細については、TIMESTAMPTZ フィールドを参照のこと。

例1:コレクションレベルのTTLを設定する

以下のコード・スニペットは、コレクションTTLを設定する方法を示している。

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
    }
}'

例2:エンティティレベルのTTLの設定Compatible with Milvus 3.0.x

以下のコードスニペットは、既存のTIMESTAMPTZ フィールド(expire_at)をエンティティレベルTTLのTTLフィールドとして指定する。コレクションには、すでにその名前を持つTIMESTAMPTZ フィールドが含まれていなければならない。また、collection.ttl.seconds が設定されていてはならない。

完全なエンティティレベルTTLワークフロー(スキーマのセットアップ、挿入、クエリ、リフレッシュ、ドロップ)については、「エンティティレベルTTLを設定する」を参照してください。

from pymilvus import MilvusClient

client.alter_collection_properties(
    collection_name="my_collection",
    properties={"ttl_field": "expire_at"}
)
// java
// nodejs
// go
# restful

例 3: mmap を有効にする

以下のコード・スニペットは、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"
    }
  }'

例 4:パーティション・キーを有効にする

以下のコード・スニペットは、パーティション・キーを有効にする方法を示しています。

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"
    }
  }'

例 5:ダイナミック・フィールドを有効にする

以下のコード・スニペットは、ダイナミック・フィールドを有効にする方法を示しています。

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"
    }
  }'

例 6: allow_insert_auto_id を有効にする

allow_insert_auto_id プロパティは、AutoID が有効なコレクションが、挿入、upsert、および一括インポートの際にユーザ提供の主キー値を受け入れることを許可します。true "に設定すると、Milvusはユーザから提供された主キー値が存在する場合はそれを使用し、存在しない場合は自動生成します。デフォルトは"false "です。

以下の例では、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"
    }
  }'

例7:コレクションタイムゾーンの設定

timezone プロパティを使用して、コレクションにデフォルトのタイムゾーンを設定できます。これにより、データの挿入、クエリ、結果の表示など、コレクション内のすべての操作で、時間に関連するデータがどのように解釈され、表示されるかが決まります。

timezone の値には、Asia/ShanghaiAmerica/ChicagoUTC などの有効なIANA タイムゾーン識別子を指定する必要があります。無効な値や非標準の値を使用すると、コレクション・プロパティを変更するときにエラーが発生します。

以下の例は、コレクションのタイムゾーンを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"
    }
  }'

コレクション・プロパティの削除

コレクション・プロパティをドロップしてリセットすることもできます。

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"
    ]
}'