Elasticsearch Query DSL for complex searching.
Match¶
GET /articles/_search
{
"query": {
"match": {
"title": "elasticsearch tutorial"
}
}
}
Bool¶
{
"query": {
"bool": {
"must": [{"match": {"content": "kubernetes"}}],
"filter": [
{"term": {"tags": "devops"}},
{"range": {"published_at": {"gte": "2024-01-01"}}}
]
}
}
}
Aggregations¶
{
"size": 0,
"aggs": {
"by_tag": {"terms": {"field": "tags", "size": 20}},
"avg_views": {"avg": {"field": "views"}}
}
}
Practical Query Tips¶
In production environments, always use filter context instead of must for conditions that do not need scoring (date, status, type). Filters are cached and significantly faster. A bool query combining must for relevance and filter for constraints is the most common pattern.
For paginating large result sets, avoid deep pagination (from + size > 10,000). Instead, use search_after with the point-in-time API, which is more efficient and consistent. Aggregations are a powerful tool for analytics — they allow you to calculate statistics, create histograms, and run bucket analyses directly in Elasticsearch without needing to export data to an external tool.
Query DSL = Powerful¶
Bool queries + aggregations cover most needs.