向量查询

功能说明

向量查询是指用户将生成好的向量数据导入到向量检索版实例中,进行向量检索的查询方式。

:若您没有向量模型对您的数据生成向量,可以通过向量检索版提供的图片向量化文本向量化的功能将您的数据通过内置的向量模型生成向量,再通过预测查询进行检索。

URL

/vector-service/query

  • 以上 URL 省略了请求Header参数及编码等因素。

  • 以上 URL 中省略了访问应用的 host 地址。

  • 以上URL中拼接的所有查询参数,请查看下方“查询参数”的参数定义、使用方式及样例。

请求协议

HTTP

请求方式

POST

支持格式

JSON

签名机制

可用以下方法计算签名(authorization

参数

类型

描述

accessUserName

string

用户名,可在实例详情页>网络信息查看

accessPassWord

string

密码,可在实例详情页>网络信息修改

import com.aliyun.darabonba.encode.Encoder;
import com.aliyun.darabonbastring.Client;

public class GenerateAuthorization {

 public static void main(String[] args) throws Exception {
 String accessUserName = "username";
 String accessPassWord = "password";
 String realmStr = "" + accessUserName + ":" + accessPassWord + "";
 String authorization = Encoder.base64EncodeToString(Client.toBytes(realmStr, "UTF-8"));
 System.out.println(authorization);
		}
}

authorization正确返回格式:

cm9vdDp******mdhbA==

使用HTTP请求设置authorization参数时需加上Basic前缀

示例:(加在header中)

authorization: Basic cm9vdDp******mdhbA==

请求body语法

  • 参数名称

    描述

    默认值

    类型

    是否必须

    tableName

    查询的表名

    string

    vector

    查询的向量数据

    list[float]

    vectorCount

    vector字段中包含的向量个数

    1

    int

    namespace

    查询向量的空间

    ""

    string

    topK

    返回个数

    100

    int

    includeVector

    是否返回文档中的向量信息

    false

    bool

    outputFields

    需要返回值的字段列表

    []

    list[string]

    order

    排序顺序:ASC(升序);DESC(降序)。

    ASC

    string

    searchParams

    查询参数

    ""

    string

    filter

    过滤表达式

    ""

    string

    scoreThreshold

    分数过滤, 使用欧式距离时,只返回小于scoreThreshold的结果。使用内积时,只返回大于scoreThreshold的结果

    默认不过滤

    float

    sort

    排序表达式

    如果需要向量相似度和其他的字段结合排序,可通过__vs_vector_score__ 获取向量相似度得分,例如:需要用create_gmt和向量相似度得分排序,可以在sort里写“-create_gmt;+_vs_vector_score__ ”

    ""

    string

    sorts

    多维排序,配置sorts后,结果中的score字段会变成多值字段,对应每一维排序的分数。

    order 排序顺序。ASC:升序 DESC: 降序

    expression 表达式

    []

    List<Sort>

  • Sort:

    参数名称

    描述

    默认值

    类型

    是否必须

    order

    排序顺序。

    ASC:升序

    DESC:降序

    ""

    string

    expression

    表达式

    ""

    string

单向量查询

{
    "tableName": "gist",
    "vector": [
        0.1,
        0.2,
        0.3
    ],
    "topK": 3,
    "sorts": [
      {
      "order": "DESC",
      "expression": "float(cate_id) + 1.0"
      },
      {
      "order": "ASC",
      "expression": "__vs_vector_score__ + 0.5"
      }
     ]
    "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
    "includeVector": true
}

namespace查询

OpenSearch向量检索版支持通过namespace将索引进行分区,为向量索引配置namespace后,可在指定namespace内查询,因此不同的查询请求可以搜索不同的索引子集。

注意:设置namespace后,查询时必须指定namespace。

{
    "tableName": "gist",
    "namespace": "space_b",
    "vector": [
        0.1,
        0.2
    ],
    "topK": 3,
    "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
    "includeVector": true
}

namespace查询

为向量索引配置namespace后,支持在多个namespace中查询。

{
    "tableName": "gist",
    "queries": [
        {
            "vector": [
                0.1,
                0.2,
                0.3
            ],
            "namespace": "space_a"
        },
        {
            "vector": [
                0.4,
                0.5,
                0.6
            ],
            "namespace": "space_b"
        }
    ],
    "topK": 3,
    "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
    "includeVector": true
}

带过滤查询

支持使用filter表达式,指定过滤条件进行查询。

{
    "tableName": "gist",
    "vector": [
        0.1,
        0.2
    ],
    "topK": 3,
    "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
    "filter": "a > 10",
    "includeVector": true,
    "outputFields": [
        "a",
        "b"
    ]
}

多向量查询

可以同时查询多个向量,这些向量的结果会合并排序,返回最终的topK个结果。例如向量维度为2,同时查询两个向量:

{
    "tableName": "gist",
    "vector": [
        0.1,
        0.2,
        0.3,
        0.4
    ],
    "vectorCount": 2,
    "topK": 3,
    "searchParams":"{\"qc.searcher.scan_ratio\":0.01}",
    "includeVector": true,
    "outputFields": [
        "a",
        "b"
    ]
}

返回参数

字段名称

描述

类型

result

结果列表

list[Item]

totalCount

result中的个数

int

totalTime

引擎处理耗时,单位ms

float

errorCode

错误码,有错误时才有该字段

int

errorMsg

错误信息,有错误时才有该字段

string

item定义:

字段名称

描述

类型

score

距离分

float

fields

字段名称和对应的值

map<string, FieldType>

vector

向量值

list[float]

id

主键值,类型为所定义的字段类型

FieldType

namespace

向量的名称空间,如果设置了namespace会返回该字段

string

示例

{
    "result": [
        {
            "id": 1,
            "score": 1.0508723258972169,
            "vector": [
                0.1,
                0.2,
                0.3
            ]
        },
        {
            "id": 2,
            "score": 1.0329746007919312,
            "vector": [
                0.2,
                0.2,
                0.3
            ]
        },
        {
            "id": 3,
            "score": 0.980593204498291,
            "vector": [
                0.3,
                0.2,
                0.3
            ]
        }
    ],
    "totalCount": 3,
    "totalTime": 2.943
}