Metrics Aggregations

2019-06-26  本文已影响0人  ZplD

Aggregations

Metrics Aggregations

 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"
         }
       }
     }
   }
}

 POST /exams/_search
{
   "size": 0,
   "aggs" : {
       "weighted_grade": {
           "weighted_avg": {
               "value": {
                   "field": "grade"
               },
               "weight": {
                   "field": "weight"
               }
           }
       }
   }
}
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"
        }
      }
      }
    }
}
}
GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "extended_stats": {
          "field": "version_code"
        }
      }
    }
}

# 数据
{"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
                }            }        }    }    }
GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "max": {
          "field": "version_code"
        }
      }
    }
}
GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "percentiles": {
          "field": "dateTime"
        }
      }
    }
}
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内的两个数值内的占比
#数据
{"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"
            }
        }
    }
}

GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "stats": {
          "field": "version_code"
        }
      }
    }
}
GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "sum": {
          "field": "version_code"
        }
      }
    }
}
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"]#需要显示的数据
                }
            }
          }
        }
      }
    }
}
GET tutuapp_android_view_log_2019.05.26/_search
{
  "size":0,
  "query": {
    "term": {
      "entity_id": {
        "value": "3061402"
      }
    }
  },
"aggs":{
      "123":{
        "value_count": {
          "field": "version_code"
        }
      }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读