You need to enable JavaScript to run this app.
导航
标量过滤
最近更新时间:2025.03.24 13:55:22首次发布时间:2025.03.24 13:55:22
我的收藏
有用
有用
无用
无用

您可通过标量过滤器(Filter)在向量数据库中限定结果范围。

说明

标量过滤是通用检索能力,您可以通过在检索和聚合统计的相关接口中传入filter参数来启用标量过滤能力。

参数说明

参数

类型

是否必选

默认值

参数说明

filter

map

None

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

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

注意

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

filter 表达式

算子

算子说明

示例

must

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

{
  "op": "must",
  "field": "region",
  "conds": ["cn", "sg"]
}

must_not

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

{
  "op": "must_not",
  "field": "data_type",
  "conds": [1,2,3]
}

range

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

// price 在 [100.0, 500.0)
{
  "op": "range",
  "field": "price",
  "gte": 100.0,
  "lt": 500.0
}

//price >= 100.0
{
  "op": "range",
  "field": "price",
  "gte": 100.0
}

// 以 center 为中心,半径为50的圆内
{
  "op": "range",
  "field": ["pos_x", "pos_y"],
  "center": [100.0, 123.4],
  "radius": 50.0
}

range_out

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

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

and

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

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

or

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

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

示例
# 获取指定索引,程序初始化时调用即可,无需重复调用
index = vikingdb_service.get_index("example", "example_index")
agg_result = index.search_agg(agg={'op': 'count', 'field': 'f_string', 'cond': {'gt': 0}}, filter={'op': 'range', 'field': 'f_int64', 'gt': 90})
print(agg_result.agg_op, agg_result.group_by_field, agg_result.agg_result)