milvus数据库搜索

一、向量相似度搜索
在Milvus中进行向量相似度搜索时,会计算查询向量和集合中具有指定相似性度量的向量之间的距离,并返回最相似的结果。通过指定一个布尔表达式来过滤标量字段或主键字段,您可以执行混合搜索。

1.加载集合
执行操作的前提是集合加载到内存。

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.load()

2.准备搜索参数
搜索参数要适应你的搜索场景。

search_params = {
    "metric_type": "L2", 
    "offset": 0, 
    "ignore_growing": False, 
    "params": {"nprobe": 10}#适合 IVF_FLAT index
}

params的可选参数和值如下:

nprobe Indicates the number of cluster units to search. This parameter is available only when index_type is set to IVF_FLAT, IVF_SQ8, or IVF_PQ. The value should be less than nlist specified for the index-building process.

ef Indicates the search scope. This parameter is available only when index_type is set to HNSW. The value should be within the range from top_k to 32768.

radius Indicates the angle where the vector with the least similarity resides.

range_filter Indicates the filter used to filter vector field values whose similarity to the query vector falls into a specific range.

3.进行向量搜索

# 使用集合对象的 search 方法来进行向量检索
results = collection.search(
    data=[[0.1, 0.2]],  # 查询向量
    anns_field="book_intro",  # 指定用于检索的字段
    param=search_params,  # 检索参数
    limit=10,  # 返回结果数量的限制
    expr=None,  # 查询表达式
    output_fields=['title'],  # 指定要从搜索结果中检索的字段
    consistency_level="Strong"  # 一致性级别
)

# 获取搜索结果中最相似的文档 IDs
results[0].ids

# 获取搜索结果中的距离值
results[0].distances

# 获取第一个匹配的文档
hit = results[0][0]

# 从匹配的文档中获取 'title' 字段的值
hit.entity.get('title')

二、混合搜索

混合搜索是使用属性过滤的向量搜索。通过指定过滤标量字段或主键字段的布尔表达式,来先限定搜索范围。
1.加载集合
2.进行混合向量搜索
其实也就是在前面的搜索配置加了个布尔表达式

search_param = {
  "data": [[0.1, 0.2]],
  "anns_field": "book_intro",
  "param": {"metric_type": "L2", "params": {"nprobe": 10}, "offset": 0},
  "limit": 10,
  "expr": "word_count <= 11000",
}
res = collection.search(**search_param)

3.检查搜索结果

assert len(res) == 1 # 断言
hits = res[0]
assert len(hits) == 2
print(f"- Total hits: {len(hits)}, hits ids: {hits.ids} ")
print(f"- Top1 hit id: {hits[0].id}, distance: {hits[0].distance}, score: {hits[0].score} ")

三、范围搜索
1.加载集合
2.定义范围搜索参数
l2度量:

param = {
    # use `L2` as the metric to calculate the distance
    "metric_type": "L2",
    "params": {
        # search for vectors with a distance smaller than 1.0
        "radius": 1.0,# 半径 只有距离查询向量的距离小于半径值的向量才会被返回作为检索结果。
        # filter out vectors with a distance smaller than or equal to 0.8
        "range_filter" : 0.8 #大于或等于指定值的向量将被返回作为检索结果
    }
}

内积ip度量:

param = {
    # use `IP` as the metric to calculate the distance
    "metric_type": "IP",
    "params": {
        # search for vectors with a distance greater than 0.8
        "radius": 0.8,
        # filter out most similar vectors with a distance greater than or equal to 1.0
        "range_filter" : 1.0
    }
}

3.执行范围搜索

res = collection.search(
    data=[[0.3785311281681061,0.2960498034954071]], # query vector
    anns_field='book_intro', # vector field name
    param=param, # search parameters defined in step 2
    limit=5 # number of results to return
)

print(res)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Azure Open AI Embedding是一种嵌入模型,通过使用Azure平台上的开放AI服务,可以将文本转换为高维向量表示。这种嵌入模型可以用于自然语言处理、推荐系统等领域,帮助我们更好地理解和处理文本数据。在使用Azure Open AI Embedding时,我们可以利用Azure的强大计算资源和高效的API接口,快速获取到所需的文本嵌入表示。 Milvus本地向量数据库是一种针对大规模向量数据的高性能数据库。它提供了快速的向量相似度搜索和存储功能,可以高效地应用于图像识别、人脸识别、文本检索等领域。在搭建Milvus本地向量数据库的单例安装和使用时,我们可以通过简单的配置和管理,快速部署本地向量检索系统,并且能够自由定制化自己的向量索引。 对于私有模型的应用,可以将Azure Open AI Embedding模型和Milvus本地向量数据库结合起来。首先,可以使用Azure Open AI Embedding模型将文本数据转换为向量表示,然后将这些向量存储到Milvus本地向量数据库中进行索引和检索。这样可以实现自己的定制化文本嵌入表示和快速的向量相似度搜索。同时,我们也可以通过对Milvus本地向量数据库进行单例安装和私有化部署,更好地保护自己的数据和模型隐私。这样的集成和应用可以帮助我们更好地理解和处理大规模文本数据,并且能够高效地进行相似度搜索和检索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灵海之森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值