Executing the terms aggregation
Terms aggregation is one of the most commonly used aggregations. It groups documents in buckets based on a single term value. This aggregation is often used to narrow down a search.
Getting ready
You need a working ElasticSearch cluster and an index populated with the script (chapter_06/executing_terms_aggregation.sh
) available at https://github.com/aparo/elasticsearch-cookbook-second-edition.
How to do it...
To execute a terms aggregation, we will perform the steps given as follows:
We want to calculate the top-10 tags of all the documents; for this the REST call should be as follows:
curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true&size=0' -d '{ "query": { "match_all": {} }, "aggs": { "tag": { "terms": { "field": "tag", "size": 10 } } } }'
In this example, we need to match all the items, so the
match_all
query is used.The result returned by ElasticSearch, if everything is all right...