ELK Stack — Elasticsearch Querying

1 minute read

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, and multi_match queries
  • Exact-value lookups with term, terms, range, and exists queries
  • Combining conditions with bool queries using must, should, must_not, and filter
  • 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

  1. Query DSL Basics — Query structure, search anatomy, query vs filter context, scoring fundamentals
  2. Match and Term Queries — Full-text search, exact-value lookups, range queries, wildcards
  3. Bool Queries — Compound queries, combining conditions, boosting, function_score
  4. Aggregations — Metrics, buckets, pipelines, nested sub-aggregations
  5. Sorting and Pagination — Sort options, from/size, search_after, PIT API, source filtering
  6. 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.