Executing a standard search
In the previous recipe, we learned how to build queries. In this recipe, we will execute a query to retrieve some documents.
Getting ready
You need an up-and-running Elasticsearch installation, as we 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 the QueryExample
.
How to do it...
To execute a standard query, we will perform the following steps:
- We need to import
SearchRequest.QueryBuilder
to create the query, as follows:import co.elastic.clients.elasticsearch.core.SearchRequest;
- We can create an index and populate it with some data, as follows:
String index = "mytest"; QueryHelper qh = new QueryHelper(); qh.populateData(index...