既存のコレクションにフィールドを追加
Milvusでは、既存のコレクションに新しいフィールドを動的に追加することができ、アプリケーションのニーズに応じてデータスキーマを容易に変更できます。このガイドでは、実際の例を使用してさまざまなシナリオでフィールドを追加する方法を説明します。
考慮事項
コレクションにフィールドを追加する前に、以下の重要な点を考慮してください:
-
スカラーフィールド(
INT64、VARCHAR、FLOAT、DOUBLEなど)を追加できます。ベクトルフィールドは既存のコレクションに追加することはできません。 -
新しいフィールドには新しいフィールドの値を持っていない既存のエンティティに対応するため、nullable(nullable=True)である必要があります。
-
読み込まれたコレクションにフィールドを追加すると、メモリ使用量が増加します。
-
1つのコレクションあたりのフィールド数には最大制限があります。詳細はMilvus Limitsを参照してください。
-
フィールド名は静的フィールド内で一意である必要があります。
-
元から
enable_dynamic_field=Trueで作成されていないコレクションに、動的フィールド機能を有効にするために$metaフィールドを追加することはできません。
前提条件
このガイドでは、以下があることを前提としています:
-
実行中のMilvusインスタンス
-
Milvus SDKがインストールされている
-
既存のコレクション
コレクションの作成と基本操作については、Create Collectionを参照してください。
基本的な使用方法
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient, DataType
# Milvusサーバーに接続
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT" # MilvusサーバーURIに置き換えてください
)
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.client.ConnectConfig;
ConnectConfig config = ConnectConfig.builder()
.uri("YOUR_CLUSTER_ENDPOINT")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
const milvusClient = new MilvusClient({
address: 'YOUR_CLUSTER_ENDPOINT'
});
// go
# restful
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
シナリオ1:nullableフィールドを素早く追加
コレクションを拡張する最も簡単な方法は、nullableフィールドを追加することです。これはデータに新しい属性を素早く追加する必要がある場合に最適です。
- Python
- Java
- NodeJS
- Go
- cURL
# 既存のコレクションにnullableフィールドを追加
# この操作:
# - 即座に返る(非ブロッキング)
# - 最小限の遅延でフィールドを使用可能にする
# - 既存のすべてのエンティティにNULLを設定
client.add_collection_field(
collection_name="product_catalog",
field_name="created_timestamp", # 追加する新しいフィールドの名前
data_type=DataType.INT64, # データ型はスカラー型である必要があります
nullable=True # 追加されたフィールドに対してTrueでなければなりません
# 既存のエンティティにNULL値を許可
)
import io.milvus.v2.service.collection.request.AddCollectionFieldReq;
client.addCollectionField(AddCollectionFieldReq.builder()
.collectionName("product_catalog")
.fieldName("created_timestamp")
.dataType(DataType.Int64)
.isNullable(true)
.build());
await client.addCollectionField({
collection_name: 'product_catalog',
field: {
name: 'created_timestamp',
dataType: 'Int64',
nullable: true
}
});
// go
# restful
curl -X POST "YOUR_CLUSTER_ENDPOINT/v2/vectordb/collections/fields/add" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "product_catalog",
"schema": {
"fieldName": "created_timestamp",
"dataType": "Int64",
"nullable": true
}
}'
期待される動作:
-
既存のエンティティは新しいフィールドにNULLを持つ
-
新しいエンティティはNULLまたは実際の値を持つことができる
-
フィールドの使用可能性は内部スキーマ同期による最小限の遅延でほぼ即座に発生する
-
クエリ可能なのは短い同期期間の後ですぐに
- Python
- Java
- NodeJS
- Go
- cURL
# クエリ結果の例
{
'id': 1,
'created_timestamp': None # 既存のエンティティの新しいフィールドにNULLを表示
}
// java
// nodejs
{
'id': 1,
'created_timestamp': None # 既存のエンティティの新しいフィールドにNULLを表示
}
// go
# restful
{
"code": 0,
"data": {},
"cost": 0
}
シナリオ2:デフォルト値を持つフィールドを追加
既存のエンティティにNULLではなく意味のある初期値を設定したい場合は、デフォルト値を指定します。
- Python
- Java
- NodeJS
- Go
- cURL
# デフォルト値を持つフィールドを追加
# この操作:
# - 既存のすべてのエンティティにデフォルト値を設定
# - 最小限の遅延でフィールドを使用可能にする
# - デフォルト値によるデータ整合性を維持
client.add_collection_field(
collection_name="product_catalog",
field_name="priority_level", # 新しいフィールドの名前
data_type=DataType.VARCHAR, # 文字列型フィールド
max_length=20, # 最大文字列長
nullable=True, # 追加されたフィールドに必須
default_value="standard" # 既存のエンティティに割り当てられる値
# 値が提供されない場合の新しいエンティティにも使用
)
client.addCollectionField(AddCollectionFieldReq.builder()
.collectionName("product_catalog")
.fieldName("priority_level")
.dataType(DataType.VarChar)
.maxLength(20)
.isNullable(true)
.build());
await client.addCollectionField({
collection_name: 'product_catalog',
field: {
name: 'priority_level',
dataType: 'VarChar',
nullable: true,
default_value: 'standard',
}
});
// go
# restful
curl -X POST "YOUR_CLUSTER_ENDPOINT/v2/vectordb/collections/fields/add" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "product_catalog",
"schema": {
"fieldName": "priority_level",
"dataType": "VarChar",
"nullable": true,
"defaultValue": "standard",
"elementTypeParams": {
"max_length": "20"
}
}
}'
期待される動作:
-
既存のエンティティは新しく追加されたフィールドにデフォルト値(
"standard")を持つ -
新しいエンティティはデフォルト値を上書きするか、値が提供されない場合はそれを使用できます
-
フィールドの使用可能性は内部スキーマ同期による最小限の遅延でほぼ即座に発生する
-
クエリ可能なのは短い同期期間の後ですぐに
- Python
- Java
- NodeJS
- Go
- cURL
# クエリ結果の例
{
'id': 1,
'priority_level': 'standard' # 既存のエンティティにデフォルト値を表示
}
// java
{
'id': 1,
'priority_level': 'standard' # 既存のエンティティにデフォルト値を表示
}
// go
# restful
{
'id': 1,
'priority_level': 'standard' # 既存のエンティティにデフォルト値を表示
}
FAQ
$metaフィールドを追加して動的スキーマ機能を有効にすることはできますか?
いいえ、add_collection_fieldを使用して$metaフィールドを追加して動的フィールド機能を有効にすることはできません。たとえば、以下のコードは機能しません。
- Python
- Java
- NodeJS
- Go
- cURL
# ❌ サポートされていません
client.add_collection_field(
collection_name="existing_collection",
field_name="$meta",
data_type=DataType.JSON # この操作は失敗します
)
// ❌ サポートされていません
client.addCollectionField(AddCollectionFieldReq.builder()
.collectionName("existing_collection")
.fieldName("$meta")
.dataType(DataType.JSON)
.build());
// ❌ サポートされていません
await client.addCollectionField({
collection_name: 'product_catalog',
field: {
name: '$meta',
dataType: 'JSON',
}
});
// go
# restful
# ❌ サポートされていません
curl -X POST "YOUR_CLUSTER_ENDPOINT/v2/vectordb/collections/fields/add" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{
"collectionName": "existing_collection",
"schema": {
"fieldName": "$meta",
"dataType": "JSON",
"nullable": true
}
}'
動的スキーマ機能を有効にするには:
-
新しいコレクション:コレクション作成時に
enable_dynamic_fieldをTrueに設定します。詳細についてはCreate Collectionを参照してください。 -
既存のコレクション:コレクションレベルのプロパティ
dynamicfield.enabledをTrueに設定します。詳細についてはModify Collectionを参照してください。
同じ名前の動的フィールドキーを持つフィールドを追加するとどうなりますか?
コレクションに動的フィールドが有効($metaが存在)になっている場合、既存の動的フィールドキーと同じ名前の静的フィールドを追加できます。新しい静的フィールドは動的フィールドキーをマスクしますが、元の動的データは保持されます。
フィールド名での競合を回避するには、実際に追加する前に既存のフィールドと動的フィールドキーを参照してフィールド名を検討してください。
例のシナリオ:
- Python
- Java
- NodeJS
- Go
- cURL
# 動的フィールドが有効な元のコレクション
# 動的フィールドキーでデータを挿入
data = [{
"id": 1,
"my_vector": [0.1, 0.2, ...],
"extra_info": "this is a dynamic field key", # 文字列としての動的フィールドキー
"score": 99.5 # 別の動的フィールドキー
}]
client.insert(collection_name="product_catalog", data=data)
# 既存の動的フィールドキーと同じ名前の静的フィールドを追加
client.add_collection_field(
collection_name="product_catalog",
field_name="extra_info", # 動的フィールドキーと同じ名前
data_type=DataType.INT64, # データ型は動的フィールドキーと異なる可能性あり
nullable=True # 追加されたフィールドに対してTrueでなければなりません
)
# 静的フィールド追加後の新しいデータを挿入
new_data = [{
"id": 2,
"my_vector": [0.3, 0.4, ...],
"extra_info": 100, # 今やINT64型を使用する必要があります(静的フィールド)
"score": 88.0 # 依然として動的フィールドキー
}]
client.insert(collection_name="product_catalog", data=new_data)
import com.google.gson.*;
import io.milvus.v2.service.vector.request.InsertReq;
import io.milvus.v2.service.vector.response.InsertResp;
Gson gson = new Gson();
JsonObject row = new JsonObject();
row.addProperty("id", 1);
row.add("my_vector", gson.toJsonTree(new float[]{0.1f, 0.2f, ...}));
row.addProperty("extra_info", "this is a dynamic field key");
row.addProperty("score", 99.5);
InsertResp insertR = client.insert(InsertReq.builder()
.collectionName("product_catalog")
.data(Collections.singletonList(row))
.build());
client.addCollectionField(AddCollectionFieldReq.builder()
.collectionName("product_catalog")
.fieldName("extra_info")
.dataType(DataType.Int64)
.isNullable(true)
.build());
JsonObject newRow = new JsonObject();
newRow.addProperty("id", 2);
newRow.add("my_vector", gson.toJsonTree(new float[]{0.3f, 0.4f, ...}));
newRow.addProperty("extra_info", 100);
newRow.addProperty("score", 88.0);
insertR = client.insert(InsertReq.builder()
.collectionName("product_catalog")
.data(Collections.singletonList(newRow))
.build());
// 動的フィールドが有効な元のコレクション
// 動的フィールドキーでデータを挿入
const data = [{
"id": 1,
"my_vector": [0.1, 0.2, ...],
"extra_info": "this is a dynamic field key", // 文字列としての動的フィールドキー
"score": 99.5 // 別の動的フィールドキー
}]
await client.insert({
collection_name: "product_catalog",
data: data
});
// 既存の動的フィールドキーと同じ名前の静的フィールドを追加
await client.add_collection_field({
collection_name: "product_catalog",
field_name: "extra_info", // 動的フィールドキーと同じ名前
data_type: DataType.INT64, // データ型は動的フィールドキーと異なる可能性あり
nullable: true // 追加されたフィールドに対してTrueでなければなりません
});
// 静的フィールド追加後の新しいデータを挿入
const new_data = [{
"id": 2,
"my_vector": [0.3, 0.4, ...],
"extra_info": 100, # 今やINT64型を使用する必要があります(静的フィールド)
"score": 88.0 # 依然として動的フィールドキー
}];
await client.insert({
collection_name:"product_catalog",
data: new_data
});
// go
# restful
#!/bin/bash
export MILVUS_HOST="YOUR_CLUSTER_ENDPOINT"
export AUTH_TOKEN="your_token_here"
export COLLECTION_NAME="product_catalog"
echo "Step 1: 動的フィールドで初期データを挿入..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/entities/insert" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"data\": [{
\"id\": 1,
\"my_vector\": [0.1, 0.2, 0.3, 0.4, 0.5],
\"extra_info\": \"this is a dynamic field key\",
\"score\": 99.5
}]
}"
echo -e "\n\nStep 2: 動的フィールドと同じ名前の静的フィールドを追加..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/collections/fields/add" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"schema\": {
\"fieldName\": \"extra_info\",
\"dataType\": \"Int64\",
\"nullable\": true
}
}"
echo -e "\n\nStep 3: 静的フィールド追加後の新しいデータを挿入..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/entities/insert" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"data\": [{
\"id\": 2,
\"my_vector\": [0.3, 0.4, 0.5, 0.6, 0.7],
\"extra_info\": 100,
\"score\": 88.0
}]
}"
期待される動作:
-
既存のエンティティは新しい静的フィールド
extra_infoにNULLを持つ -
新しいエンティティは静的フィールドのデータ型(
INT64)を使用しなければならない -
元の動的フィールドキー値は保持され、
$meta構文でアクセス可能 -
静的フィールドは通常のクエリで動的フィールドキーをマスク
静的および動的値の両方へのアクセス:
- Python
- Java
- NodeJS
- Go
- cURL
# 1. 静的フィールドのみをクエリ(動的フィールドキーはマスクされる)
results = client.query(
collection_name="product_catalog",
filter="id == 1",
output_fields=["extra_info"]
)
# 返り値: {"id": 1, "extra_info": None} # 既存エンティティのNULL
# 2. 静的および元の動的値の両方をクエリ
results = client.query(
collection_name="product_catalog",
filter="id == 1",
output_fields=["extra_info", "$meta['extra_info']"]
)
# 返り値: {
# "id": 1,
# "extra_info": None, # 静的フィールド値(NULL)
# "$meta['extra_info']": "this is a dynamic field key" # 元の動的値
# }
# 3. 静的フィールド値を持つ新しいエンティティをクエリ
results = client.query(
collection_name="product_catalog",
filter="id == 2",
output_fields=["extra_info"]
)
# 返り値: {"id": 2, "extra_info": 100} # 静的フィールド値
// java
// 1. 静的フィールドのみをクエリ(動的フィールドキーはマスクされる)
let results = client.query({
collection_name: "product_catalog",
filter: "id == 1",
output_fields: ["extra_info"]
})
// 返り値: {"id": 1, "extra_info": None} # 既存エンティティのNULL
// 2. 静的および元の動的値の両方をクエリ
results = client.query({
collection_name:"product_catalog",
filter: "id == 1",
output_fields: ["extra_info", "$meta['extra_info']"]
});
// 返り値: {
// "id": 1,
// "extra_info": None, # 静的フィールド値(NULL)
// "$meta['extra_info']": "this is a dynamic field key" # 元の動的値
// }
// 3. 静的フィールド値を持つ新しいエンティティをクエリ
results = client.query({
collection_name: "product_catalog",
filter: "id == 2",
output_fields: ["extra_info"]
})
# 返り値: {"id": 2, "extra_info": 100} # 静的フィールド値
// go
# restful
#!/bin/bash
export MILVUS_HOST="YOUR_CLUSTER_ENDPOINT"
export AUTH_TOKEN="your_token_here"
export COLLECTION_NAME="product_catalog"
echo "Query 1: 静的フィールドのみ(動的フィールドはマスク)..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/entities/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"filter\": \"id == 1\",
\"outputFields\": [\"extra_info\"]
}"
echo -e "\n\nQuery 2: 静的および元の動的値の両方..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/entities/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"filter\": \"id == 1\",
\"outputFields\": [\"extra_info\", \"\$meta['extra_info']\"]
}"
echo -e "\n\nQuery 3: 静的フィールド値を持つ新しいエンティティ..."
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/entities/query" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${AUTH_TOKEN}" \
-d "{
\"collectionName\": \"${COLLECTION_NAME}\",
\"filter\": \"id == 2\",
\"outputFields\": [\"extra_info\"]
}"
新しいフィールドが使用可能になるまでどれくらいかかりますか?
追加されたフィールドはほぼ即座に使用可能になりますが、Milvusクラスター全体にわたる内部スキーマ変更のブロードキャストによる短い遅延が発生する可能性があります。この同期により、新しいフィールドを含むクエリを処理する前にすべてのノードがスキーマ更新を認識していることを確実にします。