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
| Clause | Scoring Impact | Purpose / Meaning |
|---|---|---|
must | Contributes to score (_score) | Query MUST appear in matching documents |
filter | Cached, NO score impact | Query MUST appear, used for fast binary filtering |
should | Contributes to score | Query SHOULD appear (increases relevance score) |
must_not | NO score impact | Query 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 Type | JSON Example | Use 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
matchqueries onkeywordfields or exacttermqueries on analyzedtextfields will often produce unexpected empty results!
[!TIP] Place non-scoring conditions (such as status, dates, user IDs) inside the
filterblock of aboolquery; Elasticsearch automatically caches filter clauses for maximum search performance.