You need to enable JavaScript to run this app.
文档中心
向量数据库VikingDB

向量数据库VikingDB

复制全文
下载 pdf
检索过滤能力
标量过滤
复制全文
下载 pdf
标量过滤

您可通过标量过滤器(Filter)在向量数据库中限定结果范围。
标量过滤是通用检索能力,您可以通过在检索和聚合统计的相关接口中传入filter参数来启用标量过滤能力。

参数说明

参数

类型

是否必选

默认值

参数说明

filter

map

否

None

过滤条件,详见 filter 表达式说明。

  • 默认为空,不做过滤。
  • 过滤条件包含 must、must_not、range、range_out四类查询算子,包含 and 和 or 两种对查询算子的组合。

filter的前提条件是:相应字段开启了标量索引。

filter 表达式

算子

算子说明

示例

must

针对指定字段名生效,语义为必须在 [...] 之中,即 "must in"。

JSON <br> { <br> "op": "must", <br> "field": "region", <br> "conds": ["cn", "sg"] <br> } <br>

must_not

针对指定字段名生效,语义为必须不在 [...] 之中,即 "must not in"。

JSON <br> { <br> "op": "must_not", <br> "field": "data_type", <br> "conds": [1,2,3] <br> } <br>

range

针对指定字段名生效,语义为必须在指定范围内。
配置使用gte(大于等于), gt(大于), lte(小于等于), lt(小于),用以圈定一维范围。
另外,支持用 center 和 radius 表示二维圆内范围。

JSON <br> // price 在 [100.0, 500.0) <br> { <br> "op": "range", <br> "field": "price", <br> "gte": 100.0, <br> "lt": 500.0 <br> } <br> <br> //price >= 100.0 <br> { <br> "op": "range", <br> "field": "price", <br> "gte": 100.0 <br> } <br> <br> // 以 center 为中心,半径为50的圆内 <br> { <br> "op": "range", <br> "field": ["pos_x", "pos_y"], <br> "center": [100.0, 123.4], <br> "radius": 50.0 <br> } <br>

range_out

针对指定字段名生效,语义为必须在指定范围外。配置使用gte(大于等于), gt(大于), lte(小于等于), lt(小于),用以圈定一维范围。

JSON <br> // 筛选价格低于100或高于500的商品 <br> { <br> "op": "range_out", <br> "field": "price", <br> "gt": 500.0, <br> "lt": 100.0 <br> } <br>

and

逻辑算子,针对逻辑查询需求,对多个条件取交集。

JSON <br> { <br> "op": "and", // 算子名 <br> "conds": [ // 条件列表,支持嵌套逻辑算子和 must/must_not 算子 <br> { <br> "op": "must", <br> "field": "type", <br> "conds": [1] <br> }, <br> { <br> ... // 支持>=1的任意数量的条件进行组合 <br> } <br> ] <br> } <br>

or

逻辑算子,针对逻辑查询需求,对多个条件取并集。

JSON <br> { <br> "op": "or", // 算子名 <br> "conds": [ // 条件列表,支持嵌套逻辑算子和 must/must_not 算子 <br> { <br> "op": "must", <br> "field": "type", <br> "conds": [1] <br> }, <br> { <br> ... // 支持>=1的任意数量的条件进行组合 <br> } <br> ] <br> } <br>

示例
//获取指定索引,程序初始化时调用即可,无需重复调用
Index index = vikingDBService.getIndex("test_text", "test_index_text");
// 定义输出字段列表,这些字段将在搜索结果中返回
List<String> outputFields = Arrays.asList("f_id", "city", "employee_name", "age");

// 创建一个条件列表,用于存储多个查询条件
List<Map<String, Object>> conditionsList = new ArrayList<>();

// 条件1:必须满足国家为“中国”
Map<String, Object> condition1 = new HashMap<>();
condition1.put("op", "must"); // 操作符,表示“必须”满足
condition1.put("field", "country"); // 字段名
condition1.put("conds", Arrays.asList("中国")); // 条件值
conditionsList.add(condition1); // 将条件添加到条件列表

// 条件2:必须满足城市为“成都”
Map<String, Object> condition2 = new HashMap<>();
condition2.put("op", "must"); // 操作符,表示“必须”满足
condition2.put("field", "city"); // 字段名
condition2.put("conds", Arrays.asList("成都")); // 条件值
conditionsList.add(condition2); // 将条件添加到条件列表

// 条件3:年龄必须大于25
Map<String, Object> condition3 = new HashMap<>();
condition3.put("op", "range"); // 操作符,表示范围查询
condition3.put("field", "age"); // 字段名
condition3.put("gt", 25); // 大于25岁
conditionsList.add(condition3); // 将条件添加到条件列表

// 创建过滤器,将所有条件组合在一起
Map<String, Object> filter = new HashMap<>();
filter.put("op", "and"); // 使用逻辑“与”操作符连接所有条件
filter.put("conds", conditionsList); // 将条件列表设置为过滤器的内容

// 打印过滤器内容,方便调试查看
System.out.println("Filter: " + filter);

// 执行搜索操作
List<DataObject> searchResults = index.search(new SearchParam()
                .setScalarOrder(new ScalarOrder("age", "asc").build()) // 按年龄升序排序
                .setOutputFields(outputFields) // 设置需要返回的字段
                .setFilter(filter) // 设置过滤条件
                .setLimit(20) // 限制返回结果数量为20条
                .build()); // 构建搜索参数

// 打印搜索结果
System.out.println("Search Results: " + searchResults);

返回值

Java 调用执行上面的任务,返回 List 。DataObject 实例包含的属性如下表所示。

属性

说明

id

主键 id。

fields

请求返回中的 fields 字段,是具体的数据,map 类型。

score

表示找到的向量和输入的向量的匹配程度。

最近更新时间:2026.09.08 15:30:58
这个页面对您有帮助吗?
有用
有用
无用
无用