Elasticsearch Elasticsearch Query Builder

By Opster Team

Updated: Aug 17, 2023

| 2 min read

Quick links

Introduction

Elasticsearch Query Builder is a robust tool that allows developers to construct complex queries in a more manageable and readable format. It provides a structured approach to query building, making it easier to build, test, and debug queries. This article will delve into the intricacies of Elasticsearch Query Builder, providing examples and step-by-step instructions to help you harness its full potential.

The Elasticsearch Query Builder is a part of the Elasticsearch DSL (Domain Specific Language), which is designed to construct queries in a structured manner. It provides a set of classes and methods that can be used to build queries in a programmatic way. 

How to use match query 

Let’s start with a simple example of a match query. A match query is a standard query that is used to search for a specific term in a document. Here’s how you can build a match query using the Elasticsearch Query Builder:

QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("field", "value");

In the above example, “field” is the name of the field you want to search (e.g. “username”), and “value” is the term you’re looking for (e.g. “johndoe”). The QueryBuilders.matchQuery method returns a QueryBuilder object, which can be used to build more complex queries.

How to use bool query

Now, let’s move on to a more complex example. Suppose you want to search for documents that contain a specific term in one field and a range of values in another field. You can use the bool query for this purpose. A bool query allows you to combine multiple queries in a logical manner. Here’s how you can build a bool query using the Elasticsearch Query Builder:

QueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
    .must(QueryBuilders.matchQuery("field1", "value"))
    .filter(QueryBuilders.rangeQuery("field2").from("value1").to("value2"));

In the above example, the must method is used to add a match query, and the filter method is used to add a range query. The match query will search for documents that contain the term “value” in the field “field1”, and the range query will filter out documents that don’t fall within the range of “value1” to “value2” in the field “field2”.

If you want to learn more about Elasticsearch Boolean Queries, check out this guide

How to use aggregation queries

The Elasticsearch Query Builder also supports aggregation queries. Aggregation queries allow you to compute aggregate data such as sum, average, min, max, etc. Here’s how you can build an aggregation query using the Elasticsearch Query Builder:

AggregationBuilder aggregationBuilder = AggregationBuilders.sum("agg").field("field");

In the above example, the AggregationBuilders.sum method is used to create a sum aggregation. The “agg” parameter is the name of the aggregation, and “field” is the name of the field to aggregate.

Conclusion

As shown in this article, the Elasticsearch Query Builder is a very powerful tool that allows developers to build complex queries without having to resort to writing DSL queries in JSON. The Query Builder provides support for composing search and aggregation queries in a very simple way. The examples shown were written in Java, but the same Query Builder exists in other programming languages, such as PHP, JavaScript, etc.