跳到主要内容
版本:v2.4.x

获取和标量查询

本指南演示如何通过 ID 获取实体并进行标量过滤。标量过滤可检索符合指定过滤条件的实体。

概述

标量查询使用布尔表达式根据定义的条件过滤 Collection 中的实体。查询结果是一组符合定义条件的实体。与向量搜索(在 Collection 中识别与给定向量最接近的向量)不同,查询是根据特定条件过滤实体。

在 Milvus 中,过滤器总是一个由字段名和操作符组成的字符串。在本指南中,你将看到各种过滤器示例。要了解更多操作符详情,请参阅参考资料部分。

准备工作

下面的步骤将重新利用代码连接到 Milvus,快速建立一个 Collection,并将 1000 多个随机生成的实体插入到 Collection 中。

步骤 1:创建 Collection

使用 MilvusClient连接到 Milvus 服务器,并使用 create_collection()创建 Collection。

使用 MilvusClientV2连接到 Milvus 服务器,并使用 createCollection()创建 Collection。

使用 MilvusClient连接到 Milvus 服务器,并使用 createCollection()创建 Collection。

from pymilvus import MilvusClient

# 1. Set up a Milvus client
client = MilvusClient(
uri="http://localhost:19530"
)

# 2. Create a collection
client.create_collection(
collection_name="quick_setup",
dimension=5,
)

第二步:插入随机生成的实体

使用 insert()将实体插入 Collection。

使用 insert()将实体插入 Collection。

使用 insert()将实体插入 Collection。

# 3. Insert randomly generated vectors
colors = ["green", "blue", "yellow", "red", "black", "white", "purple", "pink", "orange", "brown", "grey"]
data = []

for i in range(1000):
current_color = random.choice(colors)
current_tag = random.randint(1000, 9999)
data.append({
"id": i,
"vector": [ random.uniform(-1, 1) for _ in range(5) ],
"color": current_color,
"tag": current_tag,
"color_tag": f"{current_color}_{str(current_tag)}"
})

print(data[0])

# Output
#
# {
# "id": 0,
# "vector": [
# 0.7371107800002366,
# -0.7290389773227746,
# 0.38367002049157417,
# 0.36996000494220627,
# -0.3641898951462792
# ],
# "color": "yellow",
# "tag": 6781,
# "color_tag": "yellow_6781"
# }

res = client.insert(
collection_name="quick_setup",
data=data
)

print(res)

# Output
#
# {
# "insert_count": 1000,
# "ids": [
# 0,
# 1,
# 2,
# 3,
# 4,
# 5,
# 6,
# 7,
# 8,
# 9,
# "(990 more items hidden)"
# ]
# }

第 3 步:创建 Partition 并插入更多实体

使用 create_partition()创建 Partition,并使用 insert()将更多实体插入 Collection。

使用 createPartition()创建 Partition 并 insert()将更多实体插入 Collection。

使用 createPartition()创建 Partition,并 insert()向 Collection 插入更多实体。

# 4. Create partitions and insert more entities
client.create_partition(
collection_name="quick_setup",
partition_name="partitionA"
)

client.create_partition(
collection_name="quick_setup",
partition_name="partitionB"
)

data = []

for i in range(1000, 1500):
current_color = random.choice(colors)
data.append({
"id": i,
"vector": [ random.uniform(-1, 1) for _ in range(5) ],
"color": current_color,
"tag": current_tag,
"color_tag": f"{current_color}_{str(current_tag)}"
})

res = client.insert(
collection_name="quick_setup",
data=data,
partition_name="partitionA"
)

print(res)

# Output
#
# {
# "insert_count": 500,
# "ids": [
# 1000,
# 1001,
# 1002,
# 1003,
# 1004,
# 1005,
# 1006,
# 1007,
# 1008,
# 1009,
# "(490 more items hidden)"
# ]
# }

data = []

for i in range(1500, 2000):
current_color = random.choice(colors)
data.append({
"id": i,
"vector": [ random.uniform(-1, 1) for _ in range(5) ],
"color": current_color,
"tag": current_tag,
"color_tag": f"{current_color}_{str(current_tag)}"
})

res = client.insert(
collection_name="quick_setup",
data=data,
partition_name="partitionB"
)

print(res)

# Output
#
# {
# "insert_count": 500,
# "ids": [
# 1500,
# 1501,
# 1502,
# 1503,
# 1504,
# 1505,
# 1506,
# 1507,
# 1508,
# 1509,
# "(490 more items hidden)"
# ]
# }

通过 ID 获取实体

如果您知道感兴趣的实体的 ID,可以使用 get()方法。

如果您知道您感兴趣的实体的 ID,可以使用 get()方法。

如果您知道您感兴趣的实体的 ID,可以使用 get()方法。

# 4. Get entities by ID
res = client.get(
collection_name="quick_setup",
ids=[0, 1, 2]
)

print(res)

# Output
#
# [
# {
# "id": 0,
# "vector": [
# 0.68824464,
# 0.6552274,
# 0.33593303,
# -0.7099536,
# -0.07070546
# ],
# "color_tag": "green_2006",
# "color": "green"
# },
# {
# "id": 1,
# "vector": [
# -0.98531723,
# 0.33456197,
# 0.2844234,
# 0.42886782,
# 0.32753858
# ],
# "color_tag": "white_9298",
# "color": "white"
# },
# {
# "id": 2,
# "vector": [
# -0.9886812,
# -0.44129863,
# -0.29859528,
# 0.06059075,
# -0.43817034
# ],
# "color_tag": "grey_5312",
# "color": "grey"
# }
# ]

从 Partition 获取实体

您还可以从特定 Partition 中获取实体。

# 5. Get entities from partitions
res = client.get(
collection_name="quick_setup",
ids=[1000, 1001, 1002],
partition_names=["partitionA"]
)

print(res)

# Output
#
# [
# {
# "color": "green",
# "tag": 1995,
# "color_tag": "green_1995",
# "id": 1000,
# "vector": [
# 0.7807706,
# 0.8083741,
# 0.17276904,
# -0.8580777,
# 0.024156934
# ]
# },
# {
# "color": "red",
# "tag": 1995,
# "color_tag": "red_1995",
# "id": 1001,
# "vector": [
# 0.065074645,
# -0.44882354,
# -0.29479212,
# -0.19798489,
# -0.77542555
# ]
# },
# {
# "color": "green",
# "tag": 1995,
# "color_tag": "green_1995",
# "id": 1002,
# "vector": [
# 0.027934508,
# -0.44199976,
# -0.40262738,
# -0.041511405,
# 0.024782438
# ]
# }
# ]

使用基本操作符

在本节中,您将找到如何在标量过滤中使用基本操作符的示例。您也可以将这些筛选器应用于向量搜索数据删除

有关详细信息,请参阅 query()中的

更多信息,请参阅 query()以获取更多信息。

更多信息,请参阅 query()中的

  • 过滤标签值在 1,000 至 1,500 之间的实体。

    # 6. Use basic operators

    res = client.query(
    collection_name="quick_setup",
    filter="1000 < tag < 1500",
    output_fields=["color_tag"],
    limit=3
    )

    print(res)

    # Output
    #
    # [
    # {
    # "id": 1,
    # "color_tag": "pink_1023"
    # },
    # {
    # "id": 41,
    # "color_tag": "red_1483"
    # },
    # {
    # "id": 44,
    # "color_tag": "grey_1146"
    # }
    # ]
  • 过滤颜色值设置为棕色的实体。

    res = client.query(
    collection_name="quick_setup",
    filter='color == "brown"',
    output_fields=["color_tag"],
    limit=3
    )

    print(res)

    # Output
    #
    # [
    # {
    # "color_tag": "brown_5343",
    # "id": 15
    # },
    # {
    # "color_tag": "brown_3167",
    # "id": 27
    # },
    # {
    # "color_tag": "brown_3100",
    # "id": 30
    # }
    # ]
  • 过滤颜色值未设置为绿色紫色的实体。

    res = client.query(
    collection_name="quick_setup",
    filter='color not in ["green", "purple"]',
    output_fields=["color_tag"],
    limit=3
    )

    print(res)

    # Output
    #
    # [
    # {
    # "color_tag": "yellow_6781",
    # "id": 0
    # },
    # {
    # "color_tag": "pink_1023",
    # "id": 1
    # },
    # {
    # "color_tag": "blue_3972",
    # "id": 2
    # }
    # ]
  • 过滤颜色标记以红色开头的文章。

    res = client.query(
    collection_name="quick_setup",
    filter='color_tag like "red%"',
    output_fields=["color_tag"],
    limit=3
    )

    print(res)

    # Output
    #
    # [
    # {
    # "color_tag": "red_6443",
    # "id": 17
    # },
    # {
    # "color_tag": "red_1483",
    # "id": 41
    # },
    # {
    # "color_tag": "red_4348",
    # "id": 47
    # }
    # ]
  • 过滤颜色设置为红色且标签值在 1,000 至 1,500 范围内的实体。

    res = client.query(
    collection_name="quick_setup",
    filter='(color == "red") and (1000 < tag < 1500)',
    output_fields=["color_tag"],
    limit=3
    )

    print(res)

    # Output
    #
    # [
    # {
    # "color_tag": "red_1483",
    # "id": 41
    # },
    # {
    # "color_tag": "red_1100",
    # "id": 94
    # },
    # {
    # "color_tag": "red_1343",
    # "id": 526
    # }
    # ]

使用高级操作符

本节将举例说明如何在标量过滤中使用高级操作符。您也可以将这些筛选器应用于向量搜索数据删除

计数实体

  • 计算 Collection 中实体的总数。

    # 7. Use advanced operators

    # Count the total number of entities in a collection
    res = client.query(
    collection_name="quick_setup",
    output_fields=["count(*)"]
    )

    print(res)

    # Output
    #
    # [
    # {
    # "count(*)": 2000
    # }
    # ]
  • 计算特定 Partition 中实体的总数。

    # Count the number of entities in a partition
    res = client.query(
    collection_name="quick_setup",
    output_fields=["count(*)"],
    partition_names=["partitionA"]
    )

    print(res)

    # Output
    #
    # [
    # {
    # "count(*)": 500
    # }
    # ]
  • 统计符合过滤条件的实体数量

    # Count the number of entities that match a specific filter
    res = client.query(
    collection_name="quick_setup",
    filter='(color == "red") and (1000 < tag < 1500)',
    output_fields=["count(*)"],
    )

    print(res)

    # Output
    #
    # [
    # {
    # "count(*)": 3
    # }
    # ]

标量过滤器参考

基本操作符

布尔表达式始终是由字段名和操作符组成的字符串。本节将详细介绍基本操作符。

操作符说明
和 (&&)如果两个操作数都为真,则为真
**或 (
+, -, *, /加法、减法、乘法和除法
**指数
%模数
<, >小于、大于
==, !=等于,不等于
<=, >=小于或等于,大于或等于
不等于逆转给定条件的结果。
相似使用通配符将一个值与类似值进行比较。
例如,like "prefix%"匹配以 "prefix "开头的字符串。
in测试表达式是否匹配值列表中的任何值。

高级操作符

  • count(*)

    计算 Collection 中实体的确切数量。将其用作输出字段,可获得 Collection 或 Partition 中实体的确切数目。

    说明

    注释

    这适用于已加载的 Collection。应将其作为唯一的输出字段。