跳到主要内容
版本:v3.0.x

数字字段

数字字段是存储数值的标量字段。这些数值可以是整数(整数)或十进制数**(浮点数**)。它们通常用于表示数量、测量值或任何需要进行数学处理的数据。

下表描述了 Milvus 中可用的数字字段数据类型。

| 字段类型

|

描述

| | --- | --- | |

BOOL

|

布尔类型,用于存储truefalse ,适合描述二进制状态。

| |

INT8

|

8 位整数,适合存储小范围整数数据。

| |

INT16

|

16 位整数,适用于中范围整数数据。

| |

INT32

|

32 位整数,适合存储一般整数数据,如产品数量或用户 ID。

| |

INT64

|

64 位整数,适合存储时间戳或标识符等大范围数据。

| |

FLOAT

|

32 位浮点数,适用于需要一般精度的数据,如等级或温度。

| |

DOUBLE

|

64 位双精度浮点数,用于高精度数据,如财务信息或科学计算。

|

要声明数字字段,只需将datatype 设置为可用的数字数据类型之一。例如,DataType.INT64 表示整数字段,DataType.FLOAT 表示浮点字段。

说明

Milvus 支持数字字段的空值和默认值。要启用这些功能,请将nullable 设置为True ,将default_value 设置为数值。有关详情,请参阅可空值和默认值

添加数字字段

要存储数值数据,请在 Collection Schema 中定义一个数字字段。下面是一个包含两个数字字段的 Collection 模式示例:

  • age:存储整数数据,允许空值,默认值为18

  • price:存储浮点数据,允许空值,但没有默认值。

说明

如果在定义 Schema 时设置enable_dynamic_fields=True ,Milvus 允许插入事先未定义的标量字段。不过,这可能会增加查询和管理的复杂性,并可能影响性能。更多信息,请参阅动态字段

# Import necessary libraries
from pymilvus import MilvusClient, DataType

# Define server address
SERVER_ADDR = "http://localhost:19530"

# Create a MilvusClient instance
client = MilvusClient(uri=SERVER_ADDR)

# Define the collection schema
schema = client.create_schema(
auto_id=False,
enable_dynamic_fields=True,
)

# Add an INT64 field `age` that supports null values with default value 18
schema.add_field(field_name="age", datatype=DataType.INT64, nullable=True, default_value=18)
# Add a FLOAT field `price` that supports null values without default value
schema.add_field(field_name="price", datatype=DataType.FLOAT, nullable=True)
schema.add_field(field_name="pk", datatype=DataType.INT64, is_primary=True)
schema.add_field(field_name="embedding", datatype=DataType.FLOAT_VECTOR, dim=3)

设置索引参数

索引有助于提高搜索和查询性能。在 Milvus 中,对于向量字段必须建立索引,但对于标量字段可选。

下面的示例使用AUTOINDEX 索引类型为向量字段embedding 和标量字段age 创建了索引。使用这种类型,Milvus 会根据数据类型自动选择最合适的索引。您还可以自定义每个字段的索引类型和参数。有关详情,请参阅索引说明

# Set index params

index_params = client.prepare_index_params()

# Index `age` with AUTOINDEX
index_params.add_index(
field_name="age",
index_type="AUTOINDEX",
index_name="age_index"
)

# Index `embedding` with AUTOINDEX and specify similarity metric type
index_params.add_index(
field_name="embedding",
index_type="AUTOINDEX", # Use automatic indexing to simplify complex index settings
metric_type="COSINE" # Specify similarity metric type, options include L2, COSINE, or IP
)

创建 Collection

定义好 Schema 和索引后,创建一个包含数字字段的 Collection。

# Create Collection
client.create_collection(
collection_name="my_collection",
schema=schema,
index_params=index_params
)

插入数据

创建 Collection 后,插入与 Schema 匹配的实体。

# Sample data
data = [
{"age": 25, "price": 99.99, "pk": 1, "embedding": [0.1, 0.2, 0.3]},
{"age": 30, "pk": 2, "embedding": [0.4, 0.5, 0.6]}, # `price` field is missing, which should be null
{"age": None, "price": None, "pk": 3, "embedding": [0.2, 0.3, 0.1]}, # `age` should default to 18, `price` is null
{"age": 45, "price": None, "pk": 4, "embedding": [0.9, 0.1, 0.4]}, # `price` is null
{"age": None, "price": 59.99, "pk": 5, "embedding": [0.8, 0.5, 0.3]}, # `age` should default to 18
{"age": 60, "price": None, "pk": 6, "embedding": [0.1, 0.6, 0.9]} # `price` is null
]

client.insert(
collection_name="my_collection",
data=data
)

使用过滤表达式查询

插入实体后,使用query 方法检索与指定过滤表达式匹配的实体。

检索age 大于 30 的实体:

filter = 'age > 30'

res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["age", "price", "pk"]
)

print(res)

# Example output:
# data: [
# "{'age': 45, 'price': None, 'pk': 4}",
# "{'age': 60, 'price': None, 'pk': 6}"
# ]

检索price 为空的实体:

filter = 'price is null'

res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["age", "price", "pk"]
)

print(res)

# Example output:
# data: [
# "{'age': 30, 'price': None, 'pk': 2}",
# "{'age': 18, 'price': None, 'pk': 3}",
# "{'age': 45, 'price': None, 'pk': 4}",
# "{'age': 60, 'price': None, 'pk': 6}"
# ]

要检索age 的值为18 的实体,请使用下面的表达式。由于age 的默认值是18 ,因此预期结果应包括将age 明确设置为18 或将age 设置为空的实体。

filter = 'age == 18'

res = client.query(
collection_name="my_collection",
filter=filter,
output_fields=["age", "price", "pk"]
)

print(res)

# Example output:
# data: [
# "{'age': 18, 'price': None, 'pk': 3}",
# "{'age': 18, 'price': 59.99, 'pk': 5}"
# ]

使用过滤表达式进行向量搜索

除了基本的数字字段过滤外,您还可以将向量相似性搜索与数字字段过滤器结合起来。例如,下面的代码展示了如何在向量搜索中添加数字字段过滤器:

filter = "25 <= age <= 35"

res = client.search(
collection_name="my_collection",
data=[[0.3, -0.6, 0.1]],
limit=5,
search_params={"params": {"nprobe": 10}},
output_fields=["age","price"],
filter=filter
)

print(res)

# Example output:
# data: [
# "[{'id': 2, 'distance': -0.2016308456659317, 'entity': {'age': 30, 'price': None}}, {'id': 1, 'distance': -0.23643313348293304, 'entity': {'age': 25, 'price': 99.98999786376953}}]"
# ]

在这个示例中,我们首先定义了一个查询向量,并在搜索过程中添加了一个过滤条件25 <= age <= 35 。这样不仅能确保搜索结果与查询向量相似,还能满足指定的年龄范围。更多信息,请参阅过滤