RousselTMLEARNING

Article · Elasticsearch: from discovery to expertise

Advanced Query DSL and aggregations

1. The bool query #

The basis of everything.

Score and filter #

Clauses of the bool query
GET products/_search
{
  "query": { "bool": {
    "must":     [ { "match": { "name": { "query": "chaussure trail", "operator": "and" } } } ],
    "should":   [ { "term": { "brand": { "value": "acme", "boost": 2 } } } ],
    "filter":   [ { "term": { "in_stock": true } }, { "range": { "price": { "lte": 150 } } } ],
    "must_not": [ { "term": { "status": "discontinued" } } ]
  } }
}
Good practice: Everything that is yes/no (status, date range, customer) goes in filter: faster, cached, and it does not alter relevance.

2. Relevance #

Understanding and tuning the score.

Tools #

  • The default score is BM25: term frequency in the document, rarity in the index, field length.
  • "explain": true details the computation for a document.
  • function_score combines the text score with popularity, freshness (gauss on a date) or distance.
  • nested queries each object of an array separately (otherwise « size 42 » and « color red » can come from two different lines).

3. Aggregations #

Analyzing, not just searching.

Examples #

GET logs-nginx-*/_search
{
  "size": 0,
  "query": { "range": { "@timestamp": { "gte": "now-24h" } } },
  "aggs": {
    "par_heure": {
      "date_histogram": { "field": "@timestamp", "fixed_interval": "1h" },
      "aggs": {
        "erreurs":   { "filter": { "range": { "http.response.status_code": { "gte": 500 } } } },
        "p95":       { "percentiles": { "field": "event.duration", "percents": [ 95 ] } },
        "taux":      { "bucket_script": { "buckets_path": { "e": "erreurs>_count", "t": "_count" }, "script": "params.e / params.t * 100" } }
      }
    },
    "top_paths": { "terms": { "field": "url.path", "size": 10 } },
    "visiteurs": { "cardinality": { "field": "source.ip" } }
  }
}
Good to know: cardinality is approximate (HyperLogLog); composite lets you paginate over all combinations of an aggregation, unlike terms.

ES|QL #

FROM logs-nginx-*
| WHERE @timestamp > NOW() - 24 hours AND http.response.status_code >= 500
| STATS erreurs = COUNT(*) BY url.path
| SORT erreurs DESC
| LIMIT 10

📄 Advanced Elasticsearch Query DSL and aggregations, with ES|QL →

My progress

  • ⏱ Estimated time : ≈ 6 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Recent discussions about this course

Ask a question about this lesson →See all discussions about this course

Course Glossary

RBAC (Role-Based Access Control)

An access control method where permissions are assigned to specific roles (e.g., admin, reader) rather than directly to individual users.

Authentication

The process of verifying the identity of a user or system (e.g., login/password, tokens, biometrics).

Authorization

The process that occurs after authentication to determine if the entity has the necessary rights and privileges to access a resource.

API Key

A unique alphanumeric code used to identify, authenticate, and authorize access to an Application Programming Interface (API).

Architecture

The overall structural design of an IT system, defining its various components, their relationships, and the principles guiding its evolution.

Repository

A centralized storage location. This can refer to a source code repository (e.g., Git) or an artifact and binary repository (e.g., Harbor, Nexus, Arti...

Elasticsearch

A distributed, RESTful search and analytics engine capable of addressing a growing number of use cases, built on Apache Lucene.

Recommended Articles

Indexing and searching in OpenSearch: mapping, DSL, PPL and SQL

Declare an index template, index with bulk, search with the Query DSL, aggregate and query with P...

Advanced aggregations, PPL and SQL in OpenSearch

Analyzing data in OpenSearch: bucket, metric and pipeline aggregations, date histograms, approxim...

Understanding the concept of score

The concept of score allows Elasticsearch to rank your documents by relevance during a search.

Full-text search in OpenSearch: analyzers and relevance

Mastering full-text search: inverted index, analyzers (including French), the _analyze API, match...

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, semanti...

Advanced Elasticsearch security: TLS, certificates, API keys, FLS, DLS and audit

Secure Elasticsearch in depth: transport and HTTP TLS, certificate management with elasticsearch-...

Advanced Kibana: data views, Lens, dashboards, alerting and reporting

Use Kibana in depth: data views and runtime fields, Discover, Lens and formulas, dashboards and c...

Advanced Data Prepper: routing, OpenTelemetry and reliability

Going further with Data Prepper: architecture, buffers and workers, conditional processors, routi...

Cross-cluster search and cross-cluster replication: multi-cluster Elasticsearch architectures

Design multi-cluster Elasticsearch architectures: remote clusters, cross-cluster search, cross-cl...

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!