vector_search_hash
Find similar vectors stored in Redis hash structures using K-nearest neighbor search to retrieve relevant documents based on vector similarity.
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 |
|---|---|---|---|
| query_vector | Yes | ||
| index_name | No | vector_index | |
| vector_field | No | vector | |
| k | No | ||
| return_fields | No |
Implementation Reference
- src/tools/redis_query_engine.py:104-150 (handler)The core handler function for the 'vector_search_hash' MCP tool. It performs KNN vector similarity search on Redis hashes using HNSW index. Decorated with @mcp.tool() for automatic registration.@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)}"
- src/tools/redis_query_engine.py:104-104 (registration)The @mcp.tool() decorator registers the vector_search_hash function as an MCP tool.@mcp.tool()
- Function signature provides the input schema (parameters with types and defaults) and output type for the 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]: