elasticsearch 权威指南聚合阅读笔记(七)

count(1)

select clssId,count(1) from student group by  classId

{
  "size":0,
  "aggs": {
    "group_by_classId": {
      "terms": {
        "field": "classId.keyword"
      }
    }
  }
}

结果 key为classId  doc_count 为count

size 0表示只看聚合结果不看搜索结果

{
    "took": 82,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_classId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "2",
                    "doc_count": 3
                },
                {
                    "key": "5",
                    "doc_count": 1
                }
            ]
        }
    }
}

count+avg

select clssId,count(1),avg(score) from student group by  classId

{
  "size":0,
  "aggs": {
    "group_by_classId": {
      "terms": {
        "field": "classId.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "score"
          }
        }
      }
    }
  }
}

结果

{
    "took": 95,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "group_by_classId": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "2",
                    "doc_count": 3,
                    "average_balance": {
                        "value": 53.333333333333336
                    }
                },
                {
                    "key": "5",
                    "doc_count": 1,
                    "average_balance": {
                        "value": 10
                    }
                }
            ]
        }
    }
}

聚合并排序

select clssId,count(1),avg(score) from student group by  classId order by  avg(score)  desc

{
  "size":0,
  "aggs": {
    "group_by_classId": {
      "terms": {
        "field": "classId.keyword",
        "order": {
          "average_score": "desc"
        }
      },
      "aggs": {
        "average_score": {
          "avg": {
            "field": "score"
          }
        }
      }
    }
  }
}

Metric Aggregations

地理边界聚合

1.添加测试mapping

{
    "mappings": {
        "doc": {
            "properties": {
                "location": {
                    "type": "geo_point"
                }
            }
        }
    }
}

2.添加测试数据

{
  "text": "成都",
  "location": {
    "lat": 11.12,
    "lon": -5.34
  }
}
{
  "text": "广州",
  "location": {
    "lat": 66.12,
    "lon": -22.34
  }
}
{
  "text": "深圳",
  "location": {
    "lat": 121.12,
    "lon": -77.34
  }
}

测试query

{
    "size":0,
    "query" : {
        "match_all" : { }
    },
    "aggs" : {
        "viewport" : {
            "geo_bounds" : {
                "field" : "location", 
                "wrap_longitude" : true  //可选参数,指定边界框是否允许与国际日期变更线重叠,默认值是true。
            } 
        } 
    } 
}

结果

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "viewport": {
            "bounds": {
                "top_left": {
                    "lat": 41.1199999647215,
                    "lon": -71.34000004269183
                },
                "bottom_right": {
                    "lat": 11.119999992661178,
                    "lon": -5.340000037103891
                }
            }
        }
    }
}

地理重心聚合

用以上测试数据

query

{
    "size":0,
    "query" : {
        "match_all" : { }
    },
    "aggs" : {
        "centroid" : {
            "geo_centroid" : {
                "field" : "location"  
            } 
        }
    } 
}

结果

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "centroid": {
            "location": {
                "lat": 26.11999997869134,
                "lon": -38.34000003989786
            },
            "count": 2
        }
    }
}

平均值聚合

{
    "size":0,
    "aggs" : {
        "avg_grade" : { "avg" : { "field" : "mdProductId" } }
    }
}

count聚合

{
    "aggs" : {
        "types_count" : { "value_count" : { "field" : "type" } }
    }
}

最大值聚合

{
    "aggs" : {
        "max_price" : { "max" : { "field" : "price" } }
    }
}

最小值聚合

{
    "aggs" : {
        "min_price" : { "min" : { "field" : "price" } }
    }
}

使用脚本聚合

如最小值为例子

{
    "aggs" : {
        "min_price" : {
            "min" : {
                "script" : {
                    "inline" : "doc.price.value"
                }
            }
        }
    }
}

设置聚合默认值

POST /sales/_search 
{ 
    "aggs" : { 
        "grade_max" : { 
            "max" : { 
                "field" : "grade", 
                "missing": 10  
            } 
        } 
    } 
}

聚合结果脚本运算

{ 
    "aggs" : { 
        "min_price_in_euros" : { 
            "min" : { 
                "field" : "price", 
                "script" : { 
                    "inline" : "_value * params.conversion_rate", 
                    "params" : { 
                        "conversion_rate" : 1.2 
                    } 
                } 
            } 
        } 
    } 
}

 父子聚合

{
    "size": 0,
    "aggs": {
        "detail": {
            "children": {
                "type": "ic_product_store_account"--子文档
            },
            "aggs": {
                "sum_price": {
                    "sum": {
                        "field": "sumCount"--子文档聚合字段
                    }
                }
            }
        }
    }
}

 goupBy语法

avg

{
    "size": 0,
    "aggs" : {
        "group_by_tags" : {
            "terms" : { "field" : "productId" },
            "aggs" : {
                "avg_price" : {
                    "avg" : { "field" : "price" }
                }
            }
        }
    }
}

select  productId,avg(price) from type  grup by  productId

需要查询条件加上query语法就行了

 

转载于:https://www.cnblogs.com/LQBlog/p/10539245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值