Building a query
Before a search, a query must be built. Elasticsearch provides several ways to build these queries. In this recipe, we will learn how to create a query object via QueryBuilder
and simple strings.
Getting ready
You need an up-and-running Elasticsearch installation, which you can get as described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.
A Maven tool or an IDE that natively supports it for Java programming, such as Visual Studio Code, Eclipse, or IntelliJ IDEA, must be installed.
The code for this recipe is in the ch13/elasticsearch-java-client
directory and the referred class is QueryCreation
.
How to do it...
To create a query, we will perform the following steps:
- We need to import
SearchRequest
using the following code:import co.elastic.clients.elasticsearch.core.SearchRequest;
- Next, we'll create a query using
SearchRequest
, as follows:SearchRequest searchRequest = new SearchRequest.Builder...