milvus数据库-查询

与向量相似性搜索不同,向量查询通过基于布尔表达式的标量过滤来检索向量。Milvus支持许多标量字段中的数据类型和各种布尔表达式。布尔表达式过滤标量字段或主键字段,并检索与过滤器匹配的所有结果。

一、单次查询

1.加载集合

2.执行查询

python 复制代码
# 使用集合对象的 query 方法来执行查询操作
res = collection.query(
    expr="book_id in [2,4,6,8]",  # 查询表达式,筛选满足条件的文档
    offset=0,  # 结果的偏移量,从结果中的第一个文档开始返回
    limit=10,  # 返回结果的最大数量
    output_fields=["book_id", "book_intro"]  # 指定要从查询结果中检索的字段
)

3.检查输出

python 复制代码
sorted_res = sorted(res, key=lambda k: k['book_id'])
sorted_res

4.统计实体

在执行查询时,将输出字段设置为count(*),就会返回检索到的实体数量。此时禁用limit

python 复制代码
res = collection.query(
  # filter entities whose ID is in the specified list
  expr="book_id in [2,4,6,8]", 
  output_fields = ["count(*)"],
)

print(res)
print(res[0])

二、查询时使用迭代器

1.使用迭代器查询

python 复制代码
expr = "600 <= num_pages <= 700"

output_fields=[bookID, authors]

limit = 5

query_iterator = collection.query_iterator(expr, output_fields, limit)

while True:
    # turn to the next page
    res = query_iterator.next()
    if len(res) == 0:
        print("query iteration finished, close")
        # close the iterator
        query_iterator.close()
        break
    for i in range(len(res)):
        print(res[i])

2.使用迭代器搜索

python 复制代码
vectors_to_search = rng.random((SEARCH_NQ, DIM))

search_params = {
    "metric_type": "L2",
    "params": {"nprobe": 10, "radius": 1.0},
}

search_iterator = collection.search_iterator(
    vectors_to_search,
    search_params,
    limit=5,
    output_fields=[bookID, authors]
)
                                             
while True:
    # turn to the next page
    res = search_iterator.next()
    if len(res[0]) == 0:
        print("search iteration finished, close")
        # close the iterator
        search_iterator.close()
        break
    for i in range(len(res[0])):
        print(res[0][i])
相关推荐
我叫洋洋28 分钟前
Cadence CIS 元器件库合并实战:3000+ 焊盘、156 个符号零冲突并入自有库
数据库·单片机·嵌入式硬件·oracle·电路
AI 编程助手GPT29 分钟前
GPT-6 Luna (Batch) 批量处理性能与质量深度评测
数据库·gpt·batch
殷色玫瑰37 分钟前
C/C++ 内存管理详解:从内存分布到 new/delete 底层原理
java·linux·c语言·c++
quweiie42 分钟前
Neo4j Community 安全访问配置总结
数据库·centos·neo4j
꯭自꯭闭꯭2 小时前
DM7主备升级方案
linux·服务器·数据库
青山木2 小时前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
朝朝辞暮i2 小时前
C++ 第 26 课:对象、指针与 ->
java·开发语言·c++
长弓三石2 小时前
把 AgentScope Harness 装进 RuoYi-Vue-Plus:纯 Java AI 平台的集成实践
java·人工智能·agent
蜗牛互联网2 小时前
MongoDB Atlas Agent Engine之后,如何用版本门禁防止陈旧写入
java·数据库·人工智能·后端·mongodb
长弓三石2 小时前
企业级智能体的权限到底怎么落地?以 BizBuddy 为例
java·人工智能·agent