向量数据相似性检索

最近更新时间:2025-02-24 19:45:02

我的收藏

功能介绍

基于相似度匹配的查询方式,search() 接口根据输入的1到 N 向量数据,查找存储在数据库中的相似性向量,返回指定的 Top K 个最相似的文档。
public List<List<Document>> search(String database, String collection, SearchByVectorParam param)

使用示例

基于向量数据检索
基于二进制数据检索
支持 json 类型的数据检索
如下示例,检索与向量数据(0.3123, 0.43, 0.213)与(0.5123, 0.63, 0.413)的相似性数据。
// search by filter
System.out.println("-searchByFilter----------------------");
SearchByVectorParam searchByVectorParam = SearchByVectorParam.newBuilder()
.withVectors(Arrays.asList(Arrays.asList(0.3123, 0.43, 0.213), Arrays.asList(0.5123, 0.63, 0.413)))
// 若使用 HNSW 索引,则需要指定参数 ef,ef 越大,召回率越高,但也会影响检索速度
.withParams(new HNSWSearchParams(200))
// 设置是否输出向量字段
.withRetrieveVector(true)
// 指定返回的最相似的 Top K 的 K 值
.withLimit(2)
// 设置查询
.withRadius(0.6f)
// 过滤所需的结果              
// .withFilter(new Filter("bookName=\\"三国演义\\""))
.withFilter("bookName=\\"三国演义\\"")
// 指定返回的 fields               
.withOutputFields(Arrays.asList("id", "bookName"))
.build();
List<List<Document>> svDocs = client.search("db-test", "book-vector", searchByVectorParam);
int i = 0;
for (List<Document> docs : svDocs) {
System.out.println("\\tres: " + i++);
for (Document doc : docs) {
System.out.println("\\tres: " + doc.toString());
}
}
输出信息,如下所示。
res: 0
res: {"id":"0001","vector":[0.21230000257492065,0.20999999344348907,0.21299999952316284],"sparse_vector":[[2,0.96],[5,0.53],[100,0.443]],"score":0.9617376327514648,"bookName":"三国演义"}
res: 1
res: {"id":"0001","vector":[0.21230000257492065,0.20999999344348907,0.21299999952316284],"sparse_vector":[[2,0.96],[5,0.53],[100,0.443]],"score":0.9846718311309814,"bookName":"三国演义"}
如下示例,输入二进制数据,在二进制集合,检索与其相似的数据。
SearchByVectorParam searchByVectorParam = SearchByVectorParam.newBuilder()
.addVector(BinaryUtils.binaryToUint8(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)))
// 指定 Top K 的 K 值
.withLimit(10)
.build();
List<List<Document>> svDocs = client.search("db-test", "book-vector", searchByVectorParam);
int i = 0;
for (List<Document> docs : svDocs) {
System.out.println("\\tres: " + i++);
for (Document doc : docs) {
System.out.println("\\tres: " + doc.toString());
}
}
输出信息,如下所示。
res: 0
res: {"id":"0004","score":8.0,"author":"罗贯中","bookName":"三国演义"}
res: {"id":"0001","score":9.0,"bookName":"西游记","author":"吴承恩"}
res: {"id":"0003","score":9.0,"author":"罗贯中","bookName":"三国演义"}
res: {"id":"0005","score":9.0,"author":"罗贯中","bookName":"三国演义"}
res: {"id":"0002","score":10.0,"author":"吴承恩","bookName":"西游记"}
如下示例,输入二进制数据,在二进制集合,检索与其相似的数据。
SearchByVectorParam searchByVectorParam = SearchByVectorParam.newBuilder()
.withVectors(Arrays.asList(Arrays.asList(0.3123, 0.43, 0.213), Arrays.asList(0.5123, 0.63, 0.413)))
// 若使用 HNSW 索引,则需要指定参数ef,ef越大,召回率越高,但也会影响检索速度
.withParams(new HNSWSearchParams(100))
.withFilter("bookInfo.bookName=\\"西游记\\"")
.withOutputFields(Arrays.asList("id", "bookInfo"))
// 指定 Top K 的 K 值
.withLimit(10)
.build();
// 输出相似性检索结果,检索结果为二维数组,每一位为一组返回结果,分别对应 search 时指定的多个向量
List<List<Document>> svDocs = client.search("db-test", "coll-json-autoid", searchByVectorParam);
i = 0;
for (List<Document> docs : svDocs) {
System.out.println("\\tres: " + i);
i++;
for (Document doc : docs) {
System.out.println("\\tres: " + doc.toString());
}
}
输出信息,如下所示。
res: 0
res: {"id":"229CA235-B956-E150-630F-FE1119B7C474","score":0.21057027578353882,"bookInfo":{"array":["test_6","test_7","test_3"],"author":"吴承恩","page":22,"bookName":"西游记"}}
res: {"id":"0001","score":0.20197027921676636,"bookInfo":{"author":"吴承恩","page":24,"bookName":"西游记"}}
res: 1
res: {"id":"229CA235-B956-E150-630F-FE1119B7C474","score":0.34163033962249756,"bookInfo":{"array":["test_6","test_7","test_3"],"author":"吴承恩","page":22,"bookName":"西游记"}}
res: {"id":"0001","score":0.32903027534484863,"bookInfo":{"author":"吴承恩","page":24,"bookName":"西游记"}}

请求参数

参数名
子参数
是否必选
参数含义
配置方法
database
-
指定检索数据的数据库名。
Database 命名要求如下:
只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。
长度要求:[1,128]。
collection
-
指定检索数据的集合名。
Collection 命名要求如下:
只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。
长度要求:[1,128]。
SearchByVectorParam
Vectors
表示要查询的向量列表。
数组元素数量最大为20。
Limit
指定返回最相似的 Top K 的 K 的值。
大于等于0的正整数。
Params
设置索引类型对应的检索参数。
若索引类型为 HNSW,设置检索参数 efConstruction,指定需要访问的候选向量的数目。取值范围[1,32768],默认为10。
若索引类型为 IVF 系列,设置参数 nprobe ,指定所需查询的单位数量。取值范围[1,nlist],其中 nlist 在创建 Collection 时已设置,可通过 listCollections() 查看。

RetrieveVector

标识是否需要返回检索结果的向量值。
取值如下所示:
true:需要。
false:不需要。默认为 false。
Limit
指定返回最相似的 Top K 的 K 的值。
大于等于0的正整数。
Filter
设置标量字段的 Filter 表达式,过滤所需的数据。
使用创建 CollectionView 指定的 Filter 索引的字段设置查询过滤表达式。Filter 表达式格式为 <field_name><operator><value>,多个表达式之间支持 and(与)、or(或)、not(非)关系。具体信息,请参见 Filter 条件表达式。其中:
<field_name>:表示要过滤的字段名。
<operator>:表示要使用的运算符。
string :匹配单个字符串值(=)、排除单个字符串值(!=)、匹配任意一个字符串值(in)、排除所有字符串值(not in)。其对应的 Value 必须使用英文双引号括起来。
uint64:大于(>)、大于等于(>=)、等于(=)、小于(<)、小于等于(<=)、不等于(!=)。例如:expired_time > 1623388524。
array:数组类型,包含数组元素之一(include)、排除数组元素之一(exclude)、全包含数组元素(include all)。例如,name include (\\"Bob\\", \\"Jack\\")。
json:json 类型的 Filter 表达式语法和 json 字段的键值类型保持一致。若访问 Json 对象中的键,使用点(.)符号连接。例如:Json 类型的字段 bookInfo ,其键 bookName 的 Filter 表达式如下所示。更多信息,请参见Json 类型表达式
.withFilter("bookInfo.bookName=\\"三国演义\\"")
<value>:表示要匹配的值。
示例:new Filter("author=\\"jerry\\"").and("page>20")
Radius
指定相似性检索半径。
IP:取值范围 (-∞, +∞),返回相似性计算得分 score >= Radius 的文档。
COSINE:取值范围 [-1, 1],返回相似性计算得分 score >= Radius 的文档。
L2:取值范围 [0, +∞),返回相似性计算得分 score <= Radius 的文
OutputFields
配置需返回的字段。
以数组形式配置需返回的字段。若不配置,返回所有字段。
说明:
OutputFields RetrieveVector 参数均可以配置是否输出向量值,二者任意一个配置需输出向量字段,则将输出向量字段。

出参描述

说明:
输出结果的顺序,与搜索时设置的 vectors 配置的向量值的顺序一致。
每一个查询结果都返回 Top K 条相似度计算的结果。其中,K 为 limit 设置的数值,如果插入的数据不足 K 条,则返回实际插入的 Document 数量。
检索结果会按照与查询向量的相似程度进行排列,相似度最高的结果会排在最前面,相似度最低的结果则排在最后面。
res: 0
res: {"id":"0001","vector":[0.21230000257492065,0.20999999344348907,0.21299999952316284],"sparse_vector":[[2,0.96],[5,0.53],[100,0.443]],"score":0.9617376327514648,"bookName":"三国演义"}
res: 1
res: {"id":"0001","vector":[0.21230000257492065,0.20999999344348907,0.21299999952316284],"sparse_vector":[[2,0.96],[5,0.53],[100,0.443]],"score":0.9846718311309814,"bookName":"三国演义"}
参数名
参数含义
id
Document 的 ID 信息。
vector
Document 的向量值。
score
表示查询向量与检索结果向量之间的相似性计算分数。欧式距离(L2)与汉明距离(Hamming Distance)计算所得的分数越小与搜索值越相似;而余弦相似度(COSINE)与内积(IP) 计算所得的分数越大与搜索值越相似。
author、page、section
Document 其他自定义的标量字段。