1、聚合查询
聚合(aggs)不同于普通查询,是目前学到的第二种大的查询分类,第一种即“query”,因此在代码中的第一层嵌套由“query”变为了“aggs”。用于进行聚合的字段必须是exact value,分词字段不可进行聚合,对于text字段如果需要使用聚合,需要开启fielddata,但是通常不建议,因为fielddata是将聚合使用的数据结构由磁盘(doc_values)变为了堆内存(field_data),大数据的聚合操作很容易导致OOM,详细原理会在进阶篇中阐述。
分桶聚合(Bucket agregations):类比SQL中的group by的作用,主要用于统计不同类型数据的数量
指标聚合(Metrics agregations):主要用于最大值、最小值、平均值、字段之和等指标的统计
管道聚合(Pipeline agregations):用于对聚合的结果进行二次聚合,如要统计绑定数量最多的标签bucket,就是要先按照标签进行分桶,再在分桶的结果上计算最大值。
GET product/_search
{
"aggs": {
"<aggs_name>": {
"<agg_type>": {
"field": "<field_name>"
}
}
}
}
aggs_name:聚合函数的名称
agg_type:聚合种类,比如是桶聚合(terms)或者是指标聚合(avg、sum、min、max等)
field_name:字段名称或者叫域名。
GET product/_search
{
"size": 0,
"aggs": {
"tag_bucket": {
"terms": {
"field": "tags.keyword"
}
}
}
}
--结果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"tag_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 16,
"buckets" : [
{
"key" : "性价比",
"doc_count" : 3
},
{
"key" : "发烧",
"doc_count" : 2
},
{
"key" : "大片",
"doc_count" : 2
},
{
"key" : "蓝光8K",
"doc_count" : 2
},
{
"key" : "超薄",
"doc_count" : 2
},
{
"key" : "120HZ刷新率",
"doc_count" : 1
},
{
"key" : "120W快充",
"doc_count" : 1
},
{
"key" : "120倍变焦",
"doc_count" : 1
},
{
"key" : "4G全网通",
"doc_count" : 1
},
{
"key" : "5V1A",
"doc_count" : 1
}
]
}
}
}
排序取前5
GET product/_search
{
"size": 0,
"aggs": {
"tag_bucket": {
"terms": {
"field": "tags.keyword",
"size": 5,
"order": {
"_count": "desc"
}
}
}
}
}
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"tag_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 21,
"buckets" : [
{
"key" : "性价比",
"doc_count" : 3
},
{
"key" : "发烧",
"doc_count" : 2
},
{
"key" : "大片",
"doc_count" : 2
},
{
"key" : "蓝光8K",
"doc_count" : 2
},
{
"key" : "超薄",
"doc_count" : 2
}
]
}
}
}
-
指标聚合
场景:用于统计某个指标,如最大值、最小值、平均值,可以结合桶聚合一起使用,如按照商品类型分桶,统计每个桶的平均价格。
函数:平均值:Avg、最大值:Max、最小值:Min、求和:Sum、详细信息:Stats、数量:Value count
详细信息:Stats
GET product/_search
{
"size": 0,
"aggs": {
"price_stats": {
"stats": {
"field": "price"
}
}
}
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_stats" : {
"count" : 10,
"min" : 399.0,
"max" : 4999.0,
"avg" : 3008.8,
"sum" : 30088.0
}
}
}
最贵、最便宜和平均价格三个指标
GET product/_search
{
"size": 0,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
},
"min_price": {
"min": {
"field": "price"
}
},
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"max_price" : {
"value" : 4999.0
},
"min_price" : {
"value" : 399.0
},
"avg_price" : {
"value" : 3008.8
}
}
}
doc_values和field_data
POST 执行之后才可以执行第一个get不报错
GET product/_search
{
"size": 0,
"aggs": {
"tag_bucket": {
"terms": {
"field": "name"
}
}
}
}
GET product/_search
{
"size": 0,
"aggs": {
"tag_bucket": {
"terms": {
"field": "name.keyword"
}
}
}
}
POST product/_mapping
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"fielddata": true
}
}
}
GET product/_search
{
"size": 0,
"aggs": {
"tag_bucket": {
"terms": {
"size": 20,
"field": "name"
}
}
}
}
按照name去重的数量
GET product/_search
{
"size": 0,
"aggs": {
"type_count": {
"cardinality": {
"field": "name"
}
}
}
}
GET product/_search
{
"size": 0,
"aggs": {
"type_count": {
"cardinality": {
"field": "name.keyword"
}
}
}
}
对type计算去重后数量
GET product/_search
{
"size": 0,
"aggs": {
"type_count": {
"cardinality": {
"field": "lv.keyword"
}
}
}
}
-
管道聚合
场景:用于对聚合查询的二次聚合,如统计平均价格最低的商品分类,即先按照商品分类进行桶聚合,并计算其平均价格,然后对其平均价格计算最小值聚合
函数:Min bucket:最小桶、Max bucket:最大桶、Avg bucket:桶平均值、Sum bucket:桶求和、Stats bucket:桶信息
注意:buckets_path为管道聚合的关键字,其值从当前聚合统计的聚合函数开始计算为第一级。比如下面例子中,type_bucket和min_bucket同级,
统计平均价格最低的商品分类 耳机的平均价格最低
GET product/_search
{
"size": 0,
"aggs": {
"type_bucket": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"price_bucket": {
"avg": {
"field": "price"
}
}
}
},
"min_bucket":{
"min_bucket": {
"buckets_path": "type_bucket>price_bucket"
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"type_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "手机",
"doc_count" : 6,
"price_bucket" : {
"value" : 3939.0
}
},
{
"key" : "耳机",
"doc_count" : 3,
"price_bucket" : {
"value" : 1465.3333333333333
}
},
{
"key" : "电视",
"doc_count" : 2,
"price_bucket" : {
"value" : 2998.5
}
}
]
},
"min_bucket" : {
"value" : 1465.3333333333333,
"keys" : [
"耳机"
]
}
}
}
-
嵌套聚合
语法:
GET product/_search { "size": 0, "aggs": { "<agg_name>": { "<agg_type>": { "field": "<field_name>" }, "aggs": { "<agg_name_child>": { "<agg_type>": { "field": "<field_name>" } } } } } }用途:用于在某种聚合的计算结果之上再次聚合,如统计不同类型商品的平均价格,就是在按照商品类型桶聚合之后,在其结果之上计算平均价格
统计不同类型商品的不同级别的数量
GET product/_search
{
"size": 0,
"aggs": {
"type_lv": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"lv": {
"terms": {
"field": "lv.keyword"
}
}
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"type_lv" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "手机",
"doc_count" : 6,
"lv" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "旗舰机",
"doc_count" : 5
},
{
"key" : "高端机",
"doc_count" : 1
}
]
}
},
{
"key" : "耳机",
"doc_count" : 3,
"lv" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "百元机",
"doc_count" : 2
},
{
"key" : "高端机",
"doc_count" : 1
}
]
}
},
{
"key" : "电视",
"doc_count" : 2,
"lv" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高端机",
"doc_count" : 2
}
]
}
}
]
}
}
}
按照lv分桶 输出每个桶的具体价格信息
GET product/_search
{
"size": 0,
"aggs": {
"lv_price": {
"terms": {
"field": "lv.keyword"
},
"aggs": {
"price": {
"stats": {
"field": "price"
}
}
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"lv_price" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "旗舰机",
"doc_count" : 5,
"price" : {
"count" : 4,
"min" : 3299.0,
"max" : 4999.0,
"avg" : 4174.0,
"sum" : 16696.0
}
},
{
"key" : "高端机",
"doc_count" : 4,
"price" : {
"count" : 4,
"min" : 2998.0,
"max" : 2999.0,
"avg" : 2998.5,
"sum" : 11994.0
}
},
{
"key" : "百元机",
"doc_count" : 2,
"price" : {
"count" : 2,
"min" : 399.0,
"max" : 999.0,
"avg" : 699.0,
"sum" : 1398.0
}
}
]
}
}
}
统计不同类型商品 不同档次的 价格信息 标签信息
先按照type分组 然后对每个type组里进行lv分组 lv分组里面进行按tag标签分组 同时取lv分组的价格的全部信息
GET product/_search
{
"size": 0,
"aggs": {
"type_agg": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword"
},
"aggs": {
"price_stats": {
"stats": {
"field": "price"
}
},
"tags_buckets": {
"terms": {
"field": "tags.keyword"
}
}
}
}
}
}
}
}
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"type_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "手机",
"doc_count" : 6,
"lv_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "旗舰机",
"doc_count" : 5,
"tags_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 2,
"buckets" : [
{
"key" : "发烧",
"doc_count" : 2
},
{
"key" : "性价比",
"doc_count" : 2
},
{
"key" : "120HZ刷新率",
"doc_count" : 1
},
{
"key" : "120W快充",
"doc_count" : 1
},
{
"key" : "120倍变焦",
"doc_count" : 1
},
{
"key" : "4G全网通",
"doc_count" : 1
},
{
"key" : "5V1A",
"doc_count" : 1
},
{
"key" : "不卡顿",
"doc_count" : 1
},
{
"key" : "公交卡",
"doc_count" : 1
},
{
"key" : "割新韭菜",
"doc_count" : 1
}
]
},
"price_stats" : {
"count" : 4,
"min" : 3299.0,
"max" : 4999.0,
"avg" : 4174.0,
"sum" : 16696.0
}
},
{
"key" : "高端机",
"doc_count" : 1,
"tags_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "快充",
"doc_count" : 1
},
{
"key" : "性价比",
"doc_count" : 1
},
{
"key" : "门禁卡",
"doc_count" : 1
}
]
},
"price_stats" : {
"count" : 1,
"min" : 2999.0,
"max" : 2999.0,
"avg" : 2999.0,
"sum" : 2999.0
}
}
]
}
},
{
"key" : "耳机",
"doc_count" : 3,
"lv_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "百元机",
"doc_count" : 2,
"tags_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "低音炮",
"doc_count" : 1
},
{
"key" : "听声辨位",
"doc_count" : 1
},
{
"key" : "蓝牙",
"doc_count" : 1
},
{
"key" : "防水",
"doc_count" : 1
},
{
"key" : "防火",
"doc_count" : 1
},
{
"key" : "降噪",
"doc_count" : 1
}
]
},
"price_stats" : {
"count" : 2,
"min" : 399.0,
"max" : 999.0,
"avg" : 699.0,
"sum" : 1398.0
}
},
{
"key" : "高端机",
"doc_count" : 1,
"tags_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "家庭影院",
"doc_count" : 1
},
{
"key" : "巨馍",
"doc_count" : 1
},
{
"key" : "游戏",
"doc_count" : 1
}
]
},
"price_stats" : {
"count" : 1,
"min" : 2998.0,
"max" : 2998.0,
"avg" : 2998.0,
"sum" : 2998.0
}
}
]
}
},
{
"key" : "电视",
"doc_count" : 2,
"lv_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高端机",
"doc_count" : 2,
"tags_buckets" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "大片",
"doc_count" : 2
},
{
"key" : "蓝光8K",
"doc_count" : 2
},
{
"key" : "超薄",
"doc_count" : 2
}
]
},
"price_stats" : {
"count" : 2,
"min" : 2998.0,
"max" : 2999.0,
"avg" : 2998.5,
"sum" : 5997.0
}
}
]
}
}
]
}
}
}
统计每个商品类型中 不同档次分类商品中 平均价格最低的档次
GET product/_search
{
"size": 0,
"aggs": {
"type_bucket": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"lv_bucket": {
"terms": {
"field": "lv.keyword"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
},
"min_bucket": {
"min_bucket": {
"buckets_path": "lv_bucket>price_avg"
}
}
}
}
}
}
每层嵌套在结果的对应关系

基于查询结果的聚合 先查询大于等于1000的 再按照tag分组展示每个桶的数量
GET product/_search
{
"size": 0,
"query": {
"range": {
"price": {
"gte": 1000
}
}
},
"aggs": {
"tags_bucket": {
"terms": {
"field": "tags.keyword"
}
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"tags_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 7,
"buckets" : [
{
"key" : "性价比",
"doc_count" : 3
},
{
"key" : "发烧",
"doc_count" : 2
},
{
"key" : "大片",
"doc_count" : 2
},
{
"key" : "蓝光8K",
"doc_count" : 2
},
{
"key" : "超薄",
"doc_count" : 2
},
{
"key" : "4G全网通",
"doc_count" : 1
},
{
"key" : "5V1A",
"doc_count" : 1
},
{
"key" : "不卡顿",
"doc_count" : 1
},
{
"key" : "公交卡",
"doc_count" : 1
},
{
"key" : "割新韭菜",
"doc_count" : 1
}
]
}
}
}
-
基于聚合结果的查询、
GET product/_search { "aggs": { ... }, "post_filter": { ... } }注意:以上语法,执行顺序为先aggs后post_filter,顺序和谁在上谁在下没有关系。
基于聚合的查询
GET product/_search?size=20
{
"aggs": {
"tags_bucket": {
"terms": {
"field": "tags.keyword"
}
}
},
"post_filter": {
"term": {
"tags.keyword": "性价比"
}
}
}
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "小米手机",
"desc" : "手机中的战斗机",
"price" : 3999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-10-01T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"不卡顿"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "小米NFC手机",
"desc" : "支持全功能NFC,手机中的滑翔机",
"price" : 4999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-05-21T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"公交卡"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "NFC手机",
"desc" : "手机中的轰炸机",
"price" : 2999,
"lv" : "高端机",
"type" : "手机",
"createtime" : "2020-06-20",
"tags" : [
"性价比",
"快充",
"门禁卡"
]
}
}
]
},
"aggregations" : {
"tags_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 16,
"buckets" : [
{
"key" : "性价比",
"doc_count" : 3
},
{
"key" : "发烧",
"doc_count" : 2
},
{
"key" : "大片",
"doc_count" : 2
},
{
"key" : "蓝光8K",
"doc_count" : 2
},
{
"key" : "超薄",
"doc_count" : 2
},
{
"key" : "120HZ刷新率",
"doc_count" : 1
},
{
"key" : "120W快充",
"doc_count" : 1
},
{
"key" : "120倍变焦",
"doc_count" : 1
},
{
"key" : "4G全网通",
"doc_count" : 1
},
{
"key" : "5V1A",
"doc_count" : 1
}
]
}
}
}
-
查询条件的作用域
GET product/_search { "size": 10, "query": { ... }, "aggs": { "avg_price": { ... }, "all_avg_price": { "global": {}, "aggs": { ... } } } }上面例子中,avg_price的计算结果是基于query的查询结果的,而all_avg_price的聚合是基于all data的
GET product/_search
{
"size": 10,
"query": {
"range": {
"price": {
"gte": 1000
}
}
},
"aggs": {
"max_price": {
"max": {
"field": "price"
}
},
"min_price": {
"min": {
"field": "price"
}
},
"avg_price": {
"avg": {
"field": "price"
}
},
"all_avg_price": {
"global": {},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"muti_avg_price": {
"filter": {
"range": {
"price": {
"lte": 4500
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "小米手机",
"desc" : "手机中的战斗机",
"price" : 3999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-10-01T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"不卡顿"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "小米NFC手机",
"desc" : "支持全功能NFC,手机中的滑翔机",
"price" : 4999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-05-21T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"公交卡"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "NFC手机",
"desc" : "手机中的轰炸机",
"price" : 2999,
"lv" : "高端机",
"type" : "手机",
"createtime" : "2020-06-20",
"tags" : [
"性价比",
"快充",
"门禁卡"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"name" : "挨炮 SE2",
"desc" : "除了CPU,一无是处",
"price" : "3299",
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-07-21",
"tags" : [
"割韭菜",
"割韭菜",
"割新韭菜"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"name" : "XS Max",
"desc" : "听说要出新款12手机了,终于可以换掉手中的4S了",
"price" : 4399,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-08-19",
"tags" : [
"5V1A",
"4G全网通",
"大"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"name" : "小米电视",
"desc" : "70寸性价比只选,不要一万八,要不要八千八,只要两千九百九十八",
"price" : 2998,
"lv" : "高端机",
"type" : "耳机",
"createtime" : "2020-08-16",
"tags" : [
"巨馍",
"家庭影院",
"游戏"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.0,
"_source" : {
"name" : "红米电视",
"desc" : "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
"price" : 2999,
"type" : "电视",
"lv" : "高端机",
"createtime" : "2020-08-28",
"tags" : [
"大片",
"蓝光8K",
"超薄"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "11",
"_score" : 1.0,
"_source" : {
"name" : "红米电视",
"desc" : "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
"price" : 2998,
"type" : "电视",
"lv" : "高端机",
"createtime" : "2020-08-28",
"tags" : [
"大片",
"蓝光8K",
"超薄"
]
}
}
]
},
"aggregations" : {
"max_price" : {
"value" : 4999.0
},
"all_avg_price" : {
"doc_count" : 11,
"avg_price" : {
"value" : 3008.8
}
},
"min_price" : {
"value" : 2998.0
},
"avg_price" : {
"value" : 3586.25
},
"muti_avg_price" : {
"doc_count" : 7,
"avg_price" : {
"value" : 3384.4285714285716
}
}
}
}
聚合排序_count _key _term
GET product/_search
{
"size": 0,
"aggs": {
"type_agg": {
"terms": {
"field": "tags.keyword",
"order": {
"_count": "desc"
},
"size": 2
}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"type_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 27,
"buckets" : [
{
"key" : "性价比",
"doc_count" : 3
},
{
"key" : "发烧",
"doc_count" : 2
}
]
}
}
}
-
多级排序:即排序的优先级,按照外层优先的顺序
GET product/_search?size=0 { "aggs": { "first_sort": { ... "aggs": { "second_sort": { ... } } } } }
多级排序 先按照type分类 每个type桶里面再按照lv桶数量升序
多个type桶按照桶内数量降序排 type桶内再按照lv数量升序排
GET product/_search?size=0
{
"aggs": {
"first_sort": {
"terms": {
"field": "type.keyword",
"order": {
"_count": "desc"
}
},
"aggs": {
"second_sort": {
"terms": {
"field": "lv.keyword",
"order": {
"_count": "asc"
}
}
}
}
}
}
}
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"first_sort" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "手机",
"doc_count" : 6,
"second_sort" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高端机",
"doc_count" : 1
},
{
"key" : "旗舰机",
"doc_count" : 5
}
]
}
},
{
"key" : "耳机",
"doc_count" : 3,
"second_sort" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高端机",
"doc_count" : 1
},
{
"key" : "百元机",
"doc_count" : 2
}
]
}
},
{
"key" : "电视",
"doc_count" : 2,
"second_sort" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "高端机",
"doc_count" : 2
}
]
}
}
]
}
}
}
上例中,先按照first_sort排序,再按照second_sort排序
-
多层排序:即按照多层聚合中的里层某个聚合的结果进行排序
GET product/_search { "size": 0, "aggs": { "tag_avg_price": { "terms": { "field": "type.keyword", "order": { "agg_stats>my_stats.sum": "desc" } }, "aggs": { "agg_stats": { ... "aggs": { "my_stats": { "extended_stats": { ... } } } } } } } }上例中,按照里层聚合“my_stats”进行排序
GET product/_search
{
"size": 20,
"aggs": {
"tag_avg_price": {
"terms": {
"field": "type.keyword",
"order": {
"agg_stats>stats.sum": "asc"
}
},
"aggs": {
"agg_stats": {
"filter": {
"terms": {
"type.keyword": [
"耳机","手机","电视"
]
}
},
"aggs": {
"stats": {
"extended_stats": {
"field": "price"
}
}
}
}
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "小米手机",
"desc" : "手机中的战斗机",
"price" : 3999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-10-01T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"不卡顿"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "小米NFC手机",
"desc" : "支持全功能NFC,手机中的滑翔机",
"price" : 4999,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-05-21T08:00:00Z",
"tags" : [
"性价比",
"发烧",
"公交卡"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "NFC手机",
"desc" : "手机中的轰炸机",
"price" : 2999,
"lv" : "高端机",
"type" : "手机",
"createtime" : "2020-06-20",
"tags" : [
"性价比",
"快充",
"门禁卡"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "小米耳机",
"desc" : "耳机中的黄焖鸡",
"price" : 999,
"lv" : "百元机",
"type" : "耳机",
"createtime" : "2020-06-23",
"tags" : [
"降噪",
"防水",
"蓝牙"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "红米耳机",
"desc" : "耳机中的肯德基",
"price" : 399,
"type" : "耳机",
"lv" : "百元机",
"createtime" : "2020-07-20",
"tags" : [
"防火",
"低音炮",
"听声辨位"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"name" : "小米手机10",
"desc" : "充电贼快掉电更快,超级无敌望远镜,高刷电竞屏",
"price" : "",
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-07-27",
"tags" : [
"120HZ刷新率",
"120W快充",
"120倍变焦"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"name" : "挨炮 SE2",
"desc" : "除了CPU,一无是处",
"price" : "3299",
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-07-21",
"tags" : [
"割韭菜",
"割韭菜",
"割新韭菜"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"name" : "XS Max",
"desc" : "听说要出新款12手机了,终于可以换掉手中的4S了",
"price" : 4399,
"lv" : "旗舰机",
"type" : "手机",
"createtime" : "2020-08-19",
"tags" : [
"5V1A",
"4G全网通",
"大"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"name" : "小米电视",
"desc" : "70寸性价比只选,不要一万八,要不要八千八,只要两千九百九十八",
"price" : 2998,
"lv" : "高端机",
"type" : "耳机",
"createtime" : "2020-08-16",
"tags" : [
"巨馍",
"家庭影院",
"游戏"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.0,
"_source" : {
"name" : "红米电视",
"desc" : "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
"price" : 2999,
"type" : "电视",
"lv" : "高端机",
"createtime" : "2020-08-28",
"tags" : [
"大片",
"蓝光8K",
"超薄"
]
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "11",
"_score" : 1.0,
"_source" : {
"name" : "红米电视",
"desc" : "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
"price" : 2998,
"type" : "电视",
"lv" : "高端机",
"createtime" : "2020-08-28",
"tags" : [
"大片",
"蓝光8K",
"超薄"
]
}
}
]
},
"aggregations" : {
"tag_avg_price" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "耳机",
"doc_count" : 3,
"agg_stats" : {
"doc_count" : 3,
"stats" : {
"count" : 3,
"min" : 399.0,
"max" : 2998.0,
"avg" : 1465.3333333333333,
"sum" : 4396.0,
"sum_of_squares" : 1.0145206E7,
"variance" : 1234533.5555555557,
"variance_population" : 1234533.5555555557,
"variance_sampling" : 1851800.3333333335,
"std_deviation" : 1111.0956554480608,
"std_deviation_population" : 1111.0956554480608,
"std_deviation_sampling" : 1360.8087056354884,
"std_deviation_bounds" : {
"upper" : 3687.5246442294547,
"lower" : -756.8579775627884,
"upper_population" : 3687.5246442294547,
"lower_population" : -756.8579775627884,
"upper_sampling" : 4186.95074460431,
"lower_sampling" : -1256.2840779376436
}
}
}
},
{
"key" : "电视",
"doc_count" : 2,
"agg_stats" : {
"doc_count" : 2,
"stats" : {
"count" : 2,
"min" : 2998.0,
"max" : 2999.0,
"avg" : 2998.5,
"sum" : 5997.0,
"sum_of_squares" : 1.7982005E7,
"variance" : 0.25,
"variance_population" : 0.25,
"variance_sampling" : 0.5,
"std_deviation" : 0.5,
"std_deviation_population" : 0.5,
"std_deviation_sampling" : 0.7071067811865476,
"std_deviation_bounds" : {
"upper" : 2999.5,
"lower" : 2997.5,
"upper_population" : 2999.5,
"lower_population" : 2997.5,
"upper_sampling" : 2999.914213562373,
"lower_sampling" : 2997.085786437627
}
}
}
},
{
"key" : "手机",
"doc_count" : 6,
"agg_stats" : {
"doc_count" : 6,
"stats" : {
"count" : 5,
"min" : 2999.0,
"max" : 4999.0,
"avg" : 3939.0,
"sum" : 19695.0,
"sum_of_squares" : 8.0210605E7,
"variance" : 526400.0,
"variance_population" : 526400.0,
"variance_sampling" : 658000.0,
"std_deviation" : 725.5342858886821,
"std_deviation_population" : 725.5342858886821,
"std_deviation_sampling" : 811.1719916269299,
"std_deviation_bounds" : {
"upper" : 5390.068571777364,
"lower" : 2487.931428222636,
"upper_population" : 5390.068571777364,
"lower_population" : 2487.931428222636,
"upper_sampling" : 5561.34398325386,
"lower_sampling" : 2316.6560167461403
}
}
}
}
]
}
}
}

-
常用的查询函数
-
histogram:直方图或柱状图统计
用途:用于区间统计,如不同价格商品区间的销售情况
语法:
GET product/_search?size=0 { "aggs": { "<histogram_name>": { "histogram": { "field": "price", #字段名称 "interval": 1000, #区间间隔 "keyed": true, #返回数据的结构化类型 "min_doc_count": <num>, #返回桶的最小文档数阈值,即文档数小于num的桶不会被输出 "missing": 1999 #空值的替换值,即如果文档对应字段的值为空,则默认输出1999(参数值) } } } }
-
查询每个价格区间的数量
GET product/_search?_source=false&size=0
{
"aggs": {
"price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 1000
},
{
"from": 1000,
"to": 2000
},
{
"from": 2000,
"to": 3000
},
{
"from": 3000,
"to": 4000
},
{
"from": 4000,
"to": 5000
},
{
"from": 5000,
"to": 7000
}
]
}
}
}
}
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_range" : {
"buckets" : [
{
"key" : "0.0-1000.0",
"from" : 0.0,
"to" : 1000.0,
"doc_count" : 2
},
{
"key" : "1000.0-2000.0",
"from" : 1000.0,
"to" : 2000.0,
"doc_count" : 0
},
{
"key" : "2000.0-3000.0",
"from" : 2000.0,
"to" : 3000.0,
"doc_count" : 4
},
{
"key" : "3000.0-4000.0",
"from" : 3000.0,
"to" : 4000.0,
"doc_count" : 2
},
{
"key" : "4000.0-5000.0",
"from" : 4000.0,
"to" : 5000.0,
"doc_count" : 2
},
{
"key" : "5000.0-7000.0",
"from" : 5000.0,
"to" : 7000.0,
"doc_count" : 0
}
]
}
}
}
2. #### date-histogram:基于日期的直方图,比如统计一年每个月的销售额
语法:
```json
GET product/_search?size=0
{
"aggs": {
"my_date_histogram": {
"date_histogram": {
"field": "createtime", #字段需为date类型
"<interval_type>": "month", #时间间隔的参数可选项
"format": "yyyy-MM", #日期的格式化输出
"extended_bounds": { #输出空桶
"min": "2020-01",
"max": "2020-12"
}
}
}
}
}
```
interval_type:时间间隔的参数可选项
fixed_interval:ms(毫秒)、s(秒)、 m(分钟)、h(小时)、d(天),注意单位需要带上具体的数值,如2d为两天。需要当心当单位过小,会 导致输出桶过多而导致服务崩溃。
calendar_interval:month、year
interval:(废弃,但是仍然可用)
每个时间区间的数量
GET product/_search?size=0
{
"aggs": {
"price_range": {
"range": {
"field": "createtime",
"ranges": [
{
"from": "2020-05-01",
"to": "2020-05-31"
},
{
"from": "2020-06-01",
"to": "2020-06-30"
},
{
"from": "2020-07-01",
"to": "2020-07-31"
},
{
"from": "2020-08-01"
}
]
}
}
}
}
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_range" : {
"buckets" : [
{
"key" : "2020-05-01T00:00:00.000Z-2020-05-31T00:00:00.000Z",
"from" : 1.5882912E12,
"from_as_string" : "2020-05-01T00:00:00.000Z",
"to" : 1.5908832E12,
"to_as_string" : "2020-05-31T00:00:00.000Z",
"doc_count" : 1
},
{
"key" : "2020-06-01T00:00:00.000Z-2020-06-30T00:00:00.000Z",
"from" : 1.5909696E12,
"from_as_string" : "2020-06-01T00:00:00.000Z",
"to" : 1.5934752E12,
"to_as_string" : "2020-06-30T00:00:00.000Z",
"doc_count" : 2
},
{
"key" : "2020-07-01T00:00:00.000Z-2020-07-31T00:00:00.000Z",
"from" : 1.5935616E12,
"from_as_string" : "2020-07-01T00:00:00.000Z",
"to" : 1.5961536E12,
"to_as_string" : "2020-07-31T00:00:00.000Z",
"doc_count" : 3
},
{
"key" : "2020-08-01T00:00:00.000Z-*",
"from" : 1.59624E12,
"from_as_string" : "2020-08-01T00:00:00.000Z",
"doc_count" : 5
}
]
}
}
}
以1000做为分界线查询数量
空值的处理逻辑 对字段的空值赋予默认值
GET product/_search
{
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 1000,
"keyed": true,
"min_doc_count": 0,
"missing": 1999
}
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_histogram" : {
"buckets" : {
"0.0" : {
"key" : 0.0,
"doc_count" : 2
},
"1000.0" : {
"key" : 1000.0,
"doc_count" : 1
},
"2000.0" : {
"key" : 2000.0,
"doc_count" : 4
},
"3000.0" : {
"key" : 3000.0,
"doc_count" : 2
},
"4000.0" : {
"key" : 4000.0,
"doc_count" : 2
}
}
}
}
}
- percentiles:用于评估当前数值分布情况,比如99 percentile 是 1000 , 是指 99%的数值都在1000以内。常见的一个场景就是我们制定 SLA 的时候常说 99% 的请求延迟都在100ms 以内,这个时候你就可以用 99 percentile 来查一下,看一下 99 percenttile 的值如果在 100ms 以内,就代表SLA达标了。
GET product/_search?size=0
{
"aggs": {
"price_percentiles": {
"percentiles": {
"field": "price",
"percents": [
1,
5,
25,
50,
75,
95,
99
]
}
}
}
}
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_percentiles" : {
"values" : {
"1.0" : 399.00000000000006,
"5.0" : 399.0,
"25.0" : 2998.0,
"50.0" : 2999.0,
"75.0" : 3999.0,
"95.0" : 4999.0,
"99.0" : 4999.0
}
}
}
}
“50.0” : 2999.0, 表示百分之50的数据在2999内
- percentile_ranks: percentile rank 其实就是percentiles的反向查询,比如我想看一下 1000、3000 在当前数值中处于哪一个范围内,你查一下它的 rank,发现是95,99,那么说明有95%的数值都在1000以内,99%的数值都在3000以内。
GET product/_search?size=0
{
"aggs": {
"price_percentiles": {
"percentile_ranks": {
"field": "price",
"values": [
1000,
2000,
3000,
4000,
5000,
6000
]
}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"price_percentiles" : {
"values" : {
"1000.0" : 12.316275490573299,
"2000.0" : 20.015007503751875,
"3000.0" : 50.06666666666667,
"4000.0" : 76.38181818181819,
"5000.0" : 100.0,
"6000.0" : 100.0
}
}
}
}
“3000.0” : 50.06666666666667, 表示3000以内的数据有百分之50
731




被折叠的 条评论
为什么被折叠?



