Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Executing a search with aggregations


The previous recipe can be extended to support aggregations and to retrieve analytics on indexed data.

Getting ready

You will need a working ElasticSearch cluster and a working copy of Maven.

The code of this recipe is in chapter_10/nativeclient folder in the code bundle of this chapter available on Packt's website, and on GitHub (https://github.com/aparo/elasticsearch-cookbook-second-edition). The referred class is AggregationExample.

How to do it...

To execute a search with aggregations, we will perform the following steps:

  1. We'll calculate two different aggregations (terms and extended statistics) as follows:

    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.search.aggregations.AggregationBuilder;
    import org.elasticsearch.search.aggregations.bucket.terms.Terms;
    import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
    import org.elasticsearch.search.aggregations...