RousselTMLEARNING

Observability

Vector search and RAG with Elasticsearch: dense_vector, kNN, hybrid search and RRF

Build semantic search with Elasticsearch: embeddings, the dense_vector field, kNN search, semantic_text and inference, hybrid search with Reciprocal Rank Fusion, and RAG with an LLM.

1. Vectors and kNN #

Searching by meaning.

Mapping and query #

PUT docs
{ "mappings": { "properties": {
    "title":   { "type": "text", "analyzer": "french" },
    "content": { "type": "text", "analyzer": "french" },
    "content_vector": { "type": "dense_vector", "dims": 384, "index": true, "similarity": "cosine" } } } }

GET docs/_search
{ "knn": { "field": "content_vector", "query_vector": [0.12, -0.03, "..."], "k": 10, "num_candidates": 100 },
  "_source": [ "title" ] }

Embeddings are computed by a model (in your application, or in Elasticsearch via the inference API and the semantic_text type, which handles chunking and vectorization automatically).

2. Hybrid search #

The best of both.

RRF #

BM25 + kNN hybrid search merged with RRF
GET docs/_search
{
  "retriever": { "rrf": {
    "retrievers": [
      { "standard": { "query": { "match": { "content": "réinitialiser le mot de passe" } } } },
      { "knn": { "field": "content_vector", "query_vector": [0.12, -0.03, "..."], "k": 20, "num_candidates": 100 } }
    ],
    "rank_window_size": 50
  } },
  "size": 5
}
  • BM25 excels on exact terms (error codes, product names); vector search on rephrasings.
  • Reciprocal Rank Fusion combines rankings without having to calibrate incomparable scores.
  • For RAG, the 3 to 5 best passages are sent to the LLM with the question, citing their sources.
Good to know: Some features depend on the version and the license level (Basic, Platinum, Enterprise): check them in the documentation and on the Elastic subscriptions page before using them in production.

Related articles

Transforms and enrich: summarising and enriching data in Elasticsearch

Build entity-centric indices with pivot and latest transforms, and enrich documents at ingest time with enric…

OpenTelemetry and Elasticsearch: compatibility and architecture

Send OpenTelemetry to Elasticsearch: Collector and elasticsearch exporter, managed OTLP endpoint, APM Server,…

Ingesting OpenTelemetry logs into Elasticsearch

Collect and structure logs with the OpenTelemetry Collector (filelog receiver, operators, transform processor…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!