Executing a search with aggregations
Searching for results is obviously the main activity for a search engine; thus a aggregations are very important because they often help to augment the results.
Aggregations are executed along the search by performing analytics on searched results.
Getting ready
You need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
You also need the Python installed packages of the Creating a client recipe of this chapter.
The code of this recipe can be found in the chapter_16/aggregation.py
file.
How to do it…
To extend a query with the aggregations part, you need to define an aggregation section, as we have already seen in Chapter 8, Aggregations. In the case of the official Elasticsearch client, you can add the aggregation DSL to the search dictionary to provide aggregations. We will perform the following steps:
We initialize the client and populate the index:
...