Skip to main content

Semantic Search

Traditional search matches words. A customer searching for "laptop won't turn on" does not find the article titled "Computer does not boot", because the two share no keywords. Semantic search matches meaning: it understands that both describe the same problem.

Semantic search is the foundation of RAG, and it is valuable on its own: for product search, support knowledge bases, document management, recommendations and duplicate detection.

Keyword (lexical) searchSemantic (vector) search
MatchesExact words and their variantsMeaning, including synonyms and paraphrases
TechniqueInverted index, ranking such as BM25Embeddings and nearest-neighbor search
Strong atProduct codes, names, IDs, rare technical terms, exact phrasesNatural language questions, vague descriptions, different wording, multiple languages
Weak atSynonyms, paraphrases, intentExact identifiers, rare terms the model did not learn well
ExplainabilityEasy: the matching words are visibleHarder: similarity in a vector space

In practice, the best results usually come from hybrid search, which combines both.

Embeddings​

An embedding is a list of numbers (a vector), produced by an embedding model, that represents the meaning of a piece of text, an image or another item. Items with similar meaning get vectors that are close to each other.

The first two vectors are close to each other; the third is far away.

Key properties of embedding models:

  • Dimensions: typically several hundred to a few thousand numbers per vector. More dimensions can capture more nuance but cost more storage and compute.
  • Similarity measure: usually cosine similarity (the angle between vectors) or dot product.
  • Domain and language: models differ in quality for specific languages and domains. The MTEB leaderboard compares many models; testing on your own data is still essential.
  • Modality: text models, image models, and multimodal models that place text and images in the same space (search images with text).
  • Source: commercial embedding APIs, or open source models such as those available through Sentence Transformers, which can be self-hosted.
warning

Vectors from different embedding models are not compatible. Changing the model means re-embedding the entire corpus. Store the model name and version with every vector, and plan re-indexing as a normal operation.

Vector indexing​

To find the items most similar to a query, the system compares the query vector with stored vectors. Comparing against every vector (exact k-nearest-neighbor search) is accurate but too slow for millions of items. Vector indexes use approximate nearest neighbor (ANN) algorithms that trade a little accuracy for large gains in speed.

Index typeHow it worksTrade-offs
Flat (brute force)Compares with every vectorExact; fine for up to tens of thousands of items
HNSWA layered graph where each vector links to its neighbors; search navigates the graph (paper)Fast and accurate; the most common default; memory-intensive
IVF (inverted file)Clusters vectors and searches only the nearest clustersLess memory, faster to build; accuracy depends on how many clusters are searched
Quantization (for example product or binary quantization)Compresses vectors into fewer bitsMuch less memory and cost; some loss of accuracy, often recovered by re-scoring top results
Disk-based indexesKeep most of the index on disk instead of memoryVery large collections at lower cost; higher latency

Index parameters control the balance between recall (how many of the true nearest neighbors are found), latency and memory. Libraries such as FAISS implement these algorithms and are used inside many databases.

Filtering​

Real queries combine similarity with conditions: "similar products, in stock, in the customer's country", or "relevant documents the user is allowed to read". How a vector store handles filters matters:

  • Pre-filtering applies conditions first and searches only matching items. It is accurate but can be slow with some index types.
  • Post-filtering searches first and removes non-matching results afterwards. It is fast but can return too few results when filters are selective.
  • Modern engines integrate filtering into the index search. Check how your engine handles your typical filters, especially permission filters.

Hybrid search and reranking​

  1. Run keyword and vector search in parallel, each returning its best candidates.
  2. Fuse the results. A common method is reciprocal rank fusion (RRF), which combines rankings without needing comparable scores.
  3. Rerank the top candidates with a model that reads the query and each candidate together. Rerankers are slower than embeddings, so they are applied only to a short list, but they are much more precise.

This pipeline handles both "error E-4021 on model X200" (keyword strength) and "the printer keeps saying it's out of paper even though it's full" (semantic strength).

Vector databases and search engines​

CategoryExamplesNotes
Dedicated vector databasesPinecone, Weaviate, Qdrant, MilvusBuilt for vector workloads; strong performance and scaling features
Search engines with vector supportElasticsearch, OpenSearchMature keyword search plus vectors: natural choice for hybrid search
Databases with vector extensionspgvector for PostgreSQL, and vector features in other databasesVectors next to business data, with transactions, joins and existing operations
Cloud servicesAzure AI Search, Amazon OpenSearch Service, Amazon S3 Vectors, Google Cloud vector search servicesManaged, integrated with each cloud's AI services

Choosing​

  • Start with what you already run. For up to a few million vectors, pgvector in an existing PostgreSQL database or vector search in an existing Elasticsearch/OpenSearch cluster is often enough, and avoids a new system to operate.
  • Need strong keyword search too? A search engine with vector support makes hybrid search simple.
  • Very large scale, high query rates or advanced vector features? Consider a dedicated vector database.
  • Large, rarely queried collections? Storage-based options reduce cost.
  • Record the choice in the technology radar, so teams do not each pick a different vector store.

Architecture: keeping the index in sync​

A search index is a copy of data from source systems, and must be kept current:

  • Incremental updates from change events or Change Data Capture, so new and changed items are searchable within minutes.
  • Deletes and permission changes propagated as reliably as additions.
  • Versioned re-indexing: build a new index with a new embedding model or chunking strategy in parallel, evaluate it, and switch over without downtime.
  • One search capability, many consumers: a shared search service per domain is easier to keep consistent than every application maintaining its own index.

Use cases​

Use caseHow semantic search helps
Product searchFinds products from natural descriptions ("warm waterproof jacket for hiking"), across languages
Support and knowledge basesMatches customer problems to articles and past solutions, regardless of wording
Document and contract searchFinds clauses and documents by meaning, not only by exact terms
Recommendations"Similar items" and "customers who viewed this" based on content similarity
Duplicate detectionFinds duplicate tickets, records or documents written differently
Classification and clusteringGroups feedback, tickets or documents by topic without manual rules
Retrieval for LLMsSupplies relevant context for RAG and assistants

Evaluating search quality​

  • Build a set of real queries with relevance judgments (which results are good for each query).
  • Measure recall at k (are the relevant items in the top k?) and ranking quality metrics such as nDCG.
  • Compare configurations offline (embedding model, chunking, hybrid weights, reranker) before changing production.
  • In production, monitor user behavior: click-through, reformulated queries, searches with no results, and A/B tests of changes.

Common pitfalls​

  • Vectors only: losing exact matches on codes, names and abbreviations that keyword search handles easily.
  • Untested embedding model: choosing by leaderboard rank without testing on your own language and domain.
  • Forgetting re-embedding costs: switching models without planning to re-process the whole corpus.
  • Stale or leaky index: deletes and permission changes not propagated to the index.
  • A new database for every team: several vector stores with overlapping content and no shared operations.