Course contents

Vector and hybrid search

Compare lexical and semantic retrieval and understand its costs.

4 sectionsElasticsearch 9.4.3Not read yet

What you will learn

  • Representing meaning
  • Candidates and filters
  • Hybrid retrieval and RAG

Compare lexical and semantic retrieval and understand its costs.

How it works
  • Lexical and vector candidates
  • Rank fusion
  • Rerank → results

Compare lexical and semantic retrieval and understand its costs.

The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.

01 / 04

Representing meaning

An embedding maps text to a numerical representation where a chosen metric expresses similarity. Dense and sparse representations have different structures and requirements. Models must fit language and domain; changing models normally requires reprocessing data. Similarity does not guarantee factual correctness, an exact SKU or product availability.

Check your understanding
02 / 04

Candidates and filters

Approximate kNN trades recall, memory and indexing work for faster retrieval. k controls returned neighbors, while num_candidates affects candidate exploration in applicable APIs. Pre-filtering constrains eligible neighbors during retrieval; post-filtering can leave fewer than k hits. Dimensions, similarity and index settings must fit the model. semantic_text simplifies managed workflows without removing inference cost.

Check your understanding
03 / 04

Hybrid retrieval and RAG

Hybrid retrieval combines lexical and semantic candidates. RRF uses ranks instead of directly adding incompatible scores; reranking applies a costlier model to a smaller set. Chunking long documents must preserve provenance, access and context. In RAG, retrieval and generation quality need separate evaluation. Verify inference endpoints, RRF and reranking availability for the deployment and license.

Three dimensions illustrate the schema only; production dimensions come from the embedding model.

PUT /course-products-vectors
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "embedding": {
        "type": "dense_vector",
        "dims": 3,
        "similarity": "cosine"
      },
      "in_stock": {
        "type": "boolean"
      }
    }
  }
}

How to read this result

Three dimensions illustrate the schema only; production dimensions come from the embedding model.

Check your understanding
04 / 04

Catalog reasoning example

For “what to wear running in the rain”, vector retrieval may find clothing without literal overlap. For RUN-042 it may return similar models instead of the exact SKU. Evaluate these scenarios separately. Enforce identical access and stock requirements across hybrid branches. A model-outage lexical fallback must preserve the contract and appear in metrics. Compare NDCG gains with p95 and embedding cost.

A document with a teaching vector

PUT /course-products-vectors/_doc/sku-1?refresh=wait_for
{
  "name": "Rain jacket",
  "in_stock": true,
  "embedding": [
    0.1,
    0.2,
    0.9
  ]
}

How to read this result

The vector is hand-picked to demonstrate mechanics. Real documents and queries require a compatible embedding model; arbitrary numbers do not create semantic search.

kNN with a mandatory filter

POST /course-products-vectors/_search
{
  "size": 5,
  "knn": {
    "field": "embedding",
    "query_vector": [
      0.1,
      0.2,
      0.9
    ],
    "k": 5,
    "num_candidates": 50,
    "filter": {
      "term": {
        "in_stock": true
      }
    }
  }
}

How to read this result

The filter constrains eligible neighbors inside kNN. Fewer than five indexed eligible documents can produce fewer than five hits; k does not guarantee that enough suitable products exist.

Check your understanding

Check your understanding

Answer before expanding each explanation.

1. Does an embedding guarantee exact SKU lookup?

No, identifiers need an exact lexical path.

2. What can approximate kNN lose?

Some true nearest neighbors; measure recall.

3. Why can post-filter return fewer than k?

It removes some neighbors after selection.

4. What does RRF combine?

Result ranks from multiple retrieval branches.

5. Why preserve access after chunking?

Chunks must not expose an inaccessible source document.

Key takeaways

  • No, identifiers need an exact lexical path.
  • Some true nearest neighbors; measure recall.
  • It removes some neighbors after selection.
  • Result ranks from multiple retrieval branches.
  • Chunks must not expose an inaccessible source document.

Official reference

Ready to put this into practice?

Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.