Aggregations
Metrics Aggregations
-
Avg Aggregation
作用:计算出字段平均值
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"avg": {
"field": "version_code"
}
}
}
}
#使用脚本
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"avg": {
"script": {
"source":"doc.version_code.value"
}
}
}
}
}
- Weighted Avg Aggregation
作用:算出加权平均值,公式为∑(value * weight) / ∑(weight)
POST /exams/_search
{
"size": 0,
"aggs" : {
"weighted_grade": {
"weighted_avg": {
"value": {
"field": "grade"
},
"weight": {
"field": "weight"
}
}
}
}
}
- Cardinality Aggregation
作用:计算字段的去重后的总数如sql中的distinct
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"cardinality": {
"field": "allneed_lang"
}
}
}
}
# 添加date_histogram可以指定单位时间里面的去重总数
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"months" : {
"date_histogram": {
"field": "dateTime",
"interval": "day"
},
"aggs":{
"123":{
"cardinality": {
"field": "allneed_lang"
}
}
}
}
}
}
- Extended Stats Aggregation
作用:获取字段的数量,最大最小值,平均值,总和,平方差,标准差等
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"extended_stats": {
"field": "version_code"
}
}
}
}
- Geo Bounds Aggregation
作用:地理位置搜索,用于对经纬度的距离搜索,有四种过滤器分别是:
geo_distance: 查找距离某个中心点距离在一定范围内的位置
geo_bounding_box: 查找某个长方形区域内的位置
geo_distance_range: 查找距离某个中心的距离在min和max之间的位置
geo_polygon: 查找位于多边形内的地点
# 数据
{"city": "Beijing", "state": "BJ","location": {"lat": "39.91667", "lon": "116.41667"}}
{"city": "Shanghai", "state": "SH","location": {"lat": "34.50000", "lon": "121.43333"}}
{"city": "Xiamen", "state": "FJ","location": {"lat": "24.46667", "lon": "118.10000"}}
{"city": "Fuzhou", "state": "FJ","location": {"lat": "26.08333", "lon": "119.30000"}}
{"city": "Guangzhou", "state": "GD","location": {"lat": "23.16667", "lon": "113.23333"}}
# 搜索距离厦门500KM的数据
{
"query":{
"filtered":{
"filter":{
"geo_distance" : {
"distance" : "500km",
"location" : {
"lat" : 24.46667,
"lon" : 118.10000
} } } } }
- Max Aggregation,Min Aggregation
作用:获取字段的最小值或最大值
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"max": {
"field": "version_code"
}
}
}
}
- Percentiles Aggregation
作用:求百分位数,分别获得1, 5, 25, 50, 75, 95, 99分位数
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"percentiles": {
"field": "dateTime"
}
}
}
}
- Percentile Ranks Aggregation
作用:计算某字段,在某个范围内的占比
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"percentile_ranks": {
"field": "dateTime",
"values": [1558848262552.2058,1558914598806.6667]
}
}
}
}
#计算时间在values内的两个数值内的占比
- Scripted Metric Aggregation
作用:自定义聚合,通过抒写js代码,分别为init_script,map_script,combine_script,reduce_script赋值
#数据
{"index":{"_id":1}}
{"type": "sale","amount": 80}
{"index":{"_id":2}}
{"type": "cost","amount": 10}
{"index":{"_id":3}}
{"type": "cost","amount": 30}
{"index":{"_id":4}}
{"type": "sale","amount": 130}
#搜索,计算盈利
{
"aggs": {
"profit": {
"scripted_metric": {
"init_script" : {
"id": "my_init_script"
},
"map_script" : {
"id": "my_map_script"
},
"combine_script" : {
"id": "my_combine_script"
},
"params": {
"field": "amount"
},
"reduce_script" : {
"id": "my_reduce_script"
}
}
}
}
}{
"query" : {
"match_all" : {}
},
"aggs": {
"profit": {
"scripted_metric": {
"init_script" : "state.transactions = []",
"map_script" : "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)",
"combine_script" : "double profit = 0; for (t in state.transactions) { profit += t } return profit",
"reduce_script" : "double profit = 0; for (a in states) { profit += a } return profit"
}
}
}
}
- Stats Aggregation
作用:获取该字段的个数,最小最大值,平均值,和总和
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"stats": {
"field": "version_code"
}
}
}
}
- Sum Aggregation
作用:获取字段数值总和
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"sum": {
"field": "version_code"
}
}
}
}
- Top Hits Aggregation
作用:对每组进行筛选
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"terms": {
"field": "entity_id"
},
"aggs": {
"456": {
"top_hits": {
"sort": [
{
"dateTime": {
"order": "desc" #根据时间进行排序
}
}
],
"_source": {
"includes": ["allneed_lang","countryCode"]#需要显示的数据
}
}
}
}
}
}
}
- Value Count Aggregation
作用:计算字段总数
GET tutuapp_android_view_log_2019.05.26/_search
{
"size":0,
"query": {
"term": {
"entity_id": {
"value": "3061402"
}
}
},
"aggs":{
"123":{
"value_count": {
"field": "version_code"
}
}
}
}
网友评论