精确查询

最近更新时间:2024-09-29 15:47:41

我的收藏

接口定义

基于精确匹配的查询方式,query() 用于精确查找与查询条件完全匹配的向量。
public List<DocumentSet> query(CollectionViewQueryParam param)

使用示例

说明:
支持根据主键 id(Document ID),搭配自定义的标量字段的 Filter 表达式一并查询数据。
支持指定查询起始位置 offset 和返回数量 limit,精确检索查询区间内的数据,实现数据 SCAN 能力。
// link database
Database db = client.database("db-test");
// link collection
Collection collection = db.collection("book-vector");
// Filter 表达式
Filter filterParam = new Filter(Filter.in("bookName", Arrays.asList("三国演义","西游记"))) .and(Filter.exclude("tags", Arrays.asList("曹操","诸葛亮")));
// query
QueryParam queryParam = QueryParam.newBuilder()
.withDocumentIds(Arrays.asList("0001", "0002", "0003"))
// 使用 filter 过滤数据
        .withFilter(filterParam)
        // limit 限制返回行数,默认为 1,取值为 0 到 16384 之间
        .withLimit(2)
        // 偏移
        .withOffset(0)
        // 指定返回的 fields
        .withOutputFields(Arrays.asList("id", "bookName")
// 是否返回 vector 数据
.withRetrieveVector(true)
.build();
List<Document> qdos = collection.query(queryParam);
for (Document doc : qdos) {
System.out.println("\\tres: " + doc.toString());
}

入参描述

参数
是否必选
参数含义
参数配置
DocumentIds
表示要查询的文档的所有 ID。
每个 ID 长度限制为[1,128]。支持批量查询,数组元素范围[1,20]。
RetrieveVector
标识是否需要返回检索结果的向量值。
true:需要。
false:不需要。默认为 false。
Filter
设置 Filter 表达式。
使用创建 CollectionView 指定的 Filter 索引的字段设置查询过滤表达式。Filter 表达式格式为 <field_name><operator><value>,多个表达式之间支持 and(与)、or(或)、not(非)关系。具体信息,请参见 混合检索。其中:
<field_name>:表示要过滤的字段名。
<operator>:表示要使用的运算符。
string :匹配单个字符串值(=)、排除单个字符串值(!=)、匹配任意一个字符串值(in)、排除所有字符串值(not in)。其对应的 Value 必须使用英文双引号括起来。
uint64:大于(>)、大于等于(>=)、等于(=)、小于(<)、小于等于(<=)。例如:expired_time > 1623388524。
array:数组类型,包含数组元素之一(include)、排除数组元素之一(exclude)、全包含数组元素(include all)。例如,name include (\\"Bob\\", \\"Jack\\")。
<value>:表示要匹配的值。
示例:Filter('author="jerry"').And('page>20')
Limit
每页返回的 Document 数量。


每页返回的 Document 数量。默认为 1。
数据类型:uint 64
取值范围:[1,16384]
注意:
若使用 query 检索数据时,不配置 documentIds filter 参数,则必须配置 offsetlimit 参数,返回从 offset 开始的 limit 条数据,避免遍历所有数据而浪费不必要的资源。
Offset
设置分页偏移量,用于控制分页查询返回结果的起始位置,方便用户对数据进行分页展示和浏览。
取值要求如下:
取值:为 limit 整数倍。
计算公式:offset=limit*(page-1)。
例如:当 limit = 10,page = 2 时,分页偏移量 offset = 10 * (2 - 1) = 10,表示从查询结果的第 11 条记录开始返回数据。
OutputFields
配置需返回的字段。
以数组形式配置需返回的字段。若不配置,返回所有字段。
说明:
OutputFields RetrieveVector 参数均可以配置是否输出向量值,二者任意一个配置需输出向量字段,则将输出向量字段。

出参描述

res: {"id":"0002","vector":[0.21230000257492065,0.2199999988079071,0.21299999952316284],"sparse_vector":[[2,0.97],[5,0.54],[100,0.444]],"bookName":"西游记"}
参数名
参数含义
id
Document 的 ID 信息。
vector
Document 的向量值。
other
Document 自定义扩展的标量字段。