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

デフォルト値

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

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

Limits

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

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

  • デフォルト値は collection 作成時にのみ設定でき、その後に変更することはできません。

Set default values

collection を作成する際は、add_field()default_value パラメータを使用してフィールドのデフォルト値を定義します。

次の例では、デフォルト値を持つ 2 つの scalar フィールドを含む collection を作成します。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)

Insert entities

データを挿入する際、デフォルト値を持つフィールドを省略した場合、または明示的に 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)

Search and query with default values

デフォルト値を含む entities は、vector 検索および scalar フィルタリングの際に、他の entities と同様に動作します。searchquery の両方の操作で、デフォルト値によるフィルタリングが可能です。

次の例では、age がデフォルト値 18 と等しい entities を検索します。

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

デフォルト値に直接一致する entities を query することもできます。

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

Applicable rules

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

NullableDefault ValueUser InputResult
(non-NULL)NULL or omittedデフォルト値を使用
NULL or omittedNULL として保存
(non-NULL)NULL or omittedデフォルト値を使用
NULL or omittedエラーをスロー
(NULL)NULL or omittedエラーをスロー

重要なポイント:

  • フィールドに non-NULL のデフォルト値がある場合、nullable が有効かどうかにかかわらず、その値が使用されます。

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

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

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

Ctrl I