Querying Elasticsearch – Understanding Query DSL

Elasticsearch Querying Techhyme

Elasticsearch is a powerful search and analytics engine that allows you to search, analyze, and manage your data efficiently. When interacting with Elasticsearch, you often use the REST API to send requests in JSON format. This approach applies not only to index creation, alias management, or document indexing but also to querying data.

In Elasticsearch, querying data is a vital operation, and this is where Query DSL comes into play.

Query DSL Overview

Query DSL, which stands for Query Domain Specific Language, is a structured way of crafting queries in Elasticsearch. It allows you to send complex queries to Elasticsearch, filter and search your data based on specific criteria, and obtain accurate results. Query DSL is used for both basic queries and compound queries, as well as for applying filters.

Basic Queries

Basic queries, such as the term query, are used solely for querying data. These queries search for documents that contain a specific term in a specified field. For example, if you want to find documents with the word “crime” in the “title” field, you can use a term query. The key point to remember is that term queries are not analyzed, so you need to provide the exact term you’re searching for. Here’s an example of a term query in Query DSL:

  "query": {
    "term": {
      "title": "crime"

Compound Queries

Compound queries, such as the bool query, are designed to combine multiple queries. These queries allow you to create complex search conditions by incorporating various basic queries. We will delve deeper into compound queries in a dedicated section in this article.


Filters are used to narrow down your search results based on specific criteria. They are similar to queries but are typically used for filtering rather than searching. Filters are often used to enhance the performance of queries by eliminating unnecessary data from the search scope.

Combining Queries and Filters

In Elasticsearch, you have the flexibility to create queries that include other queries or filters. You can also combine both queries and filters within a single query operation. This capability offers granular control over your search and filter conditions.

Simple Query with REST API

The simplest way to query Elasticsearch is by using the URI request query in a REST API request. For instance, to search for the word “crime” in the “title” field of a specific index and type, you can use a URI request query like this:

curl -XGET 'localhost:9200/library/book/_search?q=title:crime&pretty=true'

While this approach is straightforward, it is essential to understand that Elasticsearch performs analysis on the data during query execution. Therefore, to perform an exact term search, you should use the term query in Query DSL.

Querying with Query DSL

To perform a query using Query DSL, you structure your query as a JSON object and send it to Elasticsearch. Here’s an example of the previous query, but this time expressed in Query DSL:

  "query": {
    "term": {
      "title": "crime"

To send this query to Elasticsearch, you use an HTTP GET request to the `_search` endpoint of your chosen index and type. The request body, containing your JSON-structured query, can be sent using the `-d` switch in the cURL command. The `pretty=true` request parameter is optional but makes the response more human-readable.

Addressing Indices and Types

When executing queries in Elasticsearch, you can direct your query to specific indices and types or address multiple indices and types simultaneously. Here are some examples of addressing options:

– Request to index and type:

curl -XGET 'localhost:9200/library/book/_search' -d @query.json

– Request to index and all types in it:

curl -XGET 'localhost:9200/library/_search' -d @query.json

– Request to all indices:

curl -XGET 'localhost:9200/_search' -d @query.json

– Request to multiple indices:

curl -XGET 'localhost:9200/library,bookstore/_search' -d @query.json

– Request to multiple indices and multiple types:

curl -XGET 'localhost:9200/library,bookstore/book,recipes/_search' -d @query.json


Querying Elasticsearch using Query DSL is an essential skill for anyone working with Elasticsearch. By crafting structured queries, you can effectively search, filter, and analyze your data to extract valuable insights. Understanding the difference between basic queries and compound queries, as well as the use of filters, provides you with the tools needed to harness the full power of Elasticsearch for your data retrieval needs.

Whether you’re a developer, data analyst, or a business professional, Elasticsearch’s powerful querying capabilities can help you extract meaningful information from your data quickly and efficiently.

You may also like:

Related Posts

Leave a Reply