liveBook · Manning

Similarity search

As explained in Grokking Software Architecture

Similarity search retrieves items whose numerical representations are close to a query representation in a vector space.

Similarity search finds items whose numerical representations are close to a query’s representation. The vectors act like coordinates on a map: related meanings can be neighbors even when their words differ. section 8.2.3

This approach supports semantic search, recommendations, and retrieval-augmented generation. For example, a search for something to wear to a wedding can return suits and dresses without matching the word “wedding.” section 8.2.3

Find comfort food

An AI service converts “Lasagna” and “Comfort Food” into vectors. A vector database returns Lasagna when it is the closest match to the query. section 8.2.4

Listing 8.3 The Vector Way (Pinecone/Weaviate)
# STEP 1: Generate the Embedding (using an AI service) 
import openai 
 
# The AI converts "Lasagna" into 1536 numbers 
embedding_response = openai.Embedding.create( 
    input="Lasagna", 
    model="text-embedding-ada-002" 
) 
 
vector = embedding_response['data'][0]['embedding'] #A
# STEP 2: Store it in the Vector Database 
vector_db.upsert( 
    id="recipe_1", 
    values=vector, # The 1536-dimensional vector from OpenAI 
    metadata={"name": "Lasagna"} 
) 
 
# STEP 3: Query 
# We convert the USER'S query into a vector first 
query_vector = openai.Embedding.create( #B
    input="Comfort Food", 
    model="text-embedding-ada-002" 
)['data'][0]['embedding'] 
 
results = vector_db.query( 
    vector=query_vector, 
    top_k=1 
) 
# RETURNS: "Lasagna" (because its vector is closest to "Comfort Food")

The example shows the sequence: generate an embedding, store it with metadata, then query for the nearest result. section 8.2.4

How Grokking Software Architecture covers it

How deep it goes
Explains nearest-neighbor retrieval with embedding coordinates and a recipe workflow, but does not detail distance calculations or indexing internals.
Examples
Uses canine-to-dog retrieval, wedding-clothing search, and a Comfort Food query that returns Lasagna.
What you can do after
Apply it via an embedding service and vector database: generate vectors, store them, and query for nearest results.
How it connects
Connects embeddings and vector databases with semantic search, recommendation engines, and retrieval-augmented generation.

Also explained in

Compare how 37 books explain similarity search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.