Use the QueryVectors operation to perform a vector similarity search.
Permissions
By default, an Alibaba Cloud account has full permissions, whereas a RAM user or RAM role has none. The Alibaba Cloud account owner or an administrator must grant permissions by using a RAM policy or a bucket policy.
API | Action | Description |
QueryVectors |
| Queries vector data. |
Request syntax
The recall rate forQueryVectorscalls may be low for up to 30 seconds after a vector index is created. Data written by using thePutVectorsoperation becomes available for querying in approximately 2 to 3 seconds.
POST /?queryVectors HTTP/1.1
Host: examplebucket-123***456.cn-hangzhou-internal.oss-vectors.aliyuncs.com
Date: GMT Date
Authorization: SignatureValue
Content-type: application/json
{
"filter": {
"$and": [{
"type": {
"$in": ["comedy", "documentary"]
}
}, {
"year": {
"$eq": "2020"
}
}]
},
"indexName": "string",
"queryVector": {
"float32":[float]
},
"returnDistance": boolean,
"returnMetadata": boolean,
"topK": int
}Request headers
This operation uses only common request headers. For more information, see Common HTTP headers.
Request parameters
Parameter | Type | Required | Example | Description |
indexName | String | Yes | vectorindex1 | The name of the vector index. |
queryVector | Container | Yes | N/A | The query vector. The dimension must match the dimension of the vector index. |
filter | Container | No | N/A | Filters data based on metadata. The following limits apply to metadata filters:
|
returnDistance | Boolean | No | false | Specifies whether to return the similarity distance. Valid values:
|
returnMetadata | Boolean | No | false | Specifies whether to return the metadata. Valid values:
|
topK | Number | Yes | 10 | The number of most similar results to return. The value must be an integer from 1 to 500. |
Filter operators
Operator | Type | Description |
| String, Number, Boolean | Exact match (for a single value). When used on array-type metadata, the operator returns |
| String, Number, Boolean | Matches values that are not equal to the specified value. |
| Number | Greater than. |
| Number | Greater than or equal to. |
| Number | Less than. |
| Number | Less than or equal to. |
| Non-empty array of primitives | Matches any value in an array (similar to the SQL |
| Non-empty array of primitives | Does not match any of the values in the array (similar to the SQL |
| Boolean | Checks whether a metadata key exists. |
| Non-empty array of filters | Performs a logical AND operation on an array of filter conditions. |
| Non-empty array of filters | Performs a logical OR operation on an array of filter conditions. |
Filter examples
The following are common Filter expression examples. When the operator is omitted, $eq is used by default.
// Simple equality (uses $eq by default when the operator is omitted)
{"category": "finance"}
// Explicit equality / inequality
{"category": {"$eq": "finance"}}
{"category": {"$ne": "archived"}}
// Numeric comparison
{"created_year": {"$gt": 2023}}
{"created_year": {"$gte": 2024}}
{"created_year": {"$lt": 2026}}
{"created_year": {"$lte": 2025}}
// Array matching
{"language": {"$in": ["zh", "en"]}}
{"language": {"$nin": ["ja", "ko"]}}
// Existence check
{"author": {"$exists": true}}
// Logical combination
{"$and": [{"category": {"$eq": "finance"}}, {"created_year": {"$gte": 2024}}]}
{"$or": [{"category": {"$eq": "finance"}}, {"category": {"$eq": "tech"}}]}
// Multiple conditions on the same field (range)
{"score": {"$gte": 0.6, "$lte": 0.95}}Response headers
This operation uses only common response headers. For more information, see Common HTTP headers.
Response elements
Parameter | Type | Example | Description |
vectors | Array of objects | / | A list of the vector search results. |
key | String | doc-001 | The vector primary key. Parent node: vectors |
distance | float32 | 0.25 | The similarity distance between the returned vector and the query vector. A smaller value indicates a higher similarity. This parameter is returned only when Parent node: vectors |
metadata | Object | / | The complete metadata of the vector. This parameter is returned only when Parent node: vectors |
Examples
POST /?queryVectors HTTP/1.1
Host: examplebucket-123***456.cn-hangzhou-internal.oss-vectors.aliyuncs.com
Date: Thu, 17 Apr 2025 01:33:47 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218
Content-type: application/json
{
"filter": {
"$and": [{
"category": {
"$in": ["technology", "science"]
}
}, {
"year": {
"$eq": "2020"
}
}]
},
"indexName": "vectorindex1",
"queryVector": {
"float32": [0.15, 0.25, 0.35, 0.45, 0.55]
},
"returnDistance": true,
"returnMetadata": true,
"topK": 5
}Sample response
HTTP/1.1 200 OK
x-oss-request-id: 534B371674E88A4D8906****
Date: Thu, 17 Apr 2025 01:33:47 GMT
Connection: keep-alive
Server: AliyunOSS
Content-type: application/json
{
"vectors": [
{
"distance": 0.12,
"key": "doc-001",
"metadata": {
"category": ["technology", "ai"],
"title": "Introduction to Vector Search",
"year": "2020"
}
},
{
"distance": 0.25,
"key": "doc-003",
"metadata": {
"category": ["science"],
"title": "Advanced Vector Operations",
"year": "2020"
}
}
]
}SDK
The QueryVectors operation is available in the following SDKs:
ossutil CLI
You can use the query-vectors command in the ossutil command-line tool to perform this operation.
Error codes
Error code | HTTP status code | Description |
VectorIndexParameterInvalid | 400 | The request contains invalid vector index parameters. |
MalformedJson | 400 | The JSON format of the request body is invalid. |
AccessDenied | 403 | Possible reasons for this error include:
|
NoSuchVectorIndex | 404 | The specified vector index does not exist. |
QpsLimitExceeded | 503 | The request rate has exceeded the QPS limit. Your requests are being throttled. |