デフォルト値
Milvusでは、スカラーフィールド(プライマリフィールドを除く)にデフォルト値を設定できます。フィールドにデフォルト値が設定されている場合、挿入時にデータが提供されないと、Milvusはこの値を自動的に適用します。
デフォルト値は、既存のデフォルト値設定を保持することで、他のデータベースシステムからMilvusへのデータ移行を簡素化します。また、挿入時に値が不確かなフィールドにデフォルト値を使用することもできます。
制限事項
-
スカラーフィールドのみがデフォルト値をサポートします。プライマリフィールドとベクトルフィールドはデフォルト値を持つことができません。
-
JSONおよびARRAYフィールドはデフォルト値をサポートしません。 -
デフォルト値はコレクション作成時にのみ設定でき、後で変更することはできません。
デフォルト値の設定
コレクションを作成する際、add_field() の default_value パラメータを使用してフィールドのデフォルト値を定義します。
以下の例では、デフォルト値を持つ2つのスカラーフィールドを持つコレクションを作成します。age はデフォルトで 18、status はデフォルトで "active" です。
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import MilvusClient, DataType
client = MilvusClient(uri='YOUR_CLUSTER_ENDPOINT')
# Define collection schema
schema = client.create_schema(
auto_id=False,
enable_dynamic_schema=True,
)
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=5)
schema.add_field(field_name="age", datatype=DataType.INT64, default_value=18)
schema.add_field(field_name="status", datatype=DataType.VARCHAR, default_value="active", max_length=10)
# Set index params
index_params = client.prepare_index_params()
index_params.add_index(field_name="vector", index_type="AUTOINDEX", metric_type="L2")
# Create collection
client.create_collection(collection_name="my_collection", schema=schema, index_params=index_params)
// java
// js
// go
# restful
エンティティの挿入
データを挿入する際、デフォルト値を持つフィールドを省略したり、明示的にNULLに設定したりすると、Milvusは自動的に設定されたデフォルト値を使用します。
- Python
- Java
- NodeJS
- Go
- cURL
data = [
# All fields provided explicitly
{"id": 1, "vector": [0.1, 0.2, 0.3, 0.4, 0.5], "age": 30, "status": "premium"},
# age and status omitted → both use default values (18 and "active")
{"id": 2, "vector": [0.2, 0.3, 0.4, 0.5, 0.6]},
# status set to None → uses default value "active"
{"id": 3, "vector": [0.3, 0.4, 0.5, 0.6, 0.7], "age": 25, "status": None},
# age set to None → uses default value 18
{"id": 4, "vector": [0.4, 0.5, 0.6, 0.7, 0.8], "age": None, "status": "inactive"}
]
client.insert(collection_name="my_collection", data=data)
// java
// js
// go
# restful
デフォルト値を使用した検索とクエリ
デフォルト値を含むエンティティは、ベクトル検索およびスカラーフィルタリング中に他のエンティティと同様に動作します。search操作とquery操作の両方で、デフォルト値でフィルタリングできます。
次の例は、ageがデフォルト値18と等しいエンティティを検索します。
- Python
- Java
- NodeJS
- Go
- cURL
res = client.search(
collection_name="my_collection",
data=[[0.1, 0.2, 0.4, 0.3, 0.5]],
search_params={"params": {"nprobe": 16}},
filter="age == 18",
limit=10,
output_fields=["id", "age", "status"]
)
print("Search results (age == 18):")
for hit in res[0]:
print(f" id: {hit['id']}, age: {hit['entity']['age']}, status: {hit['entity']['status']}")
// java
// js
// go
# restful
期待される出力
Output:
Search results (age == 18):
id: 2, age: 18, status: active
id: 4, age: 18, status: inactive
デフォルト値に直接一致させることで、エンティティをクエリすることもできます。
- Python
- Java
- NodeJS
- Go
- cURL
# Query entities where age equals the default value (18)
default_age_results = client.query(
collection_name="my_collection",
filter="age == 18",
output_fields=["id", "age", "status"]
)
print("\nQuery results (age == 18):")
for r in default_age_results:
print(f" id: {r['id']}, age: {r['age']}, status: {r['status']}")
# Query entities where status equals the default value ("active")
default_status_results = client.query(
collection_name="my_collection",
filter='status == "active"',
output_fields=["id", "age", "status"]
)
print("\nQuery results (status == 'active'):")
for r in default_status_results:
print(f" id: {r['id']}, age: {r['age']}, status: {r['status']}")
// java
// js
// go
# restful
期待される出力
Query results (age == 18):
id: 2, age: 18, status: active
id: 4, age: 18, status: inactive
Query results (status == 'active'):
id: 2, age: 18, status: active
id: 3, age: 25, status: active
適用されるルール
フィールドに対してnullableとdefault_valueの両方が設定されている場合、挿入時にMilvusがNULL入力または欠落しているフィールド値をどのように処理するかは、以下のルールによって決定されます。
NULL許容 | デフォルト値 | ユーザー入力 | 結果 |
|---|---|---|---|
✅ | ✅ (非NULL) | NULLまたは省略 | デフォルト値を使用 |
✅ | ❌ | NULLまたは省略 | NULLとして保存 |
❌ | ✅ (非NULL) | NULLまたは省略 | デフォルト値を使用 |
❌ | ❌ | NULLまたは省略 | エラーをスロー |
❌ | ✅ (NULL) | NULLまたは省略 | エラーをスロー |
主なポイント:
-
フィールドに非NULLのデフォルト値がある場合、
nullableが有効になっているかどうかに関係なく、その値が使用されます。 -
nullable=Trueだがデフォルト値が設定されていない場合、フィールドはNULLを格納します。 -
nullable=Falseでデフォルト値が設定されていない場合、挿入はエラーで失敗します。 -
NULL許容ではないフィールドにNULLのデフォルト値を設定することは無効であり、エラーが発生します。