Gunakan operasi QueryVectors untuk melakukan pencarian kemiripan vektor.
Permissions
Secara default, Akun Alibaba Cloud memiliki izin penuh, sedangkan pengguna RAM atau peran RAM tidak memiliki izin apa pun. Pemilik Akun Alibaba Cloud atau administrator harus memberikan izin melalui RAM policy atau bucket policy.
API | Action | Description |
QueryVectors |
| Queries vector data. |
Request syntax
Tingkat recall untuk panggilanQueryVectorsmungkin rendah hingga 30 detik setelah indeks vektor dibuat. Data yang ditulis menggunakan operasiPutVectorsakan tersedia untuk kueri dalam waktu sekitar 2 hingga 3 detik.
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
Operasi ini hanya menggunakan header permintaan umum. Untuk informasi selengkapnya, lihat 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
Berikut adalah contoh ekspresi filter umum. Jika operator tidak ditentukan, $eq digunakan secara 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
Operasi ini hanya menggunakan header respons umum. Untuk informasi selengkapnya, lihat 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
Operasi QueryVectors tersedia di SDK berikut:
ossutil CLI
Anda dapat menggunakan perintah query-vectors pada alat baris perintah ossutil untuk menjalankan operasi ini.
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. |