デフォルト値
Milvusではスカラーフィールド(プライマリフィールドを除く)にデフォルト値を設定することができます。フィールドにデフォルト値が設定されている場合、Milvusは挿入時にデータが提供されない場合、自動的にこの値を適用します。
デフォルト値は、既存のデフォルト値設定を保持することにより、他のデータベースシステムからMilvusへのデータ移行を簡素化します。また、挿入時に値が不確かなフィールドにデフォルト値を使用することもできます。
制限事項
デフォルト値をサポートするのはスカラーフィールドのみです。プライマリフィールドとベクトルフィールドはデフォルト値を持つことができません。
JSONおよびARRAYフィールドはデフォルト値をサポートしていません。既定値はコレクション作成時にのみ設定でき、その後で変更することはできません。
デフォルト値の設定
コレクションを作成するとき、add_field() のdefault_value パラメータを使用して、 フィールドのデフォルト値を定義する。
次の例では、デフォルト値を持つ2つのスカラーフィールドを持つコレクションを作成します。age のデフォルト値は18 で、status のデフォルト値は"active" です。
from pymilvus import MilvusClient, DataType
client = MilvusClient(uri='http://localhost:19530')
# 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は自動的に設定されたデフォルト値を使用します。
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 と等しいエンティティを検索します:
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
デフォルト値に直接マッチしてエンティティを検索することもできます:
# 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 デフォルト値を設定することは無効であり、エラーになります。