Elasticsearch Vector Search and Semantic Search
On this page
Why keyword search misses meaning
A shopper searches "cheap laptop for school". The best product is a "Chromebook 14: inexpensive notebook ideal for school and homework". It shares only "for" and "school" with the query: no "cheap", no "laptop". Keyword search (BM25, Day 9) can't know that inexpensive means cheap or that a notebook is a laptop. Vector search can, because it compares meanings.
What are embeddings?
An embedding model turns text (or images) into a vector of numbers, typically hundreds to thousands of dimensions, placed so that similar meanings land close together. Picture a map where every sentence has coordinates: "cheap laptop" and "inexpensive notebook" sit in the same neighborhood, while "laptop bag" is a few streets away and "banana bread" is in another city. Real embeddings have hundreds of coordinates instead of two, but the idea is the same: search becomes "find the nearest neighbors". Cosine similarity measures closeness by the angle between two vectors, ignoring their length. You embed every document when indexing and the user's query at search time, then find the documents whose vectors are nearest the query's.
The course lab uses tiny 4-dimension hand-made vectors so the results are easy to follow; real systems use models such as OpenAI, Cohere or open-source sentence transformers, or Elastic's built-in ELSER and E5 models.
"embedding": { "type": "dense_vector", "dims": 4, "similarity": "cosine" }dense_vector fields are indexed for approximate nearest neighbor (ANN) search with HNSW graphs, and quantized by default to save memory.
Running a kNN vector query
GET /products/_search
{
"knn": {
"field": "embedding",
"query_vector": [0.28, 0.92, 0.05, 0.0],
"k": 3,
"num_candidates": 20
},
"_source": ["name"]
}total 3
106 1.000 Chromebook 14
104 0.999 Budget Laptop
105 0.949 Ultrabook ProThe Chromebook, which keyword search ranked second, comes first. k is how many results you want; num_candidates is how many each shard examines, trading accuracy for speed. kNN accepts a filter (in stock, in a category) applied during the search, so you still get k results.
Hybrid search: combining BM25 and vectors
Neither method wins everywhere. Keyword search is precise for exact terms (model numbers, names, rare words); vector search handles paraphrase and intent. Hybrid search combines them. In one request, the query and kNN scores add up, with boosts as weights:
GET /products/_search
{
"query": { "match": { "description": { "query": "cheap laptop for school", "boost": 0.3 } } },
"knn": { "field": "embedding", "query_vector": [0.28, 0.92, 0.05, 0.0],
"k": 3, "num_candidates": 20, "boost": 0.7 },
"size": 4
} 104 1.594 Budget Laptop
106 1.198 Chromebook 14
105 1.031 Ultrabook Pro
102 0.431 Wireless KeyboardBudget Laptop scores well on both, so it leads. The weights need tuning per use case, and the two score scales aren't naturally comparable.
Reciprocal rank fusion (RRF) combines rankings instead of scores, which avoids that problem. On the lab's free Basic license, an RRF request failed:
security_exception: current license is non-compliant for [Reciprocal Rank Fusion (RRF)]RRF and other retriever features (linear combination with normalization, semantic reranking) need a paid or trial license in Elasticsearch; OpenSearch offers hybrid search with normalization in its open-source distribution.
Practical notes for vector search
- Same model for documents and queries, and re-embed everything when you change models.
- Memory: vectors need RAM for fast search; quantization (int8, int4, BBQ) cuts it substantially.
semantic_textfields let Elasticsearch call an inference endpoint to embed text automatically at index and query time.- Evaluate: measure relevance on real queries (Day 10) before switching; vector search can return confident-looking but wrong results for exact lookups.
Check yourself
Its embedding vector was closest in meaning to the query's vector, even though its text shared few words with the query. Vector search compares meaning in embedding space, while BM25 needs shared terms.
k is the number of nearest results returned. num_candidates is how many candidates each shard examines in the approximate search; raising it improves accuracy, finding true nearest neighbors more often, at the cost of speed.
Keywords excel at exact terms like model numbers, names and codes, where vectors can be vague. Vectors capture meaning and paraphrase that keywords miss. Hybrid search, merged with reciprocal rank fusion or weighted scores, covers both.
The embedding model places texts with similar meanings close together, so the nearest neighbors share meaning even when they share no words.
Documents and queries must be embedded by the same model with the same dimensions. Changing the model requires re-embedding every document.