Executing a search with aggregations
The previous recipe can be extended to support aggregations in order to retrieve analytics on indexed data.
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 AggregationExample
.
How to do it...
To execute a search with aggregations, we will perform the following steps:
- We need to import the necessary classes for the aggregations using the following code:
import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate; import co.elastic.clients.elasticsearch._types.aggregations...