← All news

News / Learn / Embeddings & vector search

How does similarity search work?

You'll be able to explain how similarity search finds the closest embeddings to a query and why it returns results by meaning.

Similarity search takes a query, turns it into a list of numbers, and finds stored items whose lists of numbers are closest to it. Closeness is measured with a distance or angle between the vectors. The items that sit nearest the query are the best matches.

Most AI features that search or retrieve content depend on this step. If you understand how the matching works, you can predict why results look right or wrong. You can also tune the parts you control, like chunk size and the number of results you fetch.

Every item you want to search is first converted into an embedding, a vector of numbers that captures its meaning. The same model converts the user's query into a vector too. Both sides must use the same model, or the numbers will not be comparable.

The system then measures how close the query vector is to each stored vector. Cosine similarity is a common choice. It compares the angle between two vectors and ignores their length. Items with the highest similarity score rank first. Some systems use Euclidean distance instead, where a smaller distance means a closer match.

Comparing the query with every stored item is exact and simple. This is called brute-force or exact search. It works well for small collections. For large collections, systems use approximate nearest neighbor (ANN) indexes. These structures trade a small loss of accuracy for much faster lookups.

The top results are usually returned with their scores. Many applications then filter, rerank, or pass the results to a language model. The quality of the final answer depends on the quality of the embeddings and on how the source text was split into chunks.

A minimal exact search over a few vectors using cosine similarity.

import numpy as np

docs = [
    "How do I reset my password?",
    "Our office is closed on holidays.",
    "Steps to recover a forgotten login.",
]

# Assume embed() returns a 1-D numpy array from your embedding model.
doc_vectors = np.array([embed(d) for d in docs])
query_vector = embed("I forgot my password")

def cosine_similarity(matrix, vector):
    matrix_norm = matrix / np.linalg.norm(matrix, axis=1, keepdims=True)
    vector_norm = vector / np.linalg.norm(vector)
    return matrix_norm @ vector_norm

scores = cosine_similarity(doc_vectors, query_vector)
top_k = np.argsort(scores)[::-1][:2]

for i in top_k:
    print(round(float(scores[i]), 3), docs[i])

A short sketch to show the idea. Check the official docs of the tool you use.

  • Mixing embedding models Embed the stored items and the query with the same model and the same settings. Vectors from different models cannot be compared meaningfully. Store the model name with your index so you notice a mismatch.
  • Treating scores as probabilities A similarity score only ranks items against each other for one query. A score of 0.8 does not mean an 80 percent chance of being relevant. Compare results by rank and check quality with real test queries.
  • Assuming exact results at scale Approximate indexes can miss some true nearest neighbors. Measure recall on a sample against exact search before you trust the index in production.
  • Ignoring chunk size A long chunk mixes many ideas, so its vector blurs them together. A very short chunk may lack context. Test a few chunk sizes on real questions and keep the one that retrieves the right text.

Study how ANN indexes such as HNSW and IVF trade speed for recall. Learn how reranking with a second model improves the order of top results. Then look at hybrid search, which combines vector matches with keyword matches.

Why does similarity search find results without matching words?

Embeddings encode meaning, not exact spelling. Texts that express similar ideas land near each other in the vector space, even when they share no words.

Which is better, cosine similarity or Euclidean distance?

It depends on the embedding model. Check the model's documentation for the recommended metric. Many text embedding models are designed for cosine similarity.

Do I need a vector database to do similarity search?

No. For small collections, you can compute scores directly in memory. A vector database helps when the collection is large, changes often, or needs filtering and persistence.

How many results should I retrieve?

Retrieve more than you plan to use, then filter or rerank. Test values such as 5, 10, and 20 on real queries and check whether the answer quality improves.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.