Elasticsearch Vector Search
Keyword search matches words. A visitor who types "how to fix a flat bike tire" misses an article titled "Repairing a punctured bicycle wheel," because the two phrases share almost no words. Vector search solves the problem by comparing meaning. Documents and queries turn into lists of numbers, and Elasticsearch finds the entries whose numbers sit closest together.
What Is an Embedding
An embedding is a list of numbers that represents the meaning of a text, image, or other item. A machine learning model creates it. Similar meanings produce similar numbers.
Picture a map. Cities near each other share regions. Embeddings place related ideas near each other on a very large map with hundreds of directions instead of two.
Simplified 2-direction map:
"puppy" • • "dog"
"kitten" • • "cat"
"car" • • "truck"
Close points = similar meaning.
Query "dog" lands near "puppy", far from "truck".
How a Vector Search Runs
INDEXING SEARCHING
Document text User question
│ │
▼ ▼
[ Embedding model ] [ Same embedding model ]
│ │
▼ ▼
[0.12, -0.80, 0.33, ...] [0.10, -0.77, 0.30, ...]
│ │
▼ ▼
Stored in a dense_vector field ←── Find the nearest stored vectors
Both sides must use the same embedding model. Vectors from different models live on different maps and cannot be compared.
Mapping a Vector Field
The dense_vector type stores the numbers. The dims value must match the output size of the model. The similarity setting chooses the distance measure.
PUT /articles
{
"mappings": {
"properties": {
"title": { "type": "text" },
"title_vec": {
"type": "dense_vector",
"dims": 3,
"index": true,
"similarity": "cosine"
}
}
}
}
Real models produce hundreds or thousands of dimensions, such as 384 or 768. The tiny value 3 keeps the example readable.
| Similarity | Measures | Typical Use |
|---|---|---|
| cosine | Angle between two vectors | Text embeddings |
| dot_product | Alignment, expects unit-length vectors | Normalized embeddings, speed |
| l2_norm | Straight-line distance | Image and numeric features |
Indexing Documents with Vectors
POST /articles/_doc/1
{ "title": "Repairing a punctured bicycle wheel", "title_vec": [0.91, 0.12, 0.35] }
POST /articles/_doc/2
{ "title": "Baking sourdough at home", "title_vec": [0.05, 0.88, 0.40] }
Running a kNN Search
The kNN search returns the k documents closest to the query vector. The num_candidates setting controls how many candidates each shard examines. Higher values improve accuracy and cost more time.
GET /articles/_search
{
"knn": {
"field": "title_vec",
"query_vector": [0.88, 0.15, 0.30],
"k": 3,
"num_candidates": 50
},
"_source": ["title"]
}
The query vector comes from the same model that created the stored vectors, applied to the visitor's question.
Approximate Search with HNSW
Comparing a query against every stored vector takes too long on large data. Elasticsearch builds an HNSW graph, a network that links each vector to its neighbors. A search hops through the graph toward the closest region and skips most of the data.
Exact search: compare with 10,000,000 vectors
Approximate (HNSW): hop through the graph, compare with a few thousand
Result: nearly the same answers, many times faster
The method trades a small amount of accuracy for large gains in speed. Tuning num_candidates adjusts that balance.
Adding Filters
A filter inside the kNN section limits candidates to matching documents before ranking. A shop can ask for similar items that are in stock and under a set price.
"knn": {
"field": "title_vec",
"query_vector": [0.88, 0.15, 0.30],
"k": 5,
"num_candidates": 100,
"filter": { "term": { "in_stock": true } }
}
Hybrid Search
Keyword search and vector search each have strengths. Keywords excel at exact names, codes, and rare terms. Vectors excel at paraphrases and intent. A single request can run both and blend the scores.
GET /articles/_search
{
"query": { "match": { "title": { "query": "bike tire repair", "boost": 0.4 } } },
"knn": {
"field": "title_vec",
"query_vector": [0.88, 0.15, 0.30],
"k": 5,
"num_candidates": 50,
"boost": 0.6
}
}
Reciprocal rank fusion (RRF) offers another way to merge the two result lists, based on position rather than raw score. Availability depends on your license level.
Where Vector Search Helps
| Use Case | What Gets Compared |
|---|---|
| Semantic site search | Question meaning against article meaning |
| Similar product suggestions | Product description vectors |
| Image similarity | Image embeddings |
| Question answering over documents | Question against document passages |
| Duplicate detection | Near-identical text vectors |
Cost and Planning Notes
Vectors consume memory. A vector with 768 numbers takes about 3 KB, and a million documents need roughly 3 GB for the vectors alone, plus space for the HNSW graph. Plan node memory accordingly, and consider quantization options that shrink vectors at a small accuracy cost. Split long documents into passages before embedding, since one vector cannot capture a whole book.
