liveBook · Manning

Similarity search

As explained in Generative AI in Action

Retrieval is the process of selecting relevant information from a collection in response to an information need.

An enterprise assistant cannot rely on an LM’s training data to answer questions about current or private information. Similarity search helps it find relevant information in an external collection. In retrieval-augmented generation (RAG), a retriever selects passages as context, and the LLM uses that context to generate an answer. section 7.0 section 7.3

For vector similarity search, encode the query and stored documents or passages as embeddings—numerical vectors representing their content. A vector index stores the document embeddings. At query time, search compares the query vector with indexed vectors and returns the closest matches under a chosen similarity measure. An index makes this search efficient without changing the goal: find useful context for the question. section 7.5.1 section 7.5.2

Choose the retrieval approach and index for the application:

  • Sparse retrieval matches terms, for example with TF-IDF. It is efficient for keyword-dependent searches but can miss synonyms and nuanced phrasing. Dense retrieval compares embeddings to capture semantic relationships, at greater computational and data cost. section 7.4
  • Choose a measure that suits the data: cosine similarity emphasizes vector direction and is useful for text; Euclidean distance reflects spatial separation; dot product is another supported option. Index choices such as exact search or HNSW trade speed, accuracy, and memory use. section 7.5.1 section 7.5.2
  • Chunk long documents before indexing when passage-level matches are more useful than whole-document matches. Check that each chunk remains understandable on its own: overly large or small chunks can reduce relevance, and retrieved context must fit the LLM’s context window. section 7.7.1

Search a collection of blog posts

Ingest posts once, create embeddings for their chunks, and save them in Redis rather than reloading and embedding every post for every question. For “Tell me about Longhorn,” embed the query and run a Redis K-nearest-neighbors search against the saved vectors. The illustrative hybrid search can also include other search fields; it returns the top three posts sorted by vector score. A chat version retrieves the top five chunks, adds them and the question to a prompt, then calls the LLM. These examples demonstrate the flow, not a production-ready deployment. section 8.1.2 section 8.5 section 8.6

Listing 8.2 Redis index schema
SCHEMA = [
    TagField("url"),
    TextField("title"), 
    TextField("description"),
    TextField("publish_date"),
    TextField("content"),
    VectorField("embedding", "HNSW",
                {"TYPE": "FLOAT32",
                 "DIM": 1536,
                 "DISTANCE_METRIC": "COSINE"}),
   ]

The Redis schema shows a concrete index choice: an HNSW embedding field configured as FLOAT32, with 1536 dimensions and COSINE distance, alongside searchable post metadata. section 8.3

Retrieval quality depends on the indexed information as well as the search settings. Keep data relevant and current; assess latency and index growth as the collection scales. For enterprise use, the local Redis demonstration also leaves security, scaling, management, and operations to be engineered. section 7.6 section 8.6

How Generative AI in Action covers it

How deep it goes
Explains vector matching, index and metric choices, then demonstrates Redis queries; stops short of a production-ready deployment.
Examples
Uses a Redis K-nearest-neighbors query over embedded blog posts and a chat application that places retrieved chunks in an LLM prompt.
What it assumes
Introduces embeddings, indexes, and similarity measures incrementally; the worked application uses Python, Redis, and an LLM API.
What you can do after
Build it from scratch as an illustrative Redis-backed search and RAG chat flow, while identifying additional production engineering needs.
How it connects
Connects embeddings and vector indexes with dense retrieval, chunking, and retrieval-augmented generation.

Also explained in

Compare how 22 books explain similarity 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.