1、求和sum,最小值min,最大值max,平均值avg
GET blog/_search{
"size": 0, "aggs": {
"sum_id": {
"sum": {
"field": "id"} } }}
参数说明:
size: 0 表示不需要返回参与查询的文档
aggs: aggregation 聚合方法
total_price: 是自定义的返回字段
GET blog/_search{
"size": 0, "aggs": {
"avg_id": {
"avg": {
"field": "id"} } }}
2、分段统计
统计id小于10、10-20、大于20的数量:
GET blog/_search{
"size": 0, "aggs": {
"id_range": {
"range": {
"field": "id", "ranges": [ {
"to": 10 }, {
"from": 10, "to": 20 }, {
"from": 20 } ] } } }}
(分组)按照某个字段中的值来分组并统计分组数量
GET order/_search{
"size": 0, "aggs": { "group_by_sizes": {
"terms": { "field": "size.keyword" } } }}
(分组+查询条件)根据屏幕大小分组统计数量,但只统计小米品牌的手机
GET order/_search{
"query": {
"match": {
"name": "xiaomi" } }, "aggs": {
"group_by_sizes": {
"terms": {
"field": "size.keyword" } } }, "size": 0}
GET blog/_search{
"size": 0, "query": {
"term": {
"title": {
"value": "redis" } }}, "aggs": {
"avg_id": {
"avg": {
"field": "id"} } }}
查询书名中包含 java 的图书的平均价格:
GET books/_search{
"aggs": {
"NAME": {
"filter": {
"term": {
"name": "java" } }, "aggs": {
"avg_price": {
"avg": {
"field": "price" } } } } }}
查询书名中包含 java 或者 office 的图书的平均价格: