<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5</id>
	<title>ElasticSearch聚合分析：聚合查询（aggs）基本概念 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;action=history"/>
	<updated>2026-04-29T08:08:45Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=6484&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 14:02 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=6484&amp;oldid=prev"/>
		<updated>2023-03-31T14:02:43Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 22:02的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;ElasticSearch&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;ElasticSearch教程&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=3665&amp;oldid=prev</id>
		<title>Eijux：​/* ES聚合查询语法 */</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=3665&amp;oldid=prev"/>
		<updated>2021-05-20T16:39:40Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;ES聚合查询语法&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2021年5月21日 (五) 00:39的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l84&quot;&gt;第84行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第84行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         &amp;quot;popular_colors&amp;quot; : { // 给聚合查询取个名字，叫 popular_colors&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         &amp;quot;popular_colors&amp;quot; : { // 给聚合查询取个名字，叫 popular_colors&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             &amp;quot;terms&amp;quot; : { // 聚合类型为，terms，【terms是桶聚合的一种】，类似 SQL 的 group by 的作用，根据字段分组，相同字段值的文档分为一组。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             &amp;quot;terms&amp;quot; : { // 聚合类型为，terms，【terms是桶聚合的一种】，类似 SQL 的 group by 的作用，根据字段分组，相同字段值的文档分为一组。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;              &lt;/del&gt;&amp;quot;field&amp;quot; : &amp;quot;color&amp;quot; // terms聚合类型的参数，这里需要设置分组的字段为 color，根据 color 分组&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;                &lt;/ins&gt;&amp;quot;field&amp;quot; : &amp;quot;color&amp;quot; // terms聚合类型的参数，这里需要设置分组的字段为 color，根据 color 分组&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;             }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         }&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;         }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=3664&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:ElasticSearch  == 关于 == ES 中的聚合查询，类似 SQL 的 '''SUM''' / '''AVG''' / '''COUNT''' / '''GROUP BY''' 分组查询，主要用于…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch%E8%81%9A%E5%90%88%E5%88%86%E6%9E%90%EF%BC%9A%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%88aggs%EF%BC%89%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5&amp;diff=3664&amp;oldid=prev"/>
		<updated>2021-05-20T16:33:59Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:ElasticSearch&quot; title=&quot;分类:ElasticSearch&quot;&gt;category:ElasticSearch&lt;/a&gt;  == 关于 == ES 中的聚合查询，类似 SQL 的 &amp;#039;&amp;#039;&amp;#039;SUM&amp;#039;&amp;#039;&amp;#039; / &amp;#039;&amp;#039;&amp;#039;AVG&amp;#039;&amp;#039;&amp;#039; / &amp;#039;&amp;#039;&amp;#039;COUNT&amp;#039;&amp;#039;&amp;#039; / &amp;#039;&amp;#039;&amp;#039;GROUP BY&amp;#039;&amp;#039;&amp;#039; 分组查询，主要用于…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:ElasticSearch]]&lt;br /&gt;
&lt;br /&gt;
== 关于 ==&lt;br /&gt;
ES 中的聚合查询，类似 SQL 的 '''SUM''' / '''AVG''' / '''COUNT''' / '''GROUP BY''' 分组查询，主要用于统计分析场景。&lt;br /&gt;
&lt;br /&gt;
== ES聚合查询流程 ==&lt;br /&gt;
ES聚合查询类似 SQL 的 GROUP by，一般统计分析主要分为两个步骤：&lt;br /&gt;
# '''分组'''：对查询的数据首先进行一轮分组，可以设置分组条件；&lt;br /&gt;
#: 例如：新生入学，把所有的学生按专业分班，这个分班的过程就是对学生进行了分组。&lt;br /&gt;
# '''组内聚合'''：对组内的数据进行'''统计'''；&lt;br /&gt;
#: 例如：计算总数、求平均值等等，接上面的例子，学生都按专业分班了，那么就可以统计每个班的学生总数，这个统计每个班学生总数的计算，就是组内聚合计算。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
分组类似 SQL 的 group by 语句设定的条件。&lt;br /&gt;
组内聚合，就是在 select 编写的 avg、sum、count 统计函数；&lt;br /&gt;
&lt;br /&gt;
熟悉SQL语句都知道 sum、count 这些统计函数不一定要跟 group by 语句配合使用，单独使用统计函数等同于将所有数据分成一个组，直接对所有数据进行统计。&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 核心概念 ==&lt;br /&gt;
=== 桶 ===&lt;br /&gt;
'''满足特定条件的文档的集合'''，叫做'''桶'''。&lt;br /&gt;
: 桶的就是一组数据的集合，对数据分组后，得到一组组的数据，就是一个个的桶。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 桶等同于组，'''分桶'''和'''分组'''是一个意思，ES 使用桶代表一组相同特征的数据。&lt;br /&gt;
&lt;br /&gt;
ES 中桶聚合，指的就是先对数据进行分组。&lt;br /&gt;
: ES支持多种分组条件，例如：支持类似 SQL 的 group by 根据字段分组，当然 ES 比 SQL 更强大，支持更多的分组条件，以满足各种统计需求。&lt;br /&gt;
&lt;br /&gt;
=== 指标 ===&lt;br /&gt;
指标：指的是'''对文档进行统计计算的方式'''，又叫'''指标聚合'''。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''桶内聚合'''：就是先对数据进行“分组”（分桶），然后对每一个桶内的数据进行“指标聚合”。&lt;br /&gt;
: 说白了就是，前面将数据经过一轮桶聚合，把数据分成一个个的桶之后，我们根据上面计算指标对桶内的数据进行统计。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
常用的指标有：'''SUM'''、'''COUNT'''、'''MAX''' 等统计函数。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
借助SQL的统计语句理解桶和指标：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SQL&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
SELECT COUNT(*) &lt;br /&gt;
FROM order&lt;br /&gt;
GROUP BY shop_id &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
说明：&lt;br /&gt;
* “COUNT(*)”：相当于指标, 也叫统计指标。&lt;br /&gt;
* “GROUP BY shop_id”：相当于分桶的条件，也可以叫分组条件，相同shop_id的数据都分到一个桶内。&lt;br /&gt;
这条 SQL 语句的作用就是统计每一个店铺的订单数，所以 SQL 统计的第一步是根据 group by shop_id 这个条件，把 shop_id（店铺ID）相同的数据分到一个组（桶）里面，然后每一组数据使用 count(*) 统计函数（指标）计算总数，最终得到每一个店铺的订单总数，ES 也是类似的过程。&lt;br /&gt;
&lt;br /&gt;
== ES聚合查询语法 ==&lt;br /&gt;
语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
   &amp;quot;aggregations&amp;quot; : {&lt;br /&gt;
      &amp;quot;&amp;lt;aggregation_name&amp;gt;&amp;quot; : {&lt;br /&gt;
         &amp;quot;&amp;lt;aggregation_type&amp;gt;&amp;quot; : {&lt;br /&gt;
            &amp;lt;aggregation_body&amp;gt;&lt;br /&gt;
         }&lt;br /&gt;
         [,&amp;quot;aggregations&amp;quot; : { [&amp;lt;sub_aggregation&amp;gt;]+ } ]? // 嵌套聚合查询，支持多层嵌套&lt;br /&gt;
      }&lt;br /&gt;
      [,&amp;quot;&amp;lt;aggregation_name_2&amp;gt;&amp;quot; : { ... } ]* // 多个聚合查询，每个聚合查询取不同的名字&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
说明：&lt;br /&gt;
* '''aggregations'''：代表聚合查询语句，可以简写为 '''aggs'''；&lt;br /&gt;
* '''&amp;lt;aggregation_name&amp;gt;'''：代表一个聚合计算的名字，可以随意命名，因为 ES 支持一次进行多次统计分析查询，后面需要通过这个名字在查询结果中找到我们想要的计算结果；&lt;br /&gt;
* '''&amp;lt;aggregation_type&amp;gt;'''：聚合类型，代表我们想要怎么统计数据，主要有两大类聚合类型，'''桶聚合'''和'''指标聚合'''。&lt;br /&gt;
** 这两类聚合又包括多种聚合类型，例如：指标聚合：sum、avg， 桶聚合：terms、Date histogram 等等；&lt;br /&gt;
* '''&amp;lt;aggregation_body&amp;gt;'''：聚合类型的参数，选择不同的聚合类型，有不同的参数；&lt;br /&gt;
* '''aggregation_name_2'''：代表其他聚合计算的名字，意思就是可以一次进行多种类型的统计。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：假设存在一个 order 索引，存储了每一笔汽车销售订单，里面包含了汽车颜色字段 color&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
GET /order/_search&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;size&amp;quot; : 0, // 【设置 size=0 的意思就是，仅返回聚合查询结果，不返回普通 query 查询结果】。&lt;br /&gt;
    &amp;quot;aggs&amp;quot; : { // 聚合查询语句的简写&lt;br /&gt;
        &amp;quot;popular_colors&amp;quot; : { // 给聚合查询取个名字，叫 popular_colors&lt;br /&gt;
            &amp;quot;terms&amp;quot; : { // 聚合类型为，terms，【terms是桶聚合的一种】，类似 SQL 的 group by 的作用，根据字段分组，相同字段值的文档分为一组。&lt;br /&gt;
              &amp;quot;field&amp;quot; : &amp;quot;color&amp;quot; // terms聚合类型的参数，这里需要设置分组的字段为 color，根据 color 分组&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
等价SQL：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SQL&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
select count(color) from order group by color&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
输出：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
...&lt;br /&gt;
   &amp;quot;hits&amp;quot;: { // 因为size=0,所以query查询结果为空&lt;br /&gt;
      &amp;quot;hits&amp;quot;: [] &lt;br /&gt;
   },&lt;br /&gt;
   &amp;quot;aggregations&amp;quot;: { // 聚合查询结果&lt;br /&gt;
      &amp;quot;popular_colors&amp;quot;: { // 这个就是 popular_colors 聚合查询的结果，这就是为什么需要给聚合查询取个名字的原因，如果有多个聚合查询，可以通过名字查找结果&lt;br /&gt;
         &amp;quot;buckets&amp;quot;: [ // 【因为是桶聚合，所以看到返回一个 buckets 数组】，代表分组的统计情况，下面可以看到每一种颜色的销量情况&lt;br /&gt;
            {&lt;br /&gt;
               &amp;quot;key&amp;quot;: &amp;quot;red&amp;quot;, &lt;br /&gt;
               &amp;quot;doc_count&amp;quot;: 4 // 红色的汽车销量为4&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
               &amp;quot;key&amp;quot;: &amp;quot;blue&amp;quot;,&lt;br /&gt;
               &amp;quot;doc_count&amp;quot;: 2&lt;br /&gt;
            },&lt;br /&gt;
            {&lt;br /&gt;
               &amp;quot;key&amp;quot;: &amp;quot;green&amp;quot;,&lt;br /&gt;
               &amp;quot;doc_count&amp;quot;: 2&lt;br /&gt;
            }&lt;br /&gt;
         ]&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>