Similarity search finds stored content that is closest in meaning to a user’s query. Convert the query and stored items into embeddings—numeric vectors produced by the same model—then compare them and return the best matches. A request for a dish like spaghetti carbonara, for example, can surface other creamy pasta dishes without matching those exact words. section 5.4.1 section 5.4.2
A practical retrieval pipeline has four stages:
- Ingest content such as questions or documents. For documents, split the content into manageable chunks before embedding it. section 5.4.3 section 7.2.4
- Generate embeddings and store them with the content or metadata needed to identify a result. Use the same embedding model for incoming queries. section 5.4.2 section 7.2.4
- Search the stored vectors, rank matches by similarity, and retrieve the associated answers or documents. section 5.4.3 section 7.2.2
- If generating an answer, pass the retrieved context and original question to a language model rather than relying on the question alone. section 6.2.3 section 7.2.2
IT support question matching
Precompute embeddings for support questions such as “How do I reset my password?” Embed “I forgot my password” with the same model, compare it against the stored vectors using cosineSimilarity, and return the answer associated with the highest-scoring question. The in-memory object keeps this example simple; a vector store is the storage option for a larger application. section 5.4.3 section 6.2.2
Listing 5.11 Performing similarity search on the embeddings
let maxSimilarity = -1; #1
let mostRelevantQuestion = ""; #2
for (const [question, storedEmbedding] of Object.entries(embeddingDB)) {
const similarity =
cosineSimilarity(queryEmbedding, storedEmbedding); #3
if (similarity > maxSimilarity) { #4
maxSimilarity = similarity;
mostRelevantQuestion = question;
}
}
The JavaScript loop compares a query embedding with each stored embedding and retains the highest-scoring question. section 5.4.3
LangChain.js offers a less manual route: create a MemoryVectorStore from texts and embeddings, call similaritySearch(query, k), or use asRetriever() in a RunnableSequence that feeds retrieved documents into a prompt. MemoryVectorStore is ephemeral. For a RAG application that reloads indexes, HNSWLib saves them to the filesystem; a multi-knowledge-base application instead uses Upstash Vector for externally stored document embeddings. Match each store to its deployment needs, and keep chain inputs aligned with the prompt’s expected context and question fields. section 6.2.2 section 6.2.3 section 7.2.4 section 11.1.1



















