search_vectors
¶
search_vectors(
table_name: TableArn,
index_name: IndexName,
search_vector: SearchVectorList,
top_k: TopKInteger,
*,
config_overrides: Optional[DynamoDBClientConfig] = None,
return_consumed_capacity: Optional[ReturnConsumedCapacity] = None,
expression_attribute_names: Optional[ExpressionAttributeNameMap] = None,
expression_attribute_values: Optional[ExpressionAttributeValueMap] = None,
projection_expression: Optional[ProjectionExpression] = None,
search_condition_expression: Optional[String] = None,
) -> SearchVectorsOutput
Performs a vector similarity search on a vector index associated with an Amazon DynamoDB table, and returns the most similar items sorted by similarity score based on the distance function configured for the index.
Score interpretation depends on the distance function:
-
COSINE- Returns the items with the k smallest scores. Scores range from 0 (identical) to 2 (opposite). Lower scores indicate higher similarity. -
EUCLIDEAN- Returns the items with the k smallest scores. Scores represent the Euclidean distance between vectors. Lower scores indicate higher similarity. -
DOT_PRODUCT- Returns the items with the k highest scores. Higher scores indicate higher similarity.
Parameters:
-
table_name(TableArn) –The name or Amazon Resource Name (ARN) of the table containing the vector index.
-
index_name(IndexName) –The name of the vector index to search. The index must be in the
ACTIVEstate. -
expression_attribute_names(Optional[ExpressionAttributeNameMap], default:None) –One or more substitution tokens for attribute names in an expression. Use the
#character in an expression to dereference an attribute name. -
expression_attribute_values(Optional[ExpressionAttributeValueMap], default:None) –One or more values that can be substituted in an expression. Use the
:character in an expression to dereference an attribute value. -
projection_expression(Optional[ProjectionExpression], default:None) –A string that identifies one or more attributes to retrieve from the index. Separate attribute names with commas. If not specified, the operation returns all attributes projected into the vector index.
Only attributes projected into the vector index can be retrieved.
-
search_vector(SearchVectorList) –The search vector to compare against the indexed vectors. Each element is a 32-bit IEEE-754 floating point number, provided in DynamoDB list format.
The number of dimensions must match the number of dimensions configured for the vector index.
-
search_condition_expression(Optional[String], default:None) –A condition expression used to filter the vector search results. The expression can reference attributes defined in the vector index search schema, including
HASHandINLINE_FILTERkey elements.The
HASHandINLINE_FILTERattributes support only the equality operator (=). You can reference only top-level attributes from the search schema. -
top_k(TopKInteger) –The number of most similar results to return.
Raises:
-
InternalServerError–An error occurred on the server side.
-
RequestLimitExceeded–Throughput exceeds the current throughput quota for your account. For detailed information about why the request was throttled and the ARN of the impacted resource, find the ThrottlingReason field in the returned exception. Contact Amazon Web Services Support to request a quota increase.
-
ResourceNotFoundException–The operation tried to access a nonexistent table or index. The resource might not be specified correctly, or its status might not be
ACTIVE. -
ThrottlingException–The request was denied due to request throttling. For detailed information about why the request was throttled and the ARN of the impacted resource, find the ThrottlingReason field in the returned exception.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To search for similar vectors This example searches the Products table for the top 3 items most similar to a provided vector, using the 'cosine-product-idx' vector index. The SearchConditionExpression filters results to the 'Electronics' category. The operation returns only the ProductName and Price attributes.
>>> client.search_vectors(table_name='Products', index_name='cosine-product-idx', search_vector=[{'N': '0.12'}, {'N': '0.85'}, {'N': '0.44'}, {'N': '0.67'}], top_k=3, search_condition_expression='Category = :cat', projection_expression='ProductName, Price', expression_attribute_values={':cat': {'S': 'Electronics'}}, return_consumed_capacity='INDEXES')
Source code in docs_src/capo_dynamodb/_services/dynamo_db.py
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 | |