跳到主要内容

布尔与数值类型

布尔与数值类型的标量字段用于存放布尔值或标量数值。布尔值是指在两个可能的值中取一个,而标量数值则既可能是整数,也可能是小数。它们通常用来表示数量、度量值或其它需要进行逻辑或数学运算的数据。

如下表格描述了 Zilliz Cloud 支持的布尔与数值类数据类型。

数据类型描述
BOOL布尔类型,用于存储 truefalse,适合描述二元状态,例如是否有效、是否有库存等。
INT88 位整数,适用于存储小范围的整数数据。
INT1616 位整数,适用于存储中等范围的整数数据。
INT3232 位整数,适合一般整型数据存储,如商品数量或用户 ID。
INT6464 位整数,适合存储较大范围的数据,例如时间戳或标识符。
FLOAT32 位浮点数,适用于一般精度的数值数据,如评分或温度。
DOUBLE64 位双精度浮点数,适用于需要高精度的数据,如金融数据或科学计算。

如需定义一个布尔类型的字段,需要将 datatype 设置成 BOOL。如需定义一个数值类型的字段,则可以将 datatype 设置成上述支持的数值类型。例如,DataType.INT64 表示一个整数类型的字段,而 DataType.FLOAT 则表示一个小数类型的字段。

说明

Zilliz Cloud 允许布尔类型或数值类型的字段为空。您也可以为它们设置默认值。简单来说,您可以将字段的 nullable 设置为 True 来允许字段为空,并通过 default_value 为字段设置默认值,具体可以参考 Nullable 和默认值

添加布尔或数值字段

您可以在 Collection Schema 中字义布尔或数值类型的字段来存放相应类型的数据。在下方的示例中,Schema 中定义了如下字段:

  • age:用于存放整数类型的数据,允许为空,默认值为 18

  • broken:用于存放布尔类型的数据,允许为空,无默认值。

  • height:用于存放小数类型的数据,允许为空,无默认值。

说明

如果您在定义 Schema 时将 enabled_dynamic_fields 设置为 True,您还可以在该 Collection 中插入 Schema 中未定义的字段。此操作可能会增加查询和管理的复杂性,并影响查询性能。更多详情,请参考 Dynamic Field

python
# Import necessary libraries
from pymilvus import MilvusClient, DataType

# Define server address
SERVER_ADDR = "YOUR_CLUSTER_ENDPOINT"

# 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)
schema.add_field(field_name="broken", datatype=DataType.BOOL, nullable=True)
# 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)

设置索引参数

为标量数值字段设置索引参数可以提供查询和搜索效率。在 Zilliz Cloud clusters 中,为向量字段创建索引为必选操作。对于包括布尔和数值类型的标量字段而言,该操作为可选。

以下示例中,我们为向量字段 embedding 和 标量字段 age 创建了 AUTOINDEX 类型的索引,表示 Milvus 会自动根据数据类型创建合适的索引。有关更多信息,请参考 AUTOINDEX

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

定义好 Collection 的 Schema 和索引后,我们便可以创建包含标量字段的 Collection。

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

插入数据

Collection 创建完成后,可以插入包含标量字段的数据。

python
# 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 方法获取符合指定条件的所有 Entity。

如下示例演示了获取 age 字段大于 30 的所有 Entity。

python
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 字段为 null 的所有 Entity。

python
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 的所有 Entity,可以参考如下示例。由于 18age 字段的默认值,查询结果将包含该字段显式设置为 18 以及设置为 null 的所有 Entity。

python
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}"
# ]

使用过滤表达式的向量查询

除了简单的基于数值的过滤查询外,您还可以将向量相似性搜索和过滤表达式结合。如下示例演示了如何在向量搜索中添加过滤表达式。

python
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。查询结果除了与查询向量在语义上相关外,还需要满足过滤表达式的要求。关于过滤查询的更多内容,可以参考 Filtered Search