vector_search_hash
Execute a KNN vector similarity search on Redis-stored vectors in hash structures by providing a query vector, index, and desired results. Retrieve nearest neighbors with optional field returns.
Instructions
Perform a KNN vector similarity search using Redis 8 or later version on vectors stored in hash data structures.
Args: query_vector: List of floats to use as the query vector. index_name: Name of the Redis index. Unless specifically specified, use the default index name. vector_field: Name of the indexed vector field. Unless specifically required, use the default field name k: Number of nearest neighbors to return. return_fields: List of fields to return (optional).
Returns: A list of matched documents or an error message.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index_name | No | vector_index | |
| k | No | ||
| query_vector | Yes | ||
| return_fields | No | ||
| vector_field | No | vector |
Implementation Reference
- src/tools/redis_query_engine.py:104-150 (handler)The @mcp.tool()-decorated asynchronous handler function that implements the core logic for the 'vector_search_hash' tool. It performs a KNN vector similarity search on a Redis vector index using hash data structures, converting the query vector to float32, building a RediSearch query, executing the search, and returning the results or an error message.@mcp.tool() async def vector_search_hash( query_vector: List[float], index_name: str = "vector_index", vector_field: str = "vector", k: int = 5, return_fields: Optional[List[str]] = None, ) -> Union[List[Dict[str, Any]], str]: """ Perform a KNN vector similarity search using Redis 8 or later version on vectors stored in hash data structures. Args: query_vector: List of floats to use as the query vector. index_name: Name of the Redis index. Unless specifically specified, use the default index name. vector_field: Name of the indexed vector field. Unless specifically required, use the default field name k: Number of nearest neighbors to return. return_fields: List of fields to return (optional). Returns: A list of matched documents or an error message. """ try: r = RedisConnectionManager.get_connection() # Convert query vector to float32 binary blob vector_blob = np.array(query_vector, dtype=np.float32).tobytes() # Build the KNN query base_query = f"*=>[KNN {k} @{vector_field} $vec_param AS score]" query = ( Query(base_query) .sort_by("score") .paging(0, k) .return_fields("id", "score", *return_fields or []) .dialect(2) ) # Perform the search with vector parameter results = r.ft(index_name).search( query, query_params={"vec_param": vector_blob} ) # Format and return the results return [doc.__dict__ for doc in results.docs] except RedisError as e: return f"Error performing vector search on index '{index_name}': {str(e)}"
- The function signature and docstring defining the input schema (parameters with types and defaults) and output type for the vector_search_hash tool.async def vector_search_hash( query_vector: List[float], index_name: str = "vector_index", vector_field: str = "vector", k: int = 5, return_fields: Optional[List[str]] = None, ) -> Union[List[Dict[str, Any]], str]: """ Perform a KNN vector similarity search using Redis 8 or later version on vectors stored in hash data structures. Args: query_vector: List of floats to use as the query vector. index_name: Name of the Redis index. Unless specifically specified, use the default index name. vector_field: Name of the indexed vector field. Unless specifically required, use the default field name k: Number of nearest neighbors to return. return_fields: List of fields to return (optional). Returns: A list of matched documents or an error message. """
- src/tools/redis_query_engine.py:104-104 (registration)The @mcp.tool() decorator that registers the vector_search_hash function as an MCP tool.@mcp.tool()