All Products
Search
Document Center

OpenSearch:Multi-query

Last Updated:Feb 27, 2024

URL

/vector-service/multi-query

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

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

  • 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

Signature method

You can use the following method to calculate the request signature. 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.

N/A

string

Yes

indexName

The index name. You must specify an index name in SearchRequest or in each query.

N/A

string

No

queries

The list of queries.

N/A

list[Query]

Yes

topK

The number of returned results.

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

filter

The filter expression.

""

string

No

sort

The sort expression.

""

string

No

  • Query

Parameter

Description

Default value

Type

Required

indexName

The index name. You must specify an index name in SearchRequest or in each query.

N/A

string

No

vector

The vector data to be queried. Multiple vectors can be tiled.

N/A

list[float]

No

vectorCount

The number of vectors specified in the vector parameter.

1

1

No

topK

The number of returned results.

100

int

No

namespace

The namespace of the vector data.

""

string

No

sparseData

The sparse vector data to be queried.

N/A

SparseData

No

weight

The weight of the query.

1.0

float

No

searchParams

The parameters that are used to query data.

""

string

No

scoreThreshold

The threshold score used to filter the results. If the score is the squared Euclidean distance, only the documents whose squared Euclidean distance is smaller 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.

N/A

float

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 default value of this parameter is the number of indexes.

list[int]

No

indices

The indexes of the elements in ascending order.

N/A

list[int]

Yes

values

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

N/A

list[float]

Yes

Response parameters

  • Returned results

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.

Type

namespace

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

string

Example

Query by using multiple namespaces

Sample request:

{
    "tableName": "gist",
    "indexName": "vec_index",
    "queries": [
        {
            "vector": [
                0.1,
                0.2,
                0.3
            ],
            "namespace": "space_a"
        },
        {
            "vector": [
                0.4,
                0.5,
                0.6
            ],
            "namespace": "space_b"
        }
    ],
    "topK": 3,
    "includeVector": true
}

Sample response:

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

Hybrid query by using multiple indexes

Sample request:

{
    "tableName": "gist",
    "queries": [
        {
            "indexName": "content_vec",
            "vector": [
                0.1,
                0.2,
                0.3
            ],
            "sparseData": {
                "count": [
                    3
                ],
                "indices": [
                    102,
                    405,
                    503
                ],
                "values": [
                    0.32,
                    0.94,
                    0.25
                ]
            },
            "weight": 0.7,
            "namespace": "space_a"
        },
        {
            "indexName": "title_vec",
            "vector": [
                0.4,
                0.5,
                0.6
            ],
            "sparseData": {
                "count": [
                    2
                ],
                "indices": [
                    203,
                    709
                ],
                "values": [
                    0.98,
                    0.08
                ]
            },
            "weight": 0.3,
            "namespace": "space_b"
        }
    ],
    "topK": 3,
    "includeVector": true
}