默认值

Milvus 允许为标量字段(主字段除外)设置默认值。当一个字段配置了默认值时,如果在插入过程中没有提供数据,Milvus 会自动应用该值。

通过保留现有的默认值设置,默认值简化了从其他数据库系统到 Milvus 的数据迁移。对于插入时可能无法确定值的字段,也可以使用默认值。

限制

  • 只有标量字段支持默认值。主字段和向量字段不能使用默认值。

  • JSONARRAY 字段不支持默认值。

  • 默认值只能在创建 Collections 时配置,之后不能修改。

设置默认值

创建 Collections 时,使用add_field() 中的default_value 参数定义字段的默认值。

下面的示例创建了一个集合,其中有两个标量字段具有默认值:age 默认为18status 默认为"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

使用默认值搜索和查询

在向量搜索和标量过滤过程中,包含默认值的实体与其他实体的行为相同。您可以在searchquery 操作符中使用默认值进行过滤。

下面的示例搜索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

适用规则

nullabledefault_value 都配置为一个字段时,以下规则决定了 Milvus 在插入过程中如何处理 NULL 输入或缺少的字段值。

可为空

默认值

用户输入

结果

✅(非空)

空或省略

使用默认值

NULL 或省略

存储为 NULL

✅(非空)

NULL 或省略

使用默认值

NULL 或省略

抛出错误

✅(NULL)

空或省略

抛出错误

主要启示

  • 当字段具有非空默认值时,无论是否启用nullable ,都会使用该值。

  • nullable=True 但未设置默认值时,字段存储 NULL。

  • nullable=False 但未设置默认值时,插入失败并显示错误。

  • 在不可为空的字段上设置 NULL 默认值是无效的,会导致错误。

想要更快、更简单、更好用的 Milvus SaaS服务 ?

Zilliz Cloud是基于Milvus的全托管向量数据库,拥有更高性能,更易扩展,以及卓越性价比

免费试用 Zilliz Cloud
反馈

此页对您是否有帮助?