Elasticsearch Query DSL Cheat Sheet

Quick reference guide for Elasticsearch: Query DSL, bool queries, index mappings, and cluster health APIs.

Databases
elasticsearch
search
query-dsl

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases using JSON queries.

`bool` Query Clause Structure

The bool query matches documents that match boolean combinations of other queries.

Table
ClauseScoring ImpactPurpose / Meaning
mustContributes to score (_score)Query MUST appear in matching documents
filterCached, NO score impactQuery MUST appear, used for fast binary filtering
shouldContributes to scoreQuery SHOULD appear (increases relevance score)
must_notNO score impactQuery MUST NOT appear in matching documents
json
POST /products/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "laptop" } }
      ],
      "filter": [
        { "term": { "status": "active" } },
        { "range": { "price": { "gte": 500, "lte": 2000 } } }
      ],
      "must_not": [
        { "term": { "category": "refurbished" } }
      ]
    }
  }
}

Common Query DSL Types

Table
Query TypeJSON ExampleUse Case
term{ "term": { "user.id": "kimchy" } }Exact match on keyword fields
match{ "match": { "message": "this is a test" } }Full-text search on analyzed text fields
range{ "range": { "age": { "gte": 21 } } }Greater/less than range filtering
multi_match{ "multi_match": { "query": "guide", "fields": ["title", "body"] } }Match search query across multiple fields
wildcard{ "wildcard": { "user.id": "ki*y" } }Wildcard pattern matching

Index Management & Cluster REST APIs

http
# Check Cluster Health
GET /_cluster/health

# Create Index with Mapping
PUT /my-index
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "sku": { "type": "keyword" },
      "created_at": { "type": "date" }
    }
  }
}

# Delete Index
DELETE /my-index

Common Pitfalls & Tips

[!WARNING] Running full-text match queries on keyword fields or exact term queries on analyzed text fields will often produce unexpected empty results!

[!TIP] Place non-scoring conditions (such as status, dates, user IDs) inside the filter block of a bool query; Elasticsearch automatically caches filter clauses for maximum search performance.