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.Only the equality operator (
=) is supported forHASHattributes. Comparison and range operators are supported forINLINE_FILTERattributes. Only top-level attributes from the search schema can be referenced. -
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
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 | |