{"id":3029,"date":"2026-10-27T13:46:49","date_gmt":"2026-10-27T13:46:49","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=3029"},"modified":"2026-10-28T12:58:41","modified_gmt":"2026-10-28T12:58:41","slug":"aggregations-in-elasticsearch","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/","title":{"rendered":"Aggregations in ElasticSearch:"},"content":{"rendered":"<h1><strong><b>Aggregations in Elasticsearch: Types, Syntax and Examples<\/b><\/strong><\/h1>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You have a million documents in an index, and a manager asks a simple question: how many cars did we list for each make, and what is the average price of each? You do not need to read a million documents to answer. Elasticsearch can group them and do the maths inside the cluster, and return a few lines of numbers. That is what aggregations are for.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">An aggregation summarises your data. It can count documents in groups, calculate an average or a total, show how prices spread over ranges, or work on the results of other aggregations. If you have used GROUP BY in SQL, the idea will feel familiar. Aggregations are also the engine behind the charts and tables in Kibana.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">In this guide we explain the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">aggs<\/code> request, and the three types of aggregations. We then build a small index of cars, and run the three examples from the original page, this time with the full request text, so you can copy them. After that we add the things that most beginners miss: size 0, the keyword field rule, the default of 10 buckets, query scope, range and histogram, and a pipeline example.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">What is the aggs Query in Elasticsearch?<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Every aggregation lives inside the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">aggs<\/code> parameter of a search request. You give each aggregation a name of your choice, and the name appears again in the response. The simplest form looks like this, with a placeholder name and field:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">json<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">{\r\n  \"aggs\": {\r\n    \"my_agg_name\": {\r\n      \"terms\": { \"field\": \"my_field\" }\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The word <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">aggs<\/code> is a short form. You can also write aggregations in full, and both work. Elastic also documents that you can set size to 0 when you want only the aggregation results, and no search hits. We use that in every example below.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Types of Aggregations in Elasticsearch<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Elasticsearch groups aggregations into three families.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 900px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">The three families of aggregations in Elasticsearch<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 160px;\" scope=\"col\">Family<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">What it does<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Examples<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Bucket<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Groups documents into buckets, based on field values, ranges or other rules. Each bucket has a document count<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">terms, range, histogram, date_histogram<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Metric<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Calculates a value from the field values, such as a sum or an average<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">avg, min, max, sum, value_count, stats, cardinality<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Pipeline<\/th>\n<td style=\"padding: 12px 16px; vertical-align: top;\">Takes its input from other aggregations, and not from documents or fields<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">avg_bucket, sum_bucket, derivative<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The old page said that a metric aggregation computes a single statistic. That is true for avg, min, max and sum, which return one value. But stats returns five values, which are count, min, max, avg and sum. So it is better to say that a metric aggregation calculates numbers from field values, and that some return one value while others return several.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Set Up Sample Data<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The old page used a cars example, and showed the queries only as pictures. To make the examples copyable, here is a small index of 10 cars. Run these two requests in Kibana Dev Tools. The first creates the index with the right field types. The second adds the documents. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">refresh=true<\/code> setting makes them searchable at once.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">PUT cars\r\n{\r\n  \"mappings\": {\r\n    \"properties\": {\r\n      \"make\":  { \"type\": \"keyword\" },\r\n      \"model\": { \"type\": \"keyword\" },\r\n      \"color\": { \"type\": \"keyword\" },\r\n      \"year\":  { \"type\": \"integer\" },\r\n      \"price\": { \"type\": \"integer\" }\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">POST cars\/_bulk?refresh=true\r\n{\"index\": {\"_id\": \"1\"}}\r\n{\"make\": \"Toyota\", \"model\": \"Corolla\", \"year\": 2022, \"price\": 21000, \"color\": \"white\"}\r\n{\"index\": {\"_id\": \"2\"}}\r\n{\"make\": \"Toyota\", \"model\": \"Camry\", \"year\": 2023, \"price\": 28000, \"color\": \"black\"}\r\n{\"index\": {\"_id\": \"3\"}}\r\n{\"make\": \"Toyota\", \"model\": \"Fortuner\", \"year\": 2021, \"price\": 41000, \"color\": \"white\"}\r\n{\"index\": {\"_id\": \"4\"}}\r\n{\"make\": \"Toyota\", \"model\": \"Yaris\", \"year\": 2020, \"price\": 14000, \"color\": \"silver\"}\r\n{\"index\": {\"_id\": \"5\"}}\r\n{\"make\": \"Honda\", \"model\": \"City\", \"year\": 2022, \"price\": 19000, \"color\": \"silver\"}\r\n{\"index\": {\"_id\": \"6\"}}\r\n{\"make\": \"Honda\", \"model\": \"Civic\", \"year\": 2023, \"price\": 26000, \"color\": \"black\"}\r\n{\"index\": {\"_id\": \"7\"}}\r\n{\"make\": \"Honda\", \"model\": \"Amaze\", \"year\": 2021, \"price\": 12000, \"color\": \"white\"}\r\n{\"index\": {\"_id\": \"8\"}}\r\n{\"make\": \"Ford\", \"model\": \"Figo\", \"year\": 2020, \"price\": 11000, \"color\": \"white\"}\r\n{\"index\": {\"_id\": \"9\"}}\r\n{\"make\": \"Ford\", \"model\": \"Endeavour\", \"year\": 2022, \"price\": 44000, \"color\": \"grey\"}\r\n{\"index\": {\"_id\": \"10\"}}\r\n{\"make\": \"BMW\", \"model\": \"X1\", \"year\": 2023, \"price\": 52000, \"color\": \"black\"}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Notice that make, model and color are keyword fields, and year and price are integers. This matters. The terms aggregation works on keyword fields, and we explain why below.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 640px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">The sample index of 10 cars<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 160px;\" scope=\"col\">Make<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Model<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Year<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Price<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Color<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Toyota<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Corolla<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2022<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">21000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">white<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Toyota<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Camry<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2023<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">28000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">black<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Toyota<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Fortuner<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2021<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">41000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">white<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Toyota<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Yaris<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2020<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">14000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">silver<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Honda<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">City<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2022<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">19000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">silver<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Honda<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Civic<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2023<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">26000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">black<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Honda<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Amaze<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2021<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">12000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">white<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Ford<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Figo<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2020<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">11000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">white<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Ford<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Endeavour<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">2022<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">44000<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">grey<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; vertical-align: top;\" scope=\"row\">BMW<\/th>\n<td style=\"padding: 12px 16px; vertical-align: top;\">X1<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">2023<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">52000<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">black<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The responses below are shown for this data, and trimmed to the aggregations part. A real response also has fields such as took and hits.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Example 1: Group Documents With a Bucket Aggregation<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The first example from the old page groups the cars by make, in an aggregation called <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">popular_cars<\/code>. The terms aggregation makes one bucket for each different value in the field, and counts the documents in each.<\/p>\n<div style=\"margin: 16px 0 24px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png\" alt=\"Screenshot of the old page example, a terms aggregation named popular_cars that groups cars by make\" \/><\/div>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": {\r\n      \"terms\": { \"field\": \"make\" }\r\n    }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3\r\n        },\r\n        {\r\n          \"key\": \"Ford\",\r\n          \"doc_count\": 2\r\n        },\r\n        {\r\n          \"key\": \"BMW\",\r\n          \"doc_count\": 1\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Read the response like this. There are 4 Toyota cars, 3 Honda, 2 Ford and 1 BMW. The buckets are sorted with the biggest count first, which is the default. The field <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">sum_other_doc_count<\/code> is 0, which means no documents were left out. The field <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">doc_count_error_upper_bound<\/code> is also 0, which means the counts are exact here. We explain both below.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Why size 0?<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A search request returns the top 10 matching documents by default, as well as the aggregations. If you only want the numbers, setting size to 0 skips the documents and keeps the response small. Elastic also notes that using size 0 together with the same preference string for each search can let you use the shard request cache for repeat queries.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">To see how it looks when you do want both, here is a request that returns the two most expensive cars, and the make counts, in one response:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 2,\r\n  \"sort\": [ { \"price\": \"desc\" } ],\r\n  \"_source\": [ \"make\", \"model\", \"price\" ],\r\n  \"aggs\": {\r\n    \"popular_cars\": { \"terms\": { \"field\": \"make\" } }\r\n  }\r\n}<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Example 2: Run Multiple Aggregations Together<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You can put several aggregations in one request, and Elasticsearch runs them all on the same set of documents. The old example ran <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">popular_cars<\/code> with <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">avg_price<\/code>, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">max_price<\/code> and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">min_price<\/code>.<\/p>\n<div style=\"margin: 16px 0 24px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/multiple-aggs.png\" alt=\"Screenshot of the old page example that runs popular_cars with avg_price, max_price and min_price in one request\" \/><\/div>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": { \"terms\": { \"field\": \"make\" } },\r\n    \"avg_price\":    { \"avg\": { \"field\": \"price\" } },\r\n    \"max_price\":    { \"max\": { \"field\": \"price\" } },\r\n    \"min_price\":    { \"min\": { \"field\": \"price\" } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3\r\n        },\r\n        {\r\n          \"key\": \"Ford\",\r\n          \"doc_count\": 2\r\n        },\r\n        {\r\n          \"key\": \"BMW\",\r\n          \"doc_count\": 1\r\n        }\r\n      ]\r\n    },\r\n    \"avg_price\": {\r\n      \"value\": 26800.0\r\n    },\r\n    \"max_price\": {\r\n      \"value\": 52000.0\r\n    },\r\n    \"min_price\": {\r\n      \"value\": 11000.0\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The three metric aggregations work on all 10 cars. The average is 26800, the highest price is 52000 and the lowest is 11000. Note that metric values come back as decimals, such as 26800.0, even for an integer field. Also note that these three numbers are not per make. They cover the whole set, because the metric aggregations sit next to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">popular_cars<\/code> and not inside it. To get them per make, you need nesting, which is Example 3.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Example 3: Nest an Aggregation Inside Another<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A bucket aggregation can hold other aggregations inside it, and they run once for every bucket. These are called sub aggregations. Here the stats aggregation runs inside each make bucket. The Elastic documentation says that there is no limit on how deep you can nest, but each level adds work, so keep it as shallow as you can.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": {\r\n      \"terms\": { \"field\": \"make\" },\r\n      \"aggs\": {\r\n        \"stats_price\": { \"stats\": { \"field\": \"price\" } }\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4,\r\n          \"stats_price\": {\r\n            \"count\": 4,\r\n            \"min\": 14000.0,\r\n            \"max\": 41000.0,\r\n            \"avg\": 26000.0,\r\n            \"sum\": 104000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3,\r\n          \"stats_price\": {\r\n            \"count\": 3,\r\n            \"min\": 12000.0,\r\n            \"max\": 26000.0,\r\n            \"avg\": 19000.0,\r\n            \"sum\": 57000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Ford\",\r\n          \"doc_count\": 2,\r\n          \"stats_price\": {\r\n            \"count\": 2,\r\n            \"min\": 11000.0,\r\n            \"max\": 44000.0,\r\n            \"avg\": 27500.0,\r\n            \"sum\": 55000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"BMW\",\r\n          \"doc_count\": 1,\r\n          \"stats_price\": {\r\n            \"count\": 1,\r\n            \"min\": 52000.0,\r\n            \"max\": 52000.0,\r\n            \"avg\": 52000.0,\r\n            \"sum\": 52000.0\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Now every make has its own count, minimum, maximum, average and sum. For example, the 4 Toyotas have an average price of 26000, and the single BMW has an average of 52000. The old page said that stats gives the average, minimum, maximum and sum. As you can see, it also gives the count.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Sort Buckets by a Sub Aggregation<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">By default the buckets are sorted by document count. You can sort them by the result of a sub aggregation instead. Here the makes are ordered by average price, with the most expensive first.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": {\r\n      \"terms\": { \"field\": \"make\", \"order\": { \"avg_price\": \"desc\" } },\r\n      \"aggs\": {\r\n        \"avg_price\": { \"avg\": { \"field\": \"price\" } }\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"BMW\",\r\n          \"doc_count\": 1,\r\n          \"avg_price\": {\r\n            \"value\": 52000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Ford\",\r\n          \"doc_count\": 2,\r\n          \"avg_price\": {\r\n            \"value\": 27500.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4,\r\n          \"avg_price\": {\r\n            \"value\": 26000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3,\r\n          \"avg_price\": {\r\n            \"value\": 19000.0\r\n          }\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The Elastic documentation advises care with ordering by a sub aggregation, and warns that ordering by ascending count gives inaccurate results. For rare values, it points to the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">rare_terms<\/code> aggregation.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">How Many Buckets Do You Get? The Size Setting<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A terms aggregation returns the top 10 terms by default. Use size to get more or fewer, up to the limit set by the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">search.max_buckets<\/code> setting of the cluster. Here is a request for only the top 2 makes:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": { \"terms\": { \"field\": \"make\", \"size\": 2 } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 3,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Now <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">sum_other_doc_count<\/code> is 3. This is the number of documents in the terms that did not make it into the top 2. Ford has 2 and BMW has 1, so 3 documents were left out. If this number is above 0 in your own data, some buckets were dropped, and you should raise size.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Limit What You Aggregate With a Query<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Aggregations run on the documents that match the query. If you add a query, the aggregation sees only those documents. Here we count the colours of cars that cost at least 20000:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"query\": { \"range\": { \"price\": { \"gte\": 20000 } } },\r\n  \"aggs\": {\r\n    \"colors\": { \"terms\": { \"field\": \"color\" } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"colors\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"black\",\r\n          \"doc_count\": 3\r\n        },\r\n        {\r\n          \"key\": \"white\",\r\n          \"doc_count\": 2\r\n        },\r\n        {\r\n          \"key\": \"grey\",\r\n          \"doc_count\": 1\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Four cars cost less than 20000, so they are left out of the counts. Without the query, white would have 4.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">More Metric Aggregations<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Besides avg, min, max and stats, three more are used all the time.<\/p>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; margin: 0 0 16px; padding-left: 24px; font-size: 17px; line-height: 1.7; color: #1f2937;\">\n<li style=\"margin: 0 0 8px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">sum<\/code> adds up the values of a field.<\/li>\n<li style=\"margin: 0 0 8px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">value_count<\/code> counts how many values the field has.<\/li>\n<li style=\"margin: 0 0 8px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">cardinality<\/code> counts how many different values the field has.<\/li>\n<\/ul>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"total_value\":  { \"sum\": { \"field\": \"price\" } },\r\n    \"priced_cars\":  { \"value_count\": { \"field\": \"price\" } },\r\n    \"different_makes\": { \"cardinality\": { \"field\": \"make\" } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"total_value\": {\r\n      \"value\": 268000.0\r\n    },\r\n    \"priced_cars\": {\r\n      \"value\": 10\r\n    },\r\n    \"different_makes\": {\r\n      \"value\": 4\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The cardinality aggregation gives an approximate count for large data. For a tiny set like ours it is exact. The result of 4 matches the four makes we saw earlier.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A whole set of numbers in one go is also available with stats, as in Example 3, but on the whole index:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"stats_price\": { \"stats\": { \"field\": \"price\" } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"stats_price\": {\r\n      \"count\": 10,\r\n      \"min\": 11000.0,\r\n      \"max\": 52000.0,\r\n      \"avg\": 26800.0,\r\n      \"sum\": 268000.0\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Group Numbers With Range and Histogram<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The terms aggregation works well for labels. For numbers, you often want bands. The range aggregation makes buckets that you define. Here three price bands are named budget, mid and premium. The from value is included, and the to value is not.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"price_bands\": {\r\n      \"range\": {\r\n        \"field\": \"price\",\r\n        \"ranges\": [\r\n          { \"key\": \"budget\",  \"to\": 20000 },\r\n          { \"key\": \"mid\",     \"from\": 20000, \"to\": 40000 },\r\n          { \"key\": \"premium\", \"from\": 40000 }\r\n        ]\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"price_bands\": {\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"budget\",\r\n          \"to\": 20000.0,\r\n          \"doc_count\": 4\r\n        },\r\n        {\r\n          \"key\": \"mid\",\r\n          \"from\": 20000.0,\r\n          \"to\": 40000.0,\r\n          \"doc_count\": 3\r\n        },\r\n        {\r\n          \"key\": \"premium\",\r\n          \"from\": 40000.0,\r\n          \"doc_count\": 3\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The histogram aggregation makes equal sized steps. Here the step is 10000. Empty steps in the middle are shown with a count of 0.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"price_steps\": { \"histogram\": { \"field\": \"price\", \"interval\": 10000 } }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"price_steps\": {\r\n      \"buckets\": [\r\n        {\r\n          \"key\": 10000.0,\r\n          \"doc_count\": 4\r\n        },\r\n        {\r\n          \"key\": 20000.0,\r\n          \"doc_count\": 3\r\n        },\r\n        {\r\n          \"key\": 30000.0,\r\n          \"doc_count\": 0\r\n        },\r\n        {\r\n          \"key\": 40000.0,\r\n          \"doc_count\": 2\r\n        },\r\n        {\r\n          \"key\": 50000.0,\r\n          \"doc_count\": 1\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">There is also the <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">date_histogram<\/code> aggregation, which groups documents by a time step such as a day or a month. It is the one behind most time charts in Kibana. Our sample has no date field, so I have not run it here. Please add a date field to your own data to try it.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Pipeline Aggregations: An Example<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A pipeline aggregation takes its input from other aggregations. Here <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">avg_bucket<\/code> finds the average of the average prices of all the makes. The path to the input uses the greater than sign to go from the bucket aggregation to the metric inside it.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": {\r\n      \"terms\": { \"field\": \"make\" },\r\n      \"aggs\": {\r\n        \"avg_price\": { \"avg\": { \"field\": \"price\" } }\r\n      }\r\n    },\r\n    \"avg_of_make_averages\": {\r\n      \"avg_bucket\": { \"buckets_path\": \"popular_cars&gt;avg_price\" }\r\n    }\r\n  }\r\n}<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{\r\n  \"aggregations\": {\r\n    \"popular_cars\": {\r\n      \"doc_count_error_upper_bound\": 0,\r\n      \"sum_other_doc_count\": 0,\r\n      \"buckets\": [\r\n        {\r\n          \"key\": \"Toyota\",\r\n          \"doc_count\": 4,\r\n          \"avg_price\": {\r\n            \"value\": 26000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Honda\",\r\n          \"doc_count\": 3,\r\n          \"avg_price\": {\r\n            \"value\": 19000.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"Ford\",\r\n          \"doc_count\": 2,\r\n          \"avg_price\": {\r\n            \"value\": 27500.0\r\n          }\r\n        },\r\n        {\r\n          \"key\": \"BMW\",\r\n          \"doc_count\": 1,\r\n          \"avg_price\": {\r\n            \"value\": 52000.0\r\n          }\r\n        }\r\n      ]\r\n    },\r\n    \"avg_of_make_averages\": {\r\n      \"value\": 31125.0\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The average of the four make averages is 31125. This is different from the overall average of 26800 in Example 2. The reason is that the overall average counts each car once, so Toyota, with 4 cars, has more weight. The pipeline average counts each make once. Neither is wrong, but they answer different questions, and you should pick the one that fits.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">A Common Error: Aggregating on a Text Field<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The most common beginner problem is running a terms aggregation on a text field. A text field is split into words for search, and is not meant for grouping. The Elastic documentation says that you cannot run a terms aggregation on a text field by default. There are two ways round it.<\/p>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; margin: 0 0 16px; padding-left: 24px; font-size: 17px; line-height: 1.7; color: #1f2937;\">\n<li style=\"margin: 0 0 8px;\">Best: use a keyword field, or a keyword sub field of the text field.<\/li>\n<li style=\"margin: 0 0 8px;\">Possible, but costly: turn on <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">fielddata<\/code> for the text field. This can use a lot of memory.<\/li>\n<\/ul>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">If you let Elasticsearch map a new text field on its own, it usually makes a text field with a keyword sub field named keyword. You can also do this yourself:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">PUT cars_fixed\r\n{\r\n  \"mappings\": {\r\n    \"properties\": {\r\n      \"make\": {\r\n        \"type\": \"text\",\r\n        \"fields\": { \"keyword\": { \"type\": \"keyword\" } }\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Then you aggregate on the sub field, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">make.keyword<\/code>:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">kibana dev tools<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">GET cars_fixed\/_search\r\n{\r\n  \"size\": 0,\r\n  \"aggs\": {\r\n    \"popular_cars\": { \"terms\": { \"field\": \"make.keyword\" } }\r\n  }\r\n}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">I could not run this error here, so I have not quoted the exact error text. Check it on your own cluster. It is an illegal argument error, and it tells you to use a keyword field.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Accuracy and Limits to Know<\/h2>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; margin: 0 0 16px; padding-left: 24px; font-size: 17px; line-height: 1.7; color: #1f2937;\">\n<li style=\"margin: 0 0 8px;\">Terms counts can be approximate. On large data, each shard returns only its top terms, so a term can be missed or its count can be too low. The field <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">doc_count_error_upper_bound<\/code> tells you the largest possible error. The Elastic documentation says to set <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">show_term_doc_count_error<\/code> to true if you want this value per bucket. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">shard_size<\/code> setting, which defaults to size times 1.5 plus 10, controls how many terms each shard returns.<\/li>\n<li style=\"margin: 0 0 8px;\">Large whole numbers. Elasticsearch uses double values inside aggregations. The documentation says that aggregations on long numbers above 2 to the power of 53 are approximate.<\/li>\n<li style=\"margin: 0 0 8px;\">Bucket limit. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">search.max_buckets<\/code> setting limits how many buckets one request may create.<\/li>\n<li style=\"margin: 0 0 8px;\">Runtime fields. If a field does not fit the aggregation you need, a runtime field can help, but it adds some overhead.<\/li>\n<li style=\"margin: 0 0 8px;\">meta and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">typed_keys<\/code>. The meta option adds your own note to an aggregation. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">typed_keys<\/code> option adds the aggregation type to the name in the response, such as <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">terms#popular_cars<\/code>.<\/li>\n<\/ul>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Aggregations Compared With SQL<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">If you know SQL, this table links the ideas. This is an idea map, and I am not claiming that one is faster than the other. That depends on your data, your hardware and your setup.<\/p>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 900px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">SQL ideas and the matching Elasticsearch aggregations<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 280px;\" scope=\"col\">SQL<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Elasticsearch<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Where to find it<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">GROUP BY make<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">terms on make<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Example 1<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">COUNT, AVG, MIN, MAX<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">value_count, avg, min, max<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Example 2<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">GROUP BY with AVG per group<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">terms with an avg sub aggregation<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Example 3<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">WHERE price &gt;= 20000<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">a range query next to the aggs<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Query scope section<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">CASE WHEN price bands<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">range aggregation<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Range section<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; vertical-align: top;\" scope=\"row\">COUNT DISTINCT make<\/th>\n<td style=\"padding: 12px 16px; vertical-align: top;\">cardinality<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">Metrics section<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The SQL for Example 3 would look like this:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">sql<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">SELECT make, COUNT(*) AS cars, AVG(price) AS avg_price\r\nFROM cars\r\nGROUP BY make\r\nORDER BY cars DESC;<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">One difference matters. A SQL query runs on tables that you design for joins and exact answers. Elasticsearch is built for search first, and for fast summaries on large amounts of data. It does not do joins in the same way, so check that it fits your task before you move work from a database.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Why Do Aggregations Matter for Big Data Analytics?<\/h2>\n<div style=\"margin: 16px 0 24px;\"><img decoding=\"async\" style=\"display: block; max-width: 100%; height: auto; border: 1px solid #d9e0ea; border-radius: 8px;\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggs_output.png\" alt=\"Screenshot of the output of the old page aggregation example\" \/><\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The old page said that one query can replace several SQL queries and a lot of application code, and that Elasticsearch is faster than an RDBMS. It gave no example or test. Here is what you can show. In a single request, Example 3 gave a count and five statistics for every make. Doing the same without aggregations would mean fetching documents and calculating in your own code.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">That is the real value. The summary is built where the data lives, so only a few lines travel over the network. This is why aggregations are used for dashboards, log analysis, product filters on shopping sites, and reports on search data.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">How fast it runs depends on the size of your data, the number of shards, the field types and how many buckets you ask for. If you plan to use it for analytics at work, test with your own data.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Common Aggregation Mistakes and How to Fix Them<\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 900px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Common aggregation mistakes, their usual cause and the fix<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 320px;\" scope=\"col\">Problem<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Usual cause<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">An error on a terms aggregation<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">The field is a text field<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Use a keyword field or a keyword sub field<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">The response is full of hits<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">size was not set to 0<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Add size 0<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Only 10 buckets come back<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">The default size of a terms aggregation is 10<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Set size, and check sum_other_doc_count<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">The metric numbers are for all documents, not per group<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">The metric is next to the bucket, not inside it<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Put the metric in the aggs of the bucket aggregation<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">Counts look slightly wrong on a large index<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Terms counts are approximate across shards<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Raise shard_size, and check doc_count_error_upper_bound<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; vertical-align: top;\" scope=\"row\">The average has a long decimal<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Metric values are doubles<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; vertical-align: top;\">Round the number in your app<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; vertical-align: top;\" scope=\"row\">The aggregation name is not found in the response<\/th>\n<td style=\"padding: 12px 16px; vertical-align: top;\">Names are your own, and are case sensitive<\/td>\n<td style=\"padding: 12px 16px; vertical-align: top;\">Use the exact name from your request<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Practice Exercises<\/h2>\n<ol style=\"font-family: Arial,Helvetica,sans-serif; margin: 0 0 16px; padding-left: 28px; font-size: 17px; line-height: 1.7; color: #1f2937;\">\n<li style=\"margin: 0 0 8px;\">Create the cars index and load the 10 documents. Run Example 1 and check that you get 4, 3, 2 and 1.<\/li>\n<li style=\"margin: 0 0 8px;\">Group the cars by color, and add the average price per color.<\/li>\n<li style=\"margin: 0 0 8px;\">Use a query to keep only cars from 2022 or later, and count them by make.<\/li>\n<li style=\"margin: 0 0 8px;\">Make a range aggregation with your own price bands, and give each band a clear name.<\/li>\n<li style=\"margin: 0 0 8px;\">Add a sub aggregation inside a range bucket, such as the average year for each band.<\/li>\n<\/ol>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Aggregations turn Elasticsearch from a search box into an analysis tool. Start with one terms aggregation on a keyword field, add one metric inside it, and add a query to narrow the data. Once that is comfortable, move on to range, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">date_histogram<\/code> and pipeline aggregations. A good next step is to build a Kibana chart from the same index, because Kibana runs these same aggregations behind every chart.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aggregations in Elasticsearch: Types, Syntax and Examples You have a million documents in an index, and a manager asks a simple question: how many cars did we list for each make, and what is the average price of each? You do not need to read a million documents to answer. Elasticsearch can group them and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57,1702],"tags":[],"class_list":["post-3029","post","type-post","status-publish","format-standard","hentry","category-elastic-search","category-elasticsearch-modules"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Elasticsearch Aggregations: Types, Syntax and Examples<\/title>\n<meta name=\"description\" content=\"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elasticsearch Aggregations: Types, Syntax and Examples\" \/>\n<meta property=\"og:description\" content=\"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-27T13:46:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-28T12:58:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/\",\"url\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/\",\"name\":\"Elasticsearch Aggregations: Types, Syntax and Examples\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png\",\"datePublished\":\"2026-10-27T13:46:49+00:00\",\"dateModified\":\"2026-10-28T12:58:41+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png\",\"width\":340,\"height\":198},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Aggregations in ElasticSearch:\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Elasticsearch Aggregations: Types, Syntax and Examples","description":"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/","og_locale":"en_US","og_type":"article","og_title":"Elasticsearch Aggregations: Types, Syntax and Examples","og_description":"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.","og_url":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-27T13:46:49+00:00","article_modified_time":"2026-10-28T12:58:41+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/","url":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/","name":"Elasticsearch Aggregations: Types, Syntax and Examples","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png","datePublished":"2026-10-27T13:46:49+00:00","dateModified":"2026-10-28T12:58:41+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn aggregations in Elasticsearch with copyable queries: bucket, metric, pipeline and nested aggs, size 0, keyword fields, range and common errors.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/11\/aggssss.png","width":340,"height":198},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/elastic-search\/elasticsearch-modules\/aggregations-in-elasticsearch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Aggregations in ElasticSearch:"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/3029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=3029"}],"version-history":[{"count":9,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/3029\/revisions"}],"predecessor-version":[{"id":12083,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/3029\/revisions\/12083"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=3029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=3029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=3029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}