Skip to content

query_vectors

query_vectors(
    top_k: TopK,
    query_vector: VectorData,
    *,
    config_overrides: Optional[S3VectorsClientConfig] = None,
    vector_bucket_name: Optional[VectorBucketName] = None,
    index_name: Optional[IndexName] = None,
    index_arn: Optional[IndexArn] = None,
    filter: Optional[object] = None,
    query_mode: Optional[IndexMode] = None,
    return_metadata: Optional[bool] = None,
    return_distance: Optional[bool] = None,
    next_token: Optional[QueryVectorsNextToken] = None,
) -> QueryVectorsOutput

Performs an approximate nearest neighbor search query in a vector index using a query vector. By default, it returns the keys of approximate nearest neighbors. You can optionally include the computed distance (between the query vector and each vector in the response) and metadata of each vector in the response.

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:QueryVectors permission to use this operation. Additional permissions are required based on the request parameters you specify:

  • With only s3vectors:QueryVectors permission, you can retrieve vector keys of approximate nearest neighbors and computed distances between these vectors. This permission is sufficient only when you don't set any metadata filters and don't request metadata (by keeping the returnMetadata parameter set to false or not specified).

  • If you specify a metadata filter or set returnMetadata to true, you must have both s3vectors:QueryVectors and s3vectors:GetVectors permissions. The request fails with a 403 Forbidden error if you request metadata filtering or metadata without the s3vectors:GetVectors permission.

Parameters:

  • vector_bucket_name (Optional[VectorBucketName], default: None ) –

    The name of the vector bucket that contains the vector index.

  • index_name (Optional[IndexName], default: None ) –

    The name of the vector index that you want to query.

  • index_arn (Optional[IndexArn], default: None ) –

    The ARN of the vector index that you want to query.

  • top_k (TopK) –

    The number of results to return for each query.

  • query_vector (VectorData) –

    The query vector. Ensure that the query vector has the same dimension as the dimension of the vector index that's being queried. For example, if your vector index contains vectors with 384 dimensions, your query vector must also have 384 dimensions.

  • filter (Optional[object], default: None ) –

    Metadata filter to apply during the query. For more information about metadata keys, see Metadata filtering in the Amazon S3 User Guide.

  • query_mode (Optional[IndexMode], default: None ) –

    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 specify CLASSIC for an ENHANCED index.

    • ENHANCED - Applies metadata filters before the vector search.

  • return_metadata (Optional[bool], default: None ) –

    Indicates whether to include metadata in the response. The default value is false.

  • return_distance (Optional[bool], default: None ) –

    Indicates whether to include the computed distance in the response. The default value is false.

  • next_token (Optional[QueryVectorsNextToken], default: None ) –

    Pagination token from a previous request. The value of this field is empty for an initial request.

Raises:

Source code in docs_src/capo_s3vectors/_services/s3_vectors.py
def query_vectors(
    self,
    top_k: "capo_s3vectors.types.top_k.TopK",
    query_vector: "capo_s3vectors.types.vector_data.VectorData",
    *,
    config_overrides: Optional[S3VectorsClientConfig] = None,
    vector_bucket_name: Optional[
        "capo_s3vectors.types.vector_bucket_name.VectorBucketName"
    ] = None,
    index_name: Optional["capo_s3vectors.types.index_name.IndexName"] = None,
    index_arn: Optional["capo_s3vectors.types.index_arn.IndexArn"] = None,
    filter: Optional[object] = None,
    query_mode: Optional["capo_s3vectors.types.index_mode.IndexMode"] = None,
    return_metadata: Optional[bool] = None,
    return_distance: Optional[bool] = None,
    next_token: Optional[
        "capo_s3vectors.types.query_vectors_next_token.QueryVectorsNextToken"
    ] = None,
) -> "capo_s3vectors.types.query_vectors_output.QueryVectorsOutput":
    """<p>Performs an approximate nearest neighbor search query in a vector index using a query vector. By default, it returns the keys of approximate nearest neighbors. You can optionally include the computed distance (between the query vector and each vector in the response) and metadata of each vector in the response.</p> <p>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). </p> <dl> <dt>Permissions</dt> <dd> <p>You must have the <code>s3vectors:QueryVectors</code> permission to use this operation. Additional permissions are required based on the request parameters you specify:</p> <ul> <li> <p>With only <code>s3vectors:QueryVectors</code> permission, you can retrieve vector keys of approximate nearest neighbors and computed distances between these vectors. This permission is sufficient only when you don't set any metadata filters and don't request metadata (by keeping the <code>returnMetadata</code> parameter set to <code>false</code> or not specified).</p> </li> <li> <p>If you specify a metadata filter or set <code>returnMetadata</code> to true, you must have both <code>s3vectors:QueryVectors</code> and <code>s3vectors:GetVectors</code> permissions. The request fails with a <code>403 Forbidden error</code> if you request metadata filtering or metadata without the <code>s3vectors:GetVectors</code> permission.</p> </li> </ul> </dd> </dl>

    Args:
        vector_bucket_name: <p>The name of the vector bucket that contains the vector index. </p>
        index_name: <p>The name of the vector index that you want to query. </p>
        index_arn: <p>The ARN of the vector index that you want to query.</p>
        top_k: <p>The number of results to return for each query.</p>
        query_vector: <p>The query vector. Ensure that the query vector has the same dimension as the dimension of the vector index that's being queried. For example, if your vector index contains vectors with 384 dimensions, your query vector must also have 384 dimensions. </p>
        filter: <p>Metadata filter to apply during the query. For more information about metadata keys, see <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-vectors-metadata-filtering.html">Metadata filtering</a> in the <i>Amazon S3 User Guide</i>. </p>
        query_mode: <p>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.</p> <p>Valid values:</p> <ul> <li> <p> <code>CLASSIC</code> - Applies metadata filters during the vector search. You can't specify <code>CLASSIC</code> for an <code>ENHANCED</code> index.</p> </li> <li> <p> <code>ENHANCED</code> - Applies metadata filters before the vector search.</p> </li> </ul>
        return_metadata: <p>Indicates whether to include metadata in the response. The default value is <code>false</code>.</p>
        return_distance: <p>Indicates whether to include the computed distance in the response. The default value is <code>false</code>.</p>
        next_token: <p>Pagination token from a previous request. The value of this field is empty for an initial request.</p>

    Raises:
        capo_s3vectors.errors.access_denied_exception.AccessDeniedException: <p>Access denied.</p>
        capo_s3vectors.errors.internal_server_exception.InternalServerException: <p>The request failed due to an internal server error.</p>
        capo_s3vectors.errors.request_timeout_exception.RequestTimeoutException: <p>The request timed out. Retry your request.</p>
        capo_s3vectors.errors.too_many_requests_exception.TooManyRequestsException: <p>The request was denied due to request throttling.</p>
        capo_s3vectors.errors.validation_exception.ValidationException: <p>The requested action isn't valid.</p>
        capo_s3vectors.errors.kms_disabled_exception.KmsDisabledException: <p>The specified Amazon Web Services KMS key isn't enabled.</p>
        capo_s3vectors.errors.kms_invalid_key_usage_exception.KmsInvalidKeyUsageException: <p>The request was rejected for one of the following reasons: </p> <ul> <li> <p>The <code>KeyUsage</code> value of the KMS key is incompatible with the API operation.</p> </li> <li> <p>The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (<code>KeySpec</code>).</p> </li> </ul> <p>For more information, see <a href="https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html#API_Encrypt_Errors">InvalidKeyUsageException</a> in the <i>Amazon Web Services Key Management Service API Reference</i>.</p>
        capo_s3vectors.errors.kms_invalid_state_exception.KmsInvalidStateException: <p>The key state of the KMS key isn't compatible with the operation.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html#API_Encrypt_Errors">KMSInvalidStateException</a> in the <i>Amazon Web Services Key Management Service API Reference</i>.</p>
        capo_s3vectors.errors.kms_not_found_exception.KmsNotFoundException: <p>The KMS key can't be found.</p>
        capo_s3vectors.errors.not_found_exception.NotFoundException: <p>The request was rejected because the specified resource can't be found.</p>
        capo_s3vectors.errors.service_unavailable_exception.ServiceUnavailableException: <p>The service is unavailable. Wait briefly and retry your request. If it continues to fail, increase your waiting time between retries.</p>
        capo_s3vectors.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_s3vectors.types.query_vectors_input.QueryVectorsInput]",
    ) -> OperationResponse[
        "capo_s3vectors.types.query_vectors_output.QueryVectorsOutput"
    ]:
        import capo_s3vectors._operations.s3_vectors.query_vectors

        output, http_response = (
            capo_s3vectors._operations.s3_vectors.query_vectors.query_vectors(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("QueryVectors")
    )
    input_: capo_s3vectors.types.query_vectors_input.QueryVectorsInput = {
        "top_k": top_k,
        "query_vector": query_vector,
    }
    if vector_bucket_name is not None:
        input_["vector_bucket_name"] = vector_bucket_name
    if index_name is not None:
        input_["index_name"] = index_name
    if index_arn is not None:
        input_["index_arn"] = index_arn
    if filter is not None:
        input_["filter"] = filter
    if query_mode is not None:
        input_["query_mode"] = query_mode
    if return_metadata is not None:
        input_["return_metadata"] = return_metadata
    if return_distance is not None:
        input_["return_distance"] = return_distance
    if next_token is not None:
        input_["next_token"] = next_token

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output