ELK Stack — Elasticsearch Querying
Introduction
Elasticsearch’s true power lies in its querying capabilities. The Query DSL (Domain Specific Language) provides a rich, flexible JSON-based interface for searching, filtering, and analyzing your data. Whether you need full-text search across millions of log entries, precise term-level lookups, or complex aggregations for dashboards — the Query DSL has you covered.
This chapter takes you from basic search anatomy through advanced compound queries and aggregations, giving you the skills to extract meaningful insights from your Elasticsearch data.
What You’ll Learn
- How the Query DSL is structured and how query context differs from filter context
- Full-text search with
match,match_phrase, andmulti_matchqueries - Exact-value lookups with
term,terms,range, andexistsqueries - Combining conditions with
boolqueries usingmust,should,must_not, andfilter - Metric and bucket aggregations for analytics and dashboards
- Sorting results, paginating efficiently, and controlling returned fields
- Hands-on exercises to reinforce every concept
Chapter Contents
- Query DSL Basics — Query structure, search anatomy, query vs filter context, scoring fundamentals
- Match and Term Queries — Full-text search, exact-value lookups, range queries, wildcards
- Bool Queries — Compound queries, combining conditions, boosting, function_score
- Aggregations — Metrics, buckets, pipelines, nested sub-aggregations
- Sorting and Pagination — Sort options, from/size, search_after, PIT API, source filtering
- Practice Exercises — Five hands-on exercises covering all querying topics
Prerequisites
Before starting this chapter, make sure you have:
- A running Elasticsearch cluster (single node is fine)
- Kibana Dev Tools open for running queries
- Completed the Elasticsearch Core Concepts chapter (or equivalent knowledge of indices, documents, and mappings)
Sample Data
Throughout this chapter, we’ll use a sample web-logs index. You can create it by running the bulk indexing commands provided in the first section. The dataset simulates real web server access logs with fields like method, status_code, response_time, url, client_ip, and @timestamp.
Let’s start with Query DSL Basics to understand the foundation of Elasticsearch searching.