メインコンテンツまでスキップ

デフォルト値

Zilliz Cloud では、スカラーフィールド(プライマリフィールドを除く)にデフォルト値を設定できます。フィールドにデフォルト値が設定されている場合、挿入時にデータが指定されなければ、Zilliz Cloud は自動的にその値を適用します。

デフォルト値を使用すると、既存のデフォルト値設定を保持したまま、他のデータベースシステムから Zilliz Cloud へのデータ移行を簡素化できます。また、挿入時点では値が未確定な可能性があるフィールドにもデフォルト値を利用できます。

制限

  • デフォルト値をサポートするのはスカラーフィールドのみです。プライマリフィールドとベクトルフィールドにはデフォルト値を設定できません。

  • JSON フィールドと ARRAY フィールドはデフォルト値をサポートしません。

  • デフォルト値を設定できるのはコレクション作成時のみで、その後は変更できません。

デフォルト値を設定する

コレクションの作成時に、add_field()default_value パラメータを使用してフィールドのデフォルト値を定義します。

次の例では、デフォルト値を持つ 2 つのスカラーフィールドを含むコレクションを作成しています。age のデフォルト値は 18status のデフォルト値は "active" です。

python
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)

エンティティを挿入する

データを挿入する際、デフォルト値を持つフィールドを省略した場合、またはそのフィールドを明示的に NULL に設定した場合、Zilliz Cloud は設定済みのデフォルト値を自動的に使用します。

python
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)

デフォルト値を使った検索とクエリ

デフォルト値を含むエンティティは、ベクトル検索やスカラーフィルタリングにおいて、他のエンティティと同じように動作します。searchquery の両方の操作で、デフォルト値を使ってフィルタリングできます。

次の例では、age がデフォルト値 18 に等しいエンティティを検索します。

python
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']}")
想定される出力
plaintext
Output:
Search results (age == 18):
id: 2, age: 18, status: active
id: 4, age: 18, status: inactive

また、デフォルト値に直接一致するエンティティをクエリすることもできます。

python
# 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']}")
想定される出力
plaintext
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

適用ルール

あるフィールドに nullabledefault_value の両方が設定されている場合、挿入時に NULL 入力またはフィールド値の欠落を Zilliz Cloud がどのように処理するかは、次のルールによって決まります。

Nullableデフォルト値ユーザー入力結果
(非 NULL)NULL または省略デフォルト値を使用
NULL または省略NULL として保存
(非 NULL)NULL または省略デフォルト値を使用
NULL または省略エラーをスロー
(NULL)NULL または省略エラーをスロー

重要なポイント:

  • フィールドに非 NULL のデフォルト値がある場合、nullable が有効かどうかに関係なく、その値が使用されます。

  • nullable=True でデフォルト値が設定されていない場合、そのフィールドには NULL が保存されます。

  • nullable=False でデフォルト値が設定されていない場合、挿入はエラーで失敗します。

  • NULL 不可のフィールドに NULL のデフォルト値を設定することは無効であり、エラーの原因となります。

Ctrl I