Skip to content

query_vectors async

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

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: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 queryTexts, don't set any metadata filters, and don't request metadata (by keeping the returnMetadata parameter set to false or not specified).

  • To use queryTexts, a metadata filter, or returnMetadata set to true with this operation, you must have both the s3vectors:QueryVectors and s3vectors:GetVectors permissions. If you use any of these options without the s3vectors:GetVectors permission, Amazon S3 Vectors returns an AccessDeniedException.

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 (Optional[VectorData], default: None ) –

    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 queryTexts to perform a full-text-only search. A request must include queryVector, queryTexts, or both.

  • 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.

  • query_texts (Optional[QueryTextList], default: None ) –

    Full-text queries for searchable metadata keys. Omit queryVector to run a full-text-only search, or provide both fields to run a hybrid search. Each entry must target a distinct searchable metadata key.

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

    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.

  • 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/async_s3_vectors.py
async def query_vectors(
    self,
    top_k: "capo_s3vectors.types.top_k.TopK",
    *,
    config_overrides: Optional[AsyncS3VectorsClientConfig] = 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,
    query_vector: Optional["capo_s3vectors.types.vector_data.VectorData"] = 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,
    query_texts: Optional[
        "capo_s3vectors.types.query_text_list.QueryTextList"
    ] = None,
    return_scores: Optional[bool] = None,
    next_token: Optional[
        "capo_s3vectors.types.query_vectors_next_token.QueryVectorsNextToken"
    ] = None,
) -> "capo_s3vectors.types.query_vectors_output.QueryVectorsOutput":
    """<p>Returns matching vector keys from a vector index ranked by vector similarity, full-text relevance, or both. Specify <code>queryVector</code> to perform a vector search, specify <code>queryTexts</code> 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).</p> <p>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 <code>ENHANCED</code> mode.</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 <code>queryTexts</code>, 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>To use <code>queryTexts</code>, a metadata filter, or <code>returnMetadata</code> set to <code>true</code> with this operation, you must have both the <code>s3vectors:QueryVectors</code> and <code>s3vectors:GetVectors</code> permissions. If you use any of these options without the <code>s3vectors:GetVectors</code> permission, Amazon S3 Vectors returns an <code>AccessDeniedException</code>.</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 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 <code>queryTexts</code> to perform a full-text-only search. A request must include <code>queryVector</code>, <code>queryTexts</code>, or both.</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>
        query_texts: <p>Full-text queries for searchable metadata keys. Omit <code>queryVector</code> to run a full-text-only search, or provide both fields to run a hybrid search. Each entry must target a distinct searchable metadata key.</p>
        return_scores: <p>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 <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.
    """

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

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

    def _redact_input(
        input_: "capo_s3vectors.types.query_vectors_input.QueryVectorsInput",
    ) -> dict[str, Any]:
        import capo_s3vectors.types.query_vectors_input

        return capo_s3vectors.types.query_vectors_input.redact_sensitive(input_)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("QueryVectors", redact_input=_redact_input),
    )
    input_: capo_s3vectors.types.query_vectors_input.QueryVectorsInput = {
        "top_k": top_k
    }
    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 query_vector is not None:
        input_["query_vector"] = query_vector
    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 query_texts is not None:
        input_["query_texts"] = query_texts
    if return_scores is not None:
        input_["return_scores"] = return_scores
    if next_token is not None:
        input_["next_token"] = next_token

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output