目录
概述
开始前
单向量搜索
本文介绍如何在 Zilliz Cloud 中执行近似最近邻(Approximate Nearest Neighbour,ANN)搜索和查询。搜索是指在 Collection 中查找与指定查询向量最接近的向量,查询是基于指定条件筛选出满足条件的数据。

概述
Zilliz Cloud 采用 ANN 算法来处理向量搜索请求,支持搜索并返回与指定查询向量最相似的前 K 个 Entity。为优化性能和吞吐,Zilliz Cloud 支持批量搜索,即同时指定多个查询向量并行搜索。您可以定义布尔表达式来缩小 ANN 搜索的范围。
开始前

执行 ANN 搜索或查询前,请确保已完成以下步骤:
- 已连接到创建了 Collection 的目标集群。详情请参见连接集群和创建 Collection。
 - 已下载示例数据集并已将数据插入到 Collection 中。详情请参见示例数据集和插入 Entity。
 - 阅读本指南系列时,建议下载代码示例。
 
说明
本指南系列中创建的 Collection 包含 id 主键和 vector 向量字段。如果您希望完全自定义 Collection,请参见定制 Schema、开启动态 Schema 和 JSON。
单向量搜索
单向量搜索是指搜索并返回与指定的某个查询向量最相似的前 K 个Entity。

以下是单向量搜索的示例代码:
- Python
 - NodeJS
 
with open("path/to/downloaded/medium_articles_2020_dpr.json") as f:
    data = json.load(f)
# 'client' 是 MilvusClient 实例。
res = client.search(
    collection_name="medium_articles_2020",
    data=[data["rows"][0]["title_vector"]],
    output_fields=["title"]
)
print(res)
# 输出:
# [
#     [
#         {
#             "id": 0,
#             "distance": -1.0,
#             "entity": {
#                 "title": "The Reported Mortality Rate of Coronavirus Is Not Important"
#             }
#         },
#         {
#             "id": 70,
#             "distance": -0.7525784969329834,
#             "entity": {
#                 "title": "How bad will the Coronavirus Outbreak get? \u2014 Predicting the outbreak figures"
#             }
#         },
#         {
#             "id": 160,
#             "distance": -0.7132074236869812,
#             "entity": {
#                 "title": "The Funeral Industry is a Killer"
#             }
#         },
#         {
#             "id": 111,
#             "distance": -0.6888885498046875,
#             "entity": {
#                 "title": "The role of AI in web-based ADA and WCAG compliance"
#             }
#         },
#         {
#             "id": 196,
#             "distance": -0.6882869601249695,
#             "entity": {
#                 "title": "The Question We Should Be Asking About the Cost of Youth Sports"
#             }
#         },
#         {
#             "id": 51,
#             "distance": -0.6719912886619568,
#             "entity": {
#                 "title": "What if Facebook had to pay you for the profit they are making?"
#             }
#         },
#         {
#             "id": 178,
#             "distance": -0.6699185371398926,
#             "entity": {
#                 "title": "Is The Environmental Damage Due To Cruise Ships Irreversible?"
#             }
#         },
#         {
#             "id": 47,
#             "distance": -0.6680259704589844,
#             "entity": {
#                 "title": "What Happens When the Google Cookie Crumbles?"
#             }
#         },
#         {
#             "id": 135,
#             "distance": -0.6597772836685181,
#             "entity": {
#                 "title": "How to Manage Risk as a Product Manager"
#             }
#         }
#     ]
# ] 
您可以在搜索请求中引用搜索参数,并指定查询向量、向量字段名称、返回结果限制以及其他相关参数。以上代码搜索与指定查询向量最相近的 10 条 Entity,并返回各 Entity 的主键、距离等信息。


















