elasticsearch之聚合函数
Posted s686zhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch之聚合函数相关的知识,希望对你有一定的参考价值。
准备数据
PUT zhifou/doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] } PUT zhifou/doc/2 { "name":"大娘子", "age":18, "from":"sheng", "desc":"肤白貌美,娇憨可爱", "tags":["白", "富","美"] } PUT zhifou/doc/3 { "name":"龙套偏房", "age":22, "from":"gu", "desc":"mmp,没怎么看,不知道怎么形容", "tags":["造数据", "真","难"] } PUT zhifou/doc/4 { "name":"石头", "age":29, "from":"gu", "desc":"粗中有细,狐假虎威", "tags":["粗", "大","猛"] } PUT zhifou/doc/5 { "name":"魏行首", "age":25, "from":"广云台", "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!", "tags":["闭月","羞花"] }
avg
需求: 查询 from 是 gu 的 人的平均年龄。
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_avg": { "avg": { "field": "age" } } }, "_source": ["name", "age"] }
如果我们只想看 平均值 my_avg 的结果怎么做?
利用size
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_avg": { "avg": { "field": "age" } } }, "size": 0, }
max
需求: 查询from 为gu 数据中 最大的age
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_max": { "max": { "field": "age" } } }, "size": 0 }
min
同理上面的max
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_min": { "min": { "field": "age" } } }, "size": 0 }
sum
需求: 查询from为gu 的数据的 age的总和
GET zhifou/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_sum": { "sum": { "field": "age" } } }, "size": 0 }
分组查询
需求: 查询所有人的年龄段,并且按照15-20,20--25,25-30 分组, 并计算每组的平均年龄
GET zhifou/doc/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "age_group": { "range": { "field": "age", "ranges": [ { "from": 15, "to": 20 }, { "from": 20, "to": 25 }, { "from": 25, "to": 30 } ] }, "aggs": { "my_avg": { "avg": { "field": "age" } } } } } }
注意:聚合函数的使用,一定是先查出结果,然后对结果使用聚合函数做处理
以上是关于elasticsearch之聚合函数的主要内容,如果未能解决你的问题,请参考以下文章
ElasticSearch进阶篇之聚合(aggregations)和映射(mapping)
Elasticsearch之指标,分桶,管道聚合之操作类ElasticsearchRestTemplate和RestHighLevelClient以及dsl