You need to enable JavaScript to run this app.
导航

search

最近更新时间2024.04.16 13:11:53

首次发布时间2023.12.08 10:47:34

概述

search 用于在当前 Index 进行检索,支持向量检索、标量检索、标量过滤检索。

  • 当请求参数配置向量 VectorOrder 对象中的 vector 字段,表示根据向量字段名称进行向量检索。
    • filter 参数配置时,表示标量过滤检索。
    • filter 参数没有配置时,表示基于向量字段名称的纯向量检索。
  • 当请求参数配置向量 VectorOrder 对象中的主键 id,表示根据主键 id 进行向量检索。
    • filter 参数配置时,表示标量过滤检索。
    • filter 参数没有配置时,表示基于主键 id 的纯向量检索。
  • 当请求参数配置标量 ScalarOrder 对象中的 fieldName 和 order,表示根据标量字段名称进行标量排序检索。filter 参数可选配置,均表示标量检索。

说明

Collection 数据写入/删除后,Index 数据更新时间最长滞后 20s,不能立即在 Index 检索到。

请求参数

请求参数是 SearchParam,SearchParam 实例包含的参数如下表所示。

参数

类型

是否必选

默认值

参数说明

VectorOrder

map

VectorOrder:参数 vector(与sparse_vectors组合) 和 id 两者二选一进行配置。

  • vector:向量字段名称,该场景即 search_by_vector,根据向量字段名称,搜索与其相似的向量。
  • sparse_vectors:稀疏向量,仅对 search_by_vector 的混合索引必传。
  • id:主键 id,该场景即 search_by_id,根据主键 id,搜索与其相似的向量。

说明

对于使用了 hnsw-hybrid 算法的混合索引,暂时不支持基于 id 进行检索。

ScalarOrder

map

指定标量 ScalarOrder 对象,参数 fieldName 和 order 需要同时配置

  • fieldName:用于做排序的标量字段名称。
  • order:排序方式,Order.Asc(升序)、Order.Desc(降序)。

filter

map

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

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

limit

int

10

检索结果数量,最大5000个。

dense_weight

float

0.5

对于混合检索,dense_weight 用于控制稠密向量在检索中的权重。范围为[0.2,1]。仅在检索的索引为混合索引时有效。

outputFields

list<string>

过滤字段,指定要返回的标量或向量字段列表。

  • outputFields 不传时,返回所有的标量字段,不返回向量字段。
  • outputFields 为空列表时,不返回 fields 字段。
  • outputFields 格式错误或者过滤字段不是 collection 里的字段时, 接口返回错误。

如果索引的距离方式为cosine,向量字段返回的向量是归一化后的向量。

partition

string/int

"default"

子索引名称,类型与 partitionBy 的 fieldType 一致,字段值对应 partitionBy 的 fieldValue。

  • fieldType 为 int64,list<int64> 时,partition 输入类型为 int64。
  • fieldType 为 string,list<string> 时,partition 输入类型为 string,格式要求 "^[a-zA-Z0-9._]+$"。

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
}

georange

支持地理距离范围筛选 。
指定经纬度字段,以center为中心,筛选出地表距离在radius范围内的数据。

// 距离center地表距离 radius 内
{
  "op": "georange",
  "field": ["longitude", "latitude"],
  "center": [100.12312, 22.4324],
  "radius": 50.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 index = vikingDBService.getIndex("javaSDKTest", "test_index");
        List<String> outputField = new ArrayList<>();
        outputField.add("doc_id");
        outputField.add("like");
        outputField.add("text_vector");
        HashMap<String, Object> filter = new HashMap<>();
        filter.put("op", "range");
        filter.put("field", "price");
        filter.put("lt", 4);
        List<DataObject> datas = index.search(new SearchParam()
                                                .setVectorOrder(new VectorOrder().setVector(genRandomVector(12)).setSparseVector({"hello": 0.34, "world": 0.03, "!": 0.11}).build())
                                                .setOutputFields(outputField)
                                                .setPartition(3)
                                                .setFilter(filter)
                                                .setLimit(2)
                                                .build());

        System.out.println(datas);
        
        
        
        Index index = vikingDBService.getIndex("javaSDKTest", "test_index");
        List<DataObject> datas = index.search(new SearchParam()
                                                .setVectorOrder(new VectorOrder().setId("22").build())
                                                .setPartition(3)
                                                .build());

        System.out.println(datas);
        
        Index index = vikingDBService.getIndex("javaSDKTest", "test_index");
        List<DataObject> datas = index.search(new SearchParam()
                                                .setScalarOrder(new ScalarOrder("price", Order.Asc).build())
                                                .setLimit(2)
                                                .setPartition(3)
                                                .build());
        System.out.println(datas);
        

返回值

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

属性

说明

id

主键 id。

fields

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

score

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