Executing a search with aggregations
The next step after searching in Elasticsearch is to execute the aggregations. The Elastic4s DSL also provides support for aggregation so that it can be built in a safer typed way.
Getting ready
You need an up-and-running Elasticsearch installation, as described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
Additionally, an IDE that supports Scala programming, such as IntelliJ IDEA, with the Scala plugin should be installed globally.
The code for this recipe can be found in the ch14/elastic4s_sample
file; 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 classes that are needed for the aggregations:
import com.sksamuel.elastic4s.ElasticDsl._ import com.sksamuel.elastic4s.requests.searches.aggs.responses.bucket.Terms
- We will create an index and populate it with some data that...