Fuzzy search and bevtor db with Redis

Saurabh Tripathy 0 Reputation points
2024-11-13T21:56:04.65+00:00

We are using Azure cache for Redis for standard key look up use case. Now we want to enable fuzzy search (full text) and vector db for our Redis.

I see these capabilities are there in Redis Enterprise. Do I need to migrate to Create --> Redis Enterprise & Flash OR Azure cache for redis provides these features.

Anything extra I need to do to store vectors in Redis?

Pls guide me through a tutorial using a Python client.

Azure Cache for Redis
Azure Cache for Redis
An Azure service that provides access to a secure, dedicated Redis cache, managed by Microsoft.
260 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sai Raghunadh M 1,045 Reputation points Microsoft Vendor
    2024-11-14T10:27:07.3833333+00:00

    Hi @saurabh tripathy

    Thanks for the question and using MS Q&A platform.

    Azure Cache for Redis offers core Redis capabilities, including key-value store functionality, but it doesn't natively support full-text search or vector-based queries. These advanced features, like full-text search and vector search (for AI/ML workloads), are available in RedisEnterprise and also in Redis with RedisSearch and RedisVector modules.

    Full-Text Search and Fuzzy Search:

    Azure Cache for Redis: Only supports basic key-value lookups and operations.

    Redis Enterprise: Includes support for RedisSearch, which enables advanced search capabilities like full-text search, filtering, and fuzzy search.

    Vector Search:

    Azure Cache for Redis: Does not natively support vector search for AI and machine learning workloads.

    Redis Enterprise: Provides RedisVector, which allows storing and querying vectors for use in AI applications like nearest neighbor search.

    If you are using Azure Cache for Redis, you will need to migrate to Redis Enterprise to enable full-text search (RedisSearch) and vector search (RedisVector).

    The provided Python code offers a simple demonstration of full-text search and vector search.

    import redis
    from redisearch import Client, TextField, Query
    import numpy as np
    from redisvector import VectorField
    # Connect to Redis Enterprise (with RedisSearch & RedisVector enabled)
    r = redis.Redis(host='your-redis-server', port=6379, db=0)
    # Initialize RedisSearch client for creating an index
    search_client = Client('my_index', conn=r)
    # Create a full-text search index with a text field
    def create_search_index():
        try:
            search_client.create_index([
                TextField('title'),  # Full-text field
                TextField('content')  # Full-text field
            ])
            print("Search index created successfully.")
        except Exception as e:
            print("Index already exists or error: ", e)
    # Index some documents
    def add_document(doc_id, title, content):
        search_client.add_document(doc_id, title=title, content=content)
        print(f"Document {doc_id} added.")
    # Full-text search query
    def search_documents(query):
        q = Query(query).paging(0, 5)  # Fetch first 5 results
        results = search_client.search(q)
        for doc in results.docs:
            print(f"ID: {doc.id}, Title: {doc.title}, Content: {doc.content}")
    # Create and insert a vector field for vector search
    def create_vector_index():
        try:
            # Example to create an index for vectors (e.g., 128-dimensional vectors)
            search_client.create_index([
                VectorField('vector', 'FLAT', dimensions=128)
            ])
            print("Vector index created successfully.")
        except Exception as e:
            print("Vector index already exists or error: ", e)
    # Example to add vectors
    def add_vector(doc_id, vector):
        search_client.add_document(doc_id, vector=vector)
        print(f"Vector for document {doc_id} added.")
    # Perform vector search
    def vector_search(query_vector, top_k=5):
        # Perform K-nearest neighbor search
        q = Query('*=>[KNN {top_k} @vector $vec as score]').sort_by('score').return_fields('id', 'score').paging(0, top_k).sort_by_score()
        results = search_client.search(q, query_params={'vec': query_vector.tolist()})
        for doc in results.docs:
            print(f"Doc ID: {doc.id}, Score: {doc.score}")
    # Sample usage:
    # Create indexes
    create_search_index()
    create_vector_index()
    # Add documents
    add_document('doc1', 'Redis Search Example', 'This document describes Redis full-text search and vector search features.')
    add_document('doc2', 'AI Vector Search', 'This document explores vector search capabilities for AI applications.')
    # Search for documents (Full-text search)
    search_documents('Redis full-text search')
    # Add a random vector for document 1 (Example: 128-dimensional vector)
    vector = np.random.rand(128)
    add_vector('doc1', vector)
    # Perform vector search (for example, a random query vector)
    query_vector = np.random.rand(128)
    vector_search(query_vector)
    

    Redis Enterprise offers these features out-of-the-box, while for Azure Cache for Redis, you'd need to rely on external services or manage the Redis modules yourself (RedisSearch is supported in the enterprise setup, and RedisVector would be tied to Redis Enterprise).

    Hope this helps. Do let us know if you any further queries. If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.