QueryVectors
Returns matching vector keys from a vector index ranked by vector similarity, full-text
relevance, or both. Specify queryVector to perform a vector search, specify
queryTexts to perform a full-text search, or specify both to perform a
hybrid search. Full-text search ranks results using BM25. Hybrid search combines the vector
and full-text rankings using Reciprocal Rank Fusion (RRF).
Amazon S3 Vectors ranks results by distance for a vector-only search, by BM25 score for
a full-text-only search, and by RRF score for a hybrid search. You can optionally include
computed distances, relevance scores, and vector metadata in the response. Full-text and
hybrid searches require a vector index in ENHANCED mode.
To specify the vector index, you can either use both the vector bucket name and the vector index name, or use the vector index Amazon Resource Name (ARN).
- Permissions
-
You must have the
s3vectors:QueryVectorspermission to use this operation. Additional permissions are required based on the request parameters you specify:-
With only
s3vectors:QueryVectorspermission, you can retrieve vector keys of approximate nearest neighbors and computed distances between these vectors. This permission is sufficient only when you don't setqueryTexts, don't set any metadata filters, and don't request metadata (by keeping thereturnMetadataparameter set tofalseor not specified). -
To use
queryTexts, a metadata filter, orreturnMetadataset totruewith this operation, you must have both thes3vectors:QueryVectorsands3vectors:GetVectorspermissions. If you use any of these options without thes3vectors:GetVectorspermission, Amazon S3 Vectors returns anAccessDeniedException.
-
Request Syntax
POST /QueryVectors HTTP/1.1
Content-type: application/json
{
"filter": JSON value,
"indexArn": "string",
"indexName": "string",
"nextToken": "string",
"queryMode": "string",
"queryTexts": [
{
"metadataKey": "string",
"text": "string"
}
],
"queryVector": { ... },
"returnDistance": boolean,
"returnMetadata": boolean,
"returnScores": boolean,
"topK": number,
"vectorBucketName": "string"
}
URI Request Parameters
The request does not use any URI parameters.
Request Body
The request accepts the following data in JSON format.
- filter
-
Metadata filter to apply during the query. For more information about metadata keys, see Metadata filtering in the Amazon S3 User Guide.
Type: JSON value
Required: No
- indexArn
-
The ARN of the vector index that you want to query.
Type: String
Pattern:
arn:aws[-a-z0-9]*:s3vectors:[a-z0-9-]+:[0-9]{12}:bucket/[a-z0-9][a-z0-9-.]{1,61}[a-z0-9]/index/[a-z0-9][a-z0-9-.]{1,61}[a-z0-9]Required: No
- indexName
-
The name of the vector index that you want to query.
Type: String
Length Constraints: Minimum length of 3. Maximum length of 63.
Required: No
- nextToken
-
Pagination token from a previous request. The value of this field is empty for an initial request.
Type: String
Length Constraints: Minimum length of 1. Maximum length of 4096.
Required: No
- queryMode
-
The mode to use to process the query. If you don't specify a query mode, the operation uses the mode that's currently configured for the vector index.
Valid values:
-
CLASSIC- Applies metadata filters during the vector search. You can't specifyCLASSICfor anENHANCEDindex. -
ENHANCED- Applies metadata filters before the vector search.
Type: String
Valid Values:
CLASSIC | ENHANCEDRequired: No
-
- queryTexts
-
Full-text queries for searchable metadata keys. Omit
queryVectorto run a full-text-only search, or provide both fields to run a hybrid search. Each entry must target a distinct searchable metadata key.Type: Array of QueryText objects
Array Members: Minimum number of 1 item. Maximum number of 10 items.
Required: No
- queryVector
-
The query vector for vector similarity search. The query vector must have the same dimension as the vector index. For example, if the vector index has 384 dimensions, the query vector must also have 384 dimensions. You can omit this parameter when you supply
queryTextsto perform a full-text-only search. A request must includequeryVector,queryTexts, or both.Type: VectorData object
Note: This object is a Union. Only one member of this object can be specified or returned.
Required: No
- returnDistance
-
Indicates whether to include the computed distance in the response. The default value is
false.Type: Boolean
Required: No
- returnMetadata
-
Indicates whether to include metadata in the response. The default value is
false.Type: Boolean
Required: No
- returnScores
-
Indicates whether to include relevance scores in the response. Full-text-only results can include BM25 scores. Hybrid results can also include a Reciprocal Rank Fusion (RRF) score and vector distance. This setting doesn't add scores to vector-only requests. The default value is
false.Type: Boolean
Required: No
- topK
-
The number of results to return for each query.
Type: Integer
Valid Range: Minimum value of 1.
Required: Yes
- vectorBucketName
-
The name of the vector bucket that contains the vector index.
Type: String
Length Constraints: Minimum length of 3. Maximum length of 63.
Required: No
Response Syntax
HTTP/1.1 200
Content-type: application/json
{
"distanceMetric": "string",
"nextToken": "string",
"vectors": [
{
"distance": number,
"key": "string",
"metadata": JSON value,
"scores": {
"bm25Scores": [
{
"metadataKey": "string",
"score": number
}
],
"fused": number,
"vectorDistance": number
}
}
]
}
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The following data is returned in JSON format by the service.
- distanceMetric
-
The distance metric that was used for the similarity search calculation. This is the same distance metric that was configured for the vector index when it was created.
Type: String
Valid Values:
euclidean | cosine - nextToken
-
Pagination token to be used in the subsequent page request. The field is empty if no further pagination is required.
Type: String
Length Constraints: Minimum length of 1. Maximum length of 4096.
- vectors
-
The matching vector results in ranked order. Results are ranked by vector distance for a vector-only search, by BM25 relevance for a full-text-only search, and by RRF score for a hybrid search.
Type: Array of QueryOutputVector objects
Errors
- AccessDeniedException
-
Access denied.
HTTP Status Code: 403
- InternalServerException
-
The request failed due to an internal server error.
HTTP Status Code: 500
- KmsDisabledException
-
The specified AWS KMS key isn't enabled.
HTTP Status Code: 400
- KmsInvalidKeyUsageException
-
The request was rejected for one of the following reasons:
-
The
KeyUsagevalue of the KMS key is incompatible with the API operation. -
The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (
KeySpec).
For more information, see InvalidKeyUsageException in the AWS Key Management Service API Reference.
HTTP Status Code: 400
-
- KmsInvalidStateException
-
The key state of the KMS key isn't compatible with the operation.
For more information, see KMSInvalidStateException in the AWS Key Management Service API Reference.
HTTP Status Code: 400
- KmsNotFoundException
-
The KMS key can't be found.
HTTP Status Code: 400
- NotFoundException
-
The request was rejected because the specified resource can't be found.
HTTP Status Code: 404
- RequestTimeoutException
-
The request timed out. Retry your request.
HTTP Status Code: 408
- ServiceUnavailableException
-
The service is unavailable. Wait briefly and retry your request. If it continues to fail, increase your waiting time between retries.
HTTP Status Code: 503
- TooManyRequestsException
-
The request was denied due to request throttling.
HTTP Status Code: 429
- ValidationException
-
The requested action isn't valid.
- fieldList
-
A list of specific validation failures that are encountered during input processing. Each entry in the list contains a path to the field that failed validation and a detailed message that explains why the validation failed. To satisfy multiple constraints, a field can appear multiple times in this list if it failed. You can use the information to identify and fix the specific validation issues in your request.
HTTP Status Code: 400
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following: