All Products
Search
Document Center

OpenSearch:Sparse vector-based query

Last Updated:Mar 26, 2025

URL

/vector-service/query

  • The sample URL omits information such as parameters in request headers and the encoding method.

  • The sample URL also omits the endpoint that is used to connect to an OpenSearch instance.

  • For more information about the definitions, usage, and example values of all the request parameters that are concatenated in the preceding URL, see the "Request parameters" section of this topic.

Protocol

HTTP

Request method

POST

Supported format

JSON

Request signature

You can calculate the request signature by using the parameters described in the following table. The request signature is stored in the authorization header.

Parameter

Type

Description

accessUserName

string

The username. You can view the username in the API Endpoint section of the Instance Details page.

accessPassWord

string

The password. You can modify the password in the API Endpoint section of the Instance Details page.

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);
    }
}

Valid format for the value of the authorization header:

cm9vdDp******mdhbA==

You must add the Basic prefix when you specify the authorization header in an HTTP request.

Example:

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

Request parameters

  • SearchRequest

Parameter

Description

Default value

Type

Required

tableName

The name of the table to be queried.

No default value

string

Yes

indexName

The name of the dense vector index.

No default value

string

Yes

vector

The dense vector data to be queried.

No default value

list[float]

Yes

sparseData

The sparse vector data to be queried.

No default value

SparseData

Yes

vectorCount

The number of vectors specified in the vector parameter.

1

int

No

namespace

The namespace of the vector data.

""

string

No

topK

The number of results to be returned.

100

int

No

includeVector

Specifies whether to return the vector information in documents.

false

bool

No

outputFields

The fields to be returned.

[]

list[string]

No

order

The order in which the results are sorted. A value of ASC indicates the ascending order. A value of DESC indicates the descending order.

ASC

string

No

searchParams

The parameters that are used to query data.

""

string

No

filter

The filter expression.

""

string

No

scoreThreshold

The score threshold used to filter the results. If the score is the Euclidean distance, only the documents whose Euclidean distance is less than the value of the scoreThreshold parameter are returned. If the score is the inner product, only the documents whose inner product is greater than the value of the scoreThreshold parameter are returned.

By default, the results are not filtered.

float

No

sort

The sort expression.

""

string

No

  • SparseData

Parameter

Description

Default value

Type

Required

count

The number of elements in each sparse vector.

If only one sparse vector is specified, the value of this parameter is the number of indexes by default.

list[int]

No

indices

The indexes of the elements in ascending order.

No default value

list[int]

Yes

values

The values of the elements in the same order as the indexes.

No default value

list[float]

Yes

In OpenSearch Vector Search Edition, two independent multi-value fields are used to represent sparse vector subscripts and values. The number of values must be the same in the two fields. The positions of the values in the two fields have a one-to-one correspondence.

For example, the sparse vector (0, 0.5), (100, 0.9), (40, 0.3), (50, 0.7), (20, 0.6) exists. The following code shows the multi-value fields of the sparse vector:

{
  "indices": [0, 100, 40, 50, 20],
  "values": [0.5, 0.9, 0.3, 0.7, 0.6]
}

When you write or query data, subscripts must be arranged in ascending order, and values need to be adjusted based on the subscripts. The following code shows the adjusted multi-value fields:

{
  "indices": [0, 20, 40, 50, 100],
  "values": [0.5, 0.6, 0.3, 0.7, 0.9]
}

Query weight

In a hybrid query, the final score of the same document is the sum of the distance of the dense vector and the distance of the sparse vector. If you want to configure different weights for the sparse vector and dense vector, you can perform configurations based on the following code:

{
    "vector": [v * weight for v in dense_vector],
    "sparseData": {
        "indices": sparse_data["indices"],
        "values": [v * (1 - weight) for v in sparse_data["values"]]
    }
}

Sample request

{
    "tableName": "in0",
    "indexName": "vector",
    "vector": [
        0.1,
        0.2,
        0.3,
        0.4,
        0.5
    ],
    "sparseData": {
        "indices": [
            0,
            2
        ],
        "values": [
            1.2,
            2.4
        ]
    },
    "topK": 2,
    "order": "DESC"
}

Response parameters

Parameter

Description

Type

result

The returned results.

list[Item]

totalCount

The number of results.

int

totalTime

The response time. Unit: milliseconds.

float

errorCode

The error code returned if the request failed.

int

errorMsg

The error message returned if the request failed.

string

Item

Parameter

Description

Type

score

The score of the vector.

float

fields

The fields and the corresponding values.

map<string, FieldType>

vector

The vector value.

list[float]

id

The primary key value. The value is of the defined data type.

FieldType

namespace

The namespace of the vector. This parameter is returned if a namespace is configured for the vector.

string

Example

{
    "totalCount": 50,
    "result": [
        {
            "id": 99,
            "score": 0.18588095903396607,
            "__source__": 1,
            "fields": {
                "content": "Zhonghao Cat 6 Network Cable RJ45 Cat 6 Gigabit 8-Core Twisted Pair Jumper Broadband Monitoring Home Network Cable Finished Cable"
            }
        }
    ],
    "totalTime": 4.057
}