メインコンテンツまでスキップ
バージョン: User Guides (Cloud)

プライマリフィールドと AutoID

Zilliz Cloud のすべてのコレクションには、各エンティティを一意に識別するためのプライマリフィールドが必要です。このフィールドにより、各エンティティを曖昧さなく挿入・更新・照会・削除できます。

ユースケースに応じて、Zilliz Cloud に自動的に ID を生成させる(AutoID)か、自分で ID を手動で割り当てるかを選択できます。

プライマリフィールドとは?

プライマリフィールドは、コレクション内の各エンティティを一意に識別するキーとして機能し、従来のデータベースにおける主キー(Primary キー)と同様の役割を果たします。Zilliz Cloud は、エンティティの挿入、アップサート、削除、および照会操作中にプライマリフィールドを使用してエンティティを管理します。

主な要件:

  • 各コレクションにはちょうど1つのプライマリフィールドが必要です。

  • プライマリフィールドの値は null にできません。

  • データ型は作成時に指定する必要があり、後から変更することはできません。

サポートされているデータ型

プライマリフィールドには、エンティティを一意に識別できるスカラー型のデータ型を使用する必要があります。

データ型

説明

INT64

64ビット整数型。AutoID と組み合わせてよく使用されます。ほとんどのユースケースで推奨されるオプションです。

VARCHAR

可変長文字列型。エンティティ識別子が外部システム(例:商品コードやユーザーID)から提供される場合に使用します。max_length プロパティを設定して、各値に許容される最大バイト数を定義する必要があります。

AutoID と手動 ID の選択

Zilliz Cloud では、プライマリキーの値を割り当てる方法として2つのモードをサポートしています。

モード

説明

推奨用途

AutoID

Zilliz Cloud が挿入またはインポートされたエンティティに対して自動的に一意の識別子を生成します。

ID を手動で管理する必要がないほとんどのシナリオ。

手動 ID

データの挿入またはインポート時に、自分で一意の ID を提供します。

ID を外部システムや既存のデータセットと整合させる必要がある場合。

📘Notes
  • どちらのモードを選べばよいかわからない場合は、AutoID から始めて、シンプルなデータ取り込みと一意性の保証を活用してください。

  • 手動でプライマリキーを設定することが有益でない限り、すべてのケースで autoId を使用することをお勧めします。

クイックスタート: AutoID の使用

Zilliz Cloud に ID の自動生成を任せることができます。

ステップ 1: AutoID を有効にしてコレクションを作成

プライマリフィールド定義で auto_id=True を有効にします。これにより、Zilliz Cloud が自動的に ID を生成します。

from pymilvus import MilvusClient, DataType

client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")

schema = client.create_schema()

# Define primary field with AutoID enabled
schema.add_field(
field_name="id", # Primary field name
is_primary=True,
auto_id=True, # Milvus generates IDs automatically; Defaults to False
datatype=DataType.INT64
)

# Define the other fields
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=4) # Vector field
schema.add_field(field_name="category", datatype=DataType.VARCHAR, max_length=1000) # Scalar field of the VARCHAR type

# Create the collection
if client.has_collection("demo_autoid"):
client.drop_collection("demo_autoid")
client.create_collection(collection_name="demo_autoid", schema=schema)

Step 2: データの挿入

重要: データに主キー列を含めないでください。Zilliz Cloud は ID を自動的に生成します。

data = [
{"embedding": [0.1, 0.2, 0.3, 0.4], "category": "book"},
{"embedding": [0.2, 0.3, 0.4, 0.5], "category": "toy"},
]

res = client.insert(collection_name="demo_autoid", data=data)
print("Generated IDs:", res.get("ids"))

# Output example:
# Generated IDs: [461526052788333649, 461526052788333650]
📘Notes

既存のエンティティを扱う際は、重複IDエラーを回避するために insert() の代わりに upsert() を使用してください。

手動IDを使用する

IDを手動で制御する必要がある場合は、AutoIDを無効にして独自の値を指定します。

ステップ 1: AutoIDなしでコレクションを作成する

from pymilvus import MilvusClient, DataType

client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")

schema = client.create_schema()

# Define the primary field without AutoID
schema.add_field(
field_name="product_id",
is_primary=True,
auto_id=False, # You'll provide IDs manually at data ingestion
datatype=DataType.VARCHAR,
max_length=100 # Required when datatype is VARCHAR
)

# Define the other fields
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=4) # Vector field
schema.add_field(field_name="category", datatype=DataType.VARCHAR, max_length=1000) # Scalar field of the VARCHAR type

# Create the collection
if client.has_collection("demo_manual_ids"):
client.drop_collection("demo_manual_ids")
client.create_collection(collection_name="demo_manual_ids", schema=schema)

ステップ 2: 自分の ID を使ってデータを挿入する

すべての insert 操作には、主キー(primary field)カラムを含める必要があります。

# Each entity must contain the primary field `product_id`
data = [
{"product_id": "PROD-001", "embedding": [0.1, 0.2, 0.3, 0.4], "category": "book"},
{"product_id": "PROD-002", "embedding": [0.2, 0.3, 0.4, 0.5], "category": "toy"},
]

res = client.insert(collection_name="demo_manual_ids", data=data)
print("Generated IDs:", res.get("ids"))

# Output example:
# Generated IDs: ['PROD-001', 'PROD-002']

あなたの責任:

  • すべてのエンティティ間で ID が一意になるようにすること

  • 挿入/インポート操作ごとに主キーを含めること

  • ID の競合および重複検出を自分で処理すること