ElasticSearch-聚合metric

DELETE cars
PUT cars
{
  "mappings": {
    "transactions": {
      "properties": {
        "price": {
          "type":"long"
        },
        "color": {
          "type":"keyword"
        },
        "make": {
          "type":"keyword"
        },
        "sold": {
          "type":"date"
        }
      }
    }
  }
}

POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

----------Cardinality Aggregation-------

查看有多少种制造商

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "make_count" : {
            "cardinality" : {
                "field" : "make"
            }
        }
    }
}

----------Avg Aggregation-------

计算平均值

POST /cars/transactions/_search?size=0
{
  "aggs": {
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}

使用脚本

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "avg_price" : {
            "avg" : {
                "script" : {
                    "inline" : "doc.price.value"
                }
            }
        }
    }
}

使用脚本修改计算值

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "avg_fix_price" : {
            "avg" : {
                "field" : "price",
                "script" : {
                    "lang": "painless",
                    "inline": "_value * params.fix",
                    "params" : {
                        "fix" : 1.2
                    }
                }
            }
        }
    }
}

----------Max Aggregation-------

计算最大值

POST /cars/transactions/_search?size=0
{
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}

使用脚本

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "max_price" : {
            "max" : {
                "script" : {
                    "inline" : "doc.price.value"
                }
            }
        }
    }
}

使用脚本修改计算值

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "max_price_rmb" : {
            "max" : {
                "field" : "price",
                "script" : {
                    "lang": "painless",
                    "inline": "_value * params.rate",
                    "params" : {
                        "rate" : 6.78
                    }
                }
            }
        }
    }
}

----------Min Aggregation-------

计算最小值

POST /cars/transactions/_search?size=0
{
  "aggs": {
    "min_price": {
      "min": {
        "field": "price"
      }
    }
  }
}

使用脚本方式同max(略)

----------Sum Aggregation-------

计算求和

POST /cars/transactions/_search?size=0
{
  "aggs": {
    "sum_price": {
      "sum": {
        "field": "price"
      }
    }
  }
}

----------Stats Aggregation-------

统计函数,直接计算count、min。max、avg、sum

POST /cars/transactions/_search?size=0
{
  "aggs": {
    "price_stats": {
      "stats": {
        "field": "price"
      }
    }
  }
}

使用脚本

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "price_stats" : {
            "stats" : {
                "script" : {
                    "inline" : "doc.price.value"
                }
            }
        }
    }
}

使用脚本修改计算值

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "price_stats" : {
            "stats" : {
                "field" : "price",
                "script" : {
                    "lang": "painless",
                    "inline": "_value * params.rate",
                    "params" : {
                        "rate" : 6.78
                    }
                }
            }
        }
    }
}

----------Percentiles Aggregation-------

DELETE website
PUT website
{
  "mappings": {
    "logs": {
      "properties": {
        "latency": {
          "type":"long"
        },
        "zone": {
          "type":"keyword"
        },
        "timestamp": {
          "type":"date"
        }
      }
    }
  }
}

POST /website/logs/_bulk
{ "index": {}}
{ "latency" : 100, "zone" : "US", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 80, "zone" : "US", "timestamp" : "2014-10-29" }
{ "index": {}}
{ "latency" : 99, "zone" : "US", "timestamp" : "2014-10-29" }
{ "index": {}}
{ "latency" : 102, "zone" : "US", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 75, "zone" : "US", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 82, "zone" : "US", "timestamp" : "2014-10-29" }
{ "index": {}}
{ "latency" : 100, "zone" : "EU", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 280, "zone" : "EU", "timestamp" : "2014-10-29" }
{ "index": {}}
{ "latency" : 155, "zone" : "EU", "timestamp" : "2014-10-29" }
{ "index": {}}
{ "latency" : 623, "zone" : "EU", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 380, "zone" : "EU", "timestamp" : "2014-10-28" }
{ "index": {}}
{ "latency" : 319, "zone" : "EU", "timestamp" : "2014-10-29" }

查看百分位时间

默认百分比[ 1, 5, 25, 50, 75, 95, 99 ]

GET /website/logs/_search
{
    "size" : 0,
    "aggs" : {
        "load_times" : {
            "percentiles" : {
                "field" : "latency" 
            }
        }
    }
}

查看百分位时间,自定义百分比

GET /website/logs/_search
{
    "size" : 0,
    "aggs" : {
        "load_times" : {
            "percentiles" : {
                "field" : "latency",
                "percents" : [95, 99, 99.9]
            }
        }
    }
}

百分位使用脚本

GET /website/logs/_search
{
    "size" : 0,
    "aggs" : {
        "load_times" : {
            "percentiles" : {
                "script" : {
                    "lang": "painless",
                    "inline": "doc['latency'].value / params.timeUnit", 
                    "params" : {
                        "timeUnit" : 1000
                    }
                }
            }
        }
    }
}

----------Percentile Ranks Aggregation-------

查看某一个值属于哪个百分位(percentile_ranks)

GET /website/logs/_search
{
  "size": 0,
  "aggs": {
    "zones": {
      "percentile_ranks": {
        "field": "latency",
        "values": [210,800]
      }
    }
  }
}

----------Geo Bounds Aggregation-------

DELETE /museums
PUT /museums
{
    "mappings": {
        "doc": {
            "properties": {
                "location": {
                    "type": "geo_point"
                }
            }
        }
    }
}

POST /museums/doc/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}

计算搜索匹配到的所有地理坐标点最大的边框

POST /museums/_search?size=0
{
    "query" : {
        "match" : { "name" : "musée" }
    },
    "aggs" : {
        "viewport" : {
            "geo_bounds" : {
                "field" : "location", 
                "wrap_longitude" : true 
            }
        }
    }
}

----------Geo Centroid Aggregation-------

计算搜索匹配到的所有地理坐标点的中心点

POST /museums/_search?size=0
{
    "aggs" : {
        "centroid" : {
            "geo_centroid" : {
                "field" : "location" 
            }
        }
    }
}

你可能感兴趣的:(ElasticSearch-聚合metric)