Elasticsearch Metric聚合

首先查看index文档信息

$ curl -XGET "http://172.16.101.55:9200/_cat/indices?v"

输出

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer DvLoM7NjSYyjTwD5BSkK3A   1   1      20000            0       10mb           10mb

查看当前elasticsearch中的数据信息

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "query": { "match_all": {} }, "sort": [ { "customerid": "desc" } ], "from": 0, "size": 1 }'

输出

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "_doc",
        "_id" : "20000",
        "_score" : null,
        "_source" : {
          "customerid" : 20000,
          "firstname" : "WODADM",
          "lastname" : "AEBUFMJAWZ",
          "address1" : "6224597470 Dell Way",
          "address2" : null,
          "city" : "DVCINXG",
          "state" : null,
          "zip" : 0,
          "country" : "Australia",
          "region" : 2,
          "email" : "AEBUFMJAWZ@dell.com",
          "phone" : "6224597470",
          "creditcardtype" : 3,
          "creditcard" : "1869697669055313",
          "creditcardexpiration" : "2010/07",
          "username" : "user20000",
          "password" : "password",
          "age" : 37,
          "income" : 40000,
          "gender" : "F"
        },
        "sort" : [
          20000
        ]
      }
    ]
  }
}
View Code

avg:求平均值

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "avg_age": { "avg": { "field": "age" } } } }'

输出

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_age" : {
      "value" : 53.88315
    }
  }
}
View Code

 min:求最小值

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "avg_age": { "min": { "field": "age" } } } }'

 输出

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_age" : {
      "value" : 18.0
    }
  }
}
View Code

 max:求最大值

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "avg_age": { "max": { "field": "age" } } } }'

 输出

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "avg_age" : {
      "value" : 90.0
    }
  }
}
View Code

 cardinality:去重

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "cardinality_country": { "cardinality": { "field": "country", "precision_threshold" : 100 } } } }'

 注:precision_threshold选项表名我们确保当字段唯一值在 100 以内时会得到非常准确的结果

输出

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "cardinality_country" : {
      "value" : 12
    }
  }
}
View Code

 geo bounds:空间索引

新建图书馆索引

$ curl -XPUT "http://172.16.101.55:9200/museums?pretty" -H "Content-Type: application/json" -d '{ "mappings": { "properties": { "location": { "type": "geo_point"} } } }'

输出

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "museums"
}
View Code

查看索引信息

$ curl -XGET "http://172.16.101.55:9200/museums?pretty"

输出

{
  "museums" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "location" : {
          "type" : "geo_point"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1576335118344",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "91Br4WhVRZSLlZgpu8dihA",
        "version" : {
          "created" : "7040299"
        },
        "provided_name" : "museums"
      }
    }
  }
}
View Code

上传测试数据

$ cat geo.json 
{"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"}
$ curl -H "Content-Type: application/json" -XPOST "http://172.16.101.55:9200/museums/_bulk?pretty&refresh" --data-binary "@geo.json"

查看

$ curl -XPOST "http://172.16.101.55:9200/museums/_search?pretty" -H "Content-Type: application/json" -d '{ "size":0, "query": {"match" : { "name" : "musée" } }, "aggs": {"viewport": {"geo_bounds": {"field": "location", "wrap_longitude": true } } } }'

输出

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "viewport" : {
      "bounds" : {
        "top_left" : {
          "lat" : 48.86111099738628,
          "lon" : 2.3269999679178
        },
        "bottom_right" : {
          "lat" : 48.85999997612089,
          "lon" : 2.3363889567553997
        }
      }
    }
  }
}
View Code

Percentiles:求一个numberic类型的文档范围占总文档的百分比

查看

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "percentiles_age": { "percentiles": { "field": "age" } } } }'

输出

{
  "took" : 41,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "percentiles_age" : {
      "values" : {
        "1.0" : 18.0,
        "5.0" : 21.0,
        "25.0" : 35.543352601156066,
        "50.0" : 54.0,
        "75.0" : 72.0,
        "95.0" : 87.0,
        "99.0" : 90.0
      }
    }
  }
}
View Code

说明:年龄小于等于18岁的文档数占总文档数为1%,年龄小于等于54岁的文档数占总文档数小于等于50%

默认的范围为[ 1, 5, 25, 50, 75, 95, 99 ],我们可以自定义

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "percentiles_age": { "percentiles": { "field": "age", "percents": [30, 50, 90] } } } }'

输出

{
  "took" : 46,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "percentiles_age" : {
      "values" : {
        "30.0" : 39.123456790123456,
        "50.0" : 54.0,
        "90.0" : 83.0
      }
    }
  }
}
View Code

 Percentile rank:查看给定范围内的文档值占总文档比例

查看年龄小于等于30和年龄小于等于50的文档比例

$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "percentiles_rank_age": { "percentile_ranks": { "field": "age", "values": [30, 50], "keyed": "false" } } } }'

输出

{
  "took" : 41,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "percentiles_rank_age" : {
      "values" : [
        {
          "key" : 30.0,
          "value" : 17.395
        },
        {
          "key" : 50.0,
          "value" : 45.0
        }
      ]
    }
  }
}
View Code
stats:返回inmaxsumcount and avg
$ curl -XGET "http://172.16.101.55:9200/customer/_search?pretty" -H "Content-Type: application/json" -d '{ "size": 0, "aggs": { "stats_age": { "stats": { "field": "age" } } } }'

输出

{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "stats_age" : {
      "count" : 20000,
      "min" : 18.0,
      "max" : 90.0,
      "avg" : 53.88315,
      "sum" : 1077663.0
    }
  }
}
View Code
 
 
 
 
 
原文地址:https://www.cnblogs.com/ilifeilong/p/12022058.html