liveBook · Manning

Hybrid search

As explained in Generative AI in Action

Hybrid search combines multiple search methods, such as text-based, categorical, numerical, and vector-based retrieval, in one query.

Hybrid search combines search methods in one query. A search application might use text or other indexed fields alongside vector similarity, depending on its data and requirements. In Azure AI Search, a hybrid request runs full-text and vector queries against an index containing searchable text and embeddings, then returns one result set using reciprocal rank fusion. section 8.5 section 8.2

A Redis example starts with a user question, converts it to an embedding, and searches indexed blog posts. Its `hybrid_search()` function combines an indexed-field expression with a K-nearest-neighbors query over `@embedding`, requests fields such as title and content, sorts by `vector_score`, and limits the results with `top_k`. The shown call uses `hybrid_fields="*"`; it demonstrates the query structure without specifying a selective text or category condition. section 8.5

Listing 8.8 Searching using Redis
def hybrid_search(query_vector, client, top_k=3, hybrid_fields="*"):
    base_query = f"{hybrid_fields}=>
                        [KNN {top_k} 
                        @embedding $vector AS vector_score]"  #1
    query = Query(base_query).return_fields(
        "url",                                               #2
        "title",                                            #2
        "publish_date",                                     #2
        "description",                                      #2
        "content",                                          #2
        "vector_score").sort_by("vector_score").dialect(2)   #3
    try:
        results = client.ft("posts").search(
            query, query_params={"vector": query_vector})  #4
    except Exception as e:
        print("Error calling Redis search: ", e)
        return None

    if results.total == 0:
        print("No results found for the given query vector.")
        return None

    return results

# Connect to the Redis server
conn = redis.Redis(...)

query = input("Enter your query: ")      #5

print("Vectorizing query...")
query_vector = get_embedding(query)    #6

query_vector = np.array(query_vector).astype(              #7

                        np.float32).tobytes()
print("Searching for similar posts...")
results = hybrid_search(query_vector, conn)                #8

if results:
    print(f"Found {results.total} results:")
    for i, post in enumerate(results.docs):
        score = 1 - float(post.vector_score)
        print(post.content)
else:
    print("No results found")

The Redis listing shows where an indexed-field expression, vector KNN clause, returned fields, score ordering, and result limit enter the query. section 8.5

For retrieval-augmented chat, the matching post chunks become context for an LLM prompt alongside the original question. Choose the search methods and fields to fit the information being queried. For larger enterprise workloads, Azure AI Search offers managed scaling and security features, including encryption and role-based access control; its reciprocal-rank-fusion result assembly is specific to the Azure implementation described here. section 8.6 section 8.2

How Generative AI in Action covers it

How deep it goes
Defines combined search types and shows Redis query construction, but does not elaborate reciprocal rank fusion internals.
Examples
Uses a Redis search for three similar blog posts and an end-to-end chat application that retrieves five results.
What it assumes
Assumes indexed posts and generated embeddings; demonstrates query construction rather than building those inputs from scratch.
What you can do after
Apply it via Redis to retrieve indexed posts for a query and supply matching chunks to an LLM prompt.
How it connects
Connects K-nearest neighbors, vector embeddings, full-text search, search indexes, and retrieval-augmented generation.

Also explained in

Compare how 8 books explain hybrid search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.