目录
目标
ES版本信息
官方文档
实战
新增测试数据
基本语法
百分数分桶聚合
掌握Pipeline aggregations(管道聚合,相当于MySQL中的having )的基本语法。它比另外两种类型的聚合用得较少,这里只举了少量案例供大家参考分析。Pipeline aggregations分为两类:
7.17.5
Pipeline aggregationshttps://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-pipeline.html
#图书馆数据库
PUT /library_db
{
"settings": {
"index": {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
PUT /library_db/_bulk
{"index":{"_id":"1"}}
{"id":1,"type":"玄幻","name":"诛仙","words_num":120,"chapter_num":600,"completion_time":"2000-09-01","author":"萧鼎","prices":32.12}
{"index":{"_id":"2"}}
{"id":2,"type":"玄幻","name":"诛仙前传:蛮荒行","words_num":30,"chapter_num":67,"completion_time":"2020-09-01","author":"萧鼎","prices":23.12}
{"index":{"_id":"3"}}
{"id":3,"type":"武侠","name":"天龙八部","words_num":80,"chapter_num":120,"completion_time":"1995-09-01","author":"金庸","prices":52.1}
{"index":{"_id":"4"}}
{"id":4,"type":"武侠","name":"射雕英雄传","words_num":67,"chapter_num":95,"completion_time":"1998-01-01","author":"金庸","prices":4.12}
{"index":{"_id":"5"}}
{"id":5,"type":"武侠","name":"神雕侠侣","words_num":75,"chapter_num":76,"completion_time":"2000-01-01","author":"金庸","prices":32.8}
{"index":{"_id":"6"}}
{"id":5,"type":"武侠","name":"倚天屠龙记","words_num":83,"chapter_num":130,"completion_time":"2003-01-01","author":"金庸","prices":100.12}
{"index":{"_id":"7"}}
{"id":7,"type":"玄幻","name":"凡人修仙传","words_num":600,"chapter_num":3000,"completion_time":"2018-01-01","author":"忘语","prices":120.12}
{"index":{"_id":"8"}}
{"id":8,"type":"玄幻","name":"魔天记","words_num":159,"chapter_num":400,"completion_time":"2019-01-01","author":"忘语","prices":11.12}
{"index":{"_id":"9"}}
{"id":9,"type":"都市异能","name":"黄金瞳","words_num":220,"chapter_num":400,"completion_time":"2019-01-01","author":"打眼","prices":74.5}
{"index":{"_id":"10"}}
{"id":10,"type":"玄幻","name":"将夜","words_num":210,"chapter_num":600,"completion_time":"2014-01-01","author":"血红","prices":32.0}
{"index":{"_id":"11"}}
{"id":11,"type":"军事","name":"亮剑","words_num":120,"chapter_num":100,"completion_time":"2012-01-01","author":"都梁","prices":15.0}
需求一:求每个作者下面字数最多的小说的字数,按照作品数量降序排序。
分析:第一步求出每个作者的小说数量,按照降序排序;第二步求出每个作者下字数最多的小说的字数。
POST /library_db/_search
{
"size": 0,
"aggs": {
"author_group": {
"terms": {
"field": "author.keyword",
"size": 10,
"order": {
"_key": "desc"
}
},
"aggs": {
"max_words_num": {
"max": {
"field": "words_num"
}
}
}
}
}
}
需求二:求小说的每个类型的平均字数,求出最大的平均字数。
POST /library_db/_search
{
"size": 0,
"aggs": {
"author_group": {
"terms": {
"field": "type.keyword",
"size": 10,
"order": {
"_key": "desc"
}
},
"aggs": {
"avg_words_num": {
"avg": {
"field": "words_num"
}
}
}
},
"max_avg_words_num":{
"max_bucket": {
"buckets_path": "author_group>avg_words_num"
}
}
}
}
需求:求小说的每个类型的平均字数,求出平均字数的百分位。
POST /library_db/_search
{
"size": 0,
"aggs": {
"author_group": {
"terms": {
"field": "type.keyword",
"size": 10,
"order": {
"_key": "desc"
}
},
"aggs": {
"avg_words_num": {
"avg": {
"field": "words_num"
}
}
}
},
"percentiles_avg_words_num":{
"percentiles_bucket": {
"buckets_path": "author_group>avg_words_num",
"percents": [ 25.0, 50.0, 75.0,100 ]
}
}
}
}