推荐答案
在 Elasticsearch 中,Bucket 聚合是一种将文档分组到不同“桶”中的聚合方式。常见的 Bucket 聚合包括:
- Terms Aggregation:根据字段的唯一值进行分组。
- Range Aggregation:根据数值范围进行分组。
- Date Range Aggregation:根据日期范围进行分组。
- Histogram Aggregation:根据数值间隔进行分组。
- Date Histogram Aggregation:根据日期间隔进行分组。
- Geo Distance Aggregation:根据地理位置距离进行分组。
- IP Range Aggregation:根据 IP 地址范围进行分组。
- Nested Aggregation:用于嵌套文档的聚合。
- Reverse Nested Aggregation:用于从嵌套文档返回到父文档的聚合。
- Children Aggregation:用于父子文档关系的聚合。
- Significant Terms Aggregation:用于识别显著项的聚合。
- Filters Aggregation:根据多个过滤器进行分组。
- Sampler Aggregation:用于对文档进行采样后再聚合。
本题详细解读
Terms Aggregation
Terms Aggregation 是最常用的 Bucket 聚合之一,它根据字段的唯一值将文档分组。例如,可以根据“category”字段的值将商品文档分组。
// www.javascriptcn.com code example
{
"aggs": {
"categories": {
"terms": {
"field": "category.keyword"
}
}
}
}Range Aggregation
Range Aggregation 允许你根据数值范围将文档分组。例如,可以根据“price”字段的值将商品文档分组到不同的价格区间。
// www.javascriptcn.com code example
{
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 50 },
{ "from": 50, "to": 100 },
{ "from": 100 }
]
}
}
}
}Date Range Aggregation
Date Range Aggregation 类似于 Range Aggregation,但它是根据日期范围进行分组。例如,可以根据“date”字段的值将日志文档分组到不同的日期区间。
// www.javascriptcn.com code example
{
"aggs": {
"date_ranges": {
"date_range": {
"field": "date",
"ranges": [
{ "to": "now-10d/d" },
{ "from": "now-10d/d", "to": "now" },
{ "from": "now" }
]
}
}
}
}Histogram Aggregation
Histogram Aggregation 根据数值间隔将文档分组。例如,可以根据“age”字段的值将用户文档分组到不同的年龄区间。
// www.javascriptcn.com code example
{
"aggs": {
"age_histogram": {
"histogram": {
"field": "age",
"interval": 10
}
}
}
}Date Histogram Aggregation
Date Histogram Aggregation 根据日期间隔将文档分组。例如,可以根据“timestamp”字段的值将日志文档分组到不同的时间区间。
// www.javascriptcn.com code example
{
"aggs": {
"logs_over_time": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "day"
}
}
}
}Geo Distance Aggregation
Geo Distance Aggregation 根据地理位置距离将文档分组。例如,可以根据用户的位置与某个中心点的距离将用户文档分组。
// www.javascriptcn.com code example
{
"aggs": {
"distance_ranges": {
"geo_distance": {
"field": "location",
"origin": "52.3760, 4.894",
"ranges": [
{ "to": 100 },
{ "from": 100, "to": 200 },
{ "from": 200 }
]
}
}
}
}IP Range Aggregation
IP Range Aggregation 根据 IP 地址范围将文档分组。例如,可以根据“ip”字段的值将访问日志分组到不同的 IP 地址区间。
// www.javascriptcn.com code example
{
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "ip",
"ranges": [
{ "to": "10.0.0.5" },
{ "from": "10.0.0.5", "to": "10.0.0.10" },
{ "from": "10.0.0.10" }
]
}
}
}
}Nested Aggregation
Nested Aggregation 用于嵌套文档的聚合。例如,可以在嵌套的“comments”字段上进行聚合。
// www.javascriptcn.com code example
{
"aggs": {
"nested_comments": {
"nested": {
"path": "comments"
},
"aggs": {
"comment_authors": {
"terms": {
"field": "comments.author.keyword"
}
}
}
}
}
}Reverse Nested Aggregation
Reverse Nested Aggregation 用于从嵌套文档返回到父文档的聚合。例如,可以在嵌套的“comments”字段上进行聚合后,再返回到父文档进行进一步聚合。
// www.javascriptcn.com code example
{
"aggs": {
"nested_comments": {
"nested": {
"path": "comments"
},
"aggs": {
"comment_authors": {
"terms": {
"field": "comments.author.keyword"
},
"aggs": {
"reverse_nested": {
"reverse_nested": {}
}
}
}
}
}
}
}Children Aggregation
Children Aggregation 用于父子文档关系的聚合。例如,可以在“parent”和“child”文档之间进行聚合。
// www.javascriptcn.com code example
{
"aggs": {
"children": {
"children": {
"type": "child"
},
"aggs": {
"child_terms": {
"terms": {
"field": "child_field.keyword"
}
}
}
}
}
}Significant Terms Aggregation
Significant Terms Aggregation 用于识别显著项的聚合。例如,可以识别出某个字段中显著出现的值。
// www.javascriptcn.com code example
{
"aggs": {
"significant_categories": {
"significant_terms": {
"field": "category.keyword"
}
}
}
}Filters Aggregation
Filters Aggregation 根据多个过滤器将文档分组。例如,可以根据不同的过滤器将文档分组到不同的桶中。
// www.javascriptcn.com code example
{
"aggs": {
"filters": {
"filters": {
"filters": {
"filter1": { "term": { "field1": "value1" } },
"filter2": { "term": { "field2": "value2" } }
}
}
}
}
}Sampler Aggregation
Sampler Aggregation 用于对文档进行采样后再聚合。例如,可以先对文档进行采样,然后再对采样后的文档进行聚合。
// www.javascriptcn.com code example
{
"aggs": {
"sample": {
"sampler": {
"shard_size": 100
},
"aggs": {
"terms": {
"terms": {
"field": "category.keyword"
}
}
}
}
}
}