Catalog My learning Blog
Elasticsearch Tutorial: How Full-Text Search Works · Phase 3 · Lesson 21 · 4 min read

Elasticsearch Vector Search and Semantic Search

On this page
  1. Why keyword search misses meaning
  2. What are embeddings?
  3. Running a kNN vector query
  4. Hybrid search: combining BM25 and vectors
  5. Practical notes for vector search
  6. Check yourself

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.

Code
"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

HTTP
GET /products/_search
{
  "knn": {
    "field": "embedding",
    "query_vector": [0.28, 0.92, 0.05, 0.0],
    "k": 3,
    "num_candidates": 20
  },
  "_source": ["name"]
}
Code
total 3
  106    1.000   Chromebook 14
  104    0.999   Budget Laptop
  105    0.949   Ultrabook Pro

The 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.

Documents and queries are turned into vectors by an embedding model; nearest-neighbor search finds the closest document vectors, and hybrid search combines that with keyword relevance.
Documents and queries are turned into vectors by an embedding model; nearest-neighbor search finds the closest document vectors, and hybrid search combines that with keyword relevance.

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:

HTTP
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
}
Code
  104    1.594   Budget Laptop
  106    1.198   Chromebook 14
  105    1.031   Ultrabook Pro
  102    0.431   Wireless Keyboard

Budget 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:

Code
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.

  • 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_text fields 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.