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 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
]
}
]
}
}
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
}
}
}
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
}
}
}
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
}
}
}
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
}
}
}
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"
}
查看索引信息
$ curl -XGET "http://172.16.101.55:9200/museums?pretty"
输出
{
"museums" : {
"aliases" : { },
"mappings" : {
"properties" : {
"location" : {
"type" : "geo_point"
}
}
},
"settings" : {
"index" : {
"creation_date" : "",
"number_of_shards" : "",
"number_of_replicas" : "",
"uuid" : "91Br4WhVRZSLlZgpu8dihA",
"version" : {
"created" : ""
},
"provided_name" : "museums"
}
}
}
}
上传测试数据
$ cat geo.json
{"index":{"_id":}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":}}
{"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
}
}
}
}
}
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" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"skipped" : ,
"failed" :
},
"hits" : {
"total" : {
"value" : ,
"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
}
}
}
}
说明:年龄小于等于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" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"skipped" : ,
"failed" :
},
"hits" : {
"total" : {
"value" : ,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"percentiles_age" : {
"values" : {
"30.0" : 39.123456790123456,
"50.0" : 54.0,
"90.0" : 83.0
}
}
}
}
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" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"skipped" : ,
"failed" :
},
"hits" : {
"total" : {
"value" : ,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"percentiles_rank_age" : {
"values" : [
{
"key" : 30.0,
"value" : 17.395
},
{
"key" : 50.0,
"value" : 45.0
}
]
}
}
}
in
, max
, sum
, count
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" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"skipped" : ,
"failed" :
},
"hits" : {
"total" : {
"value" : ,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"stats_age" : {
"count" : ,
"min" : 18.0,
"max" : 90.0,
"avg" : 53.88315,
"sum" : 1077663.0
}
}
}
Elasticsearch Metric聚合的更多相关文章
- Elasticsearch(8) --- 聚合查询(Metric聚合)
Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...
- ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合
Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...
- Elasticsearch(9) --- 聚合查询(Bucket聚合)
Elasticsearch(9) --- 聚合查询(Bucket聚合) 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch(8) --- 聚合查询(Metri ...
- Elasticsearch 之聚合分析入门
本文主要介绍 Elasticsearch 的聚合功能,介绍什么是 Bucket 和 Metric 聚合,以及如何实现嵌套的聚合. 首先来看下聚合(Aggregation): 什么是 Aggregati ...
- Elasticsearch系列---聚合查询原理
概要 本篇主要介绍聚合查询的内部原理,正排索引是如何建立的和优化的,fielddata的使用,最后简单介绍了聚合分析时如何选用深度优先和广度优先. 正排索引 聚合查询的内部原理是什么,Elastich ...
- ElasticSearch 的 聚合(Aggregations)
Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...
- ElasticSearch - 信息聚合系列之聚合过滤
摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版本 elasticsearch版本: elasticsearch-2. ...
- (转)Elasticsearch分析聚合
Elasticsearch不仅仅适合做全文检索,分析聚合功能也很好用.下面通过实例来学习. 一.准备数据 {"index":{ "_index": " ...
- Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作
之前已将spring boot原生方式介绍了,接下将结介绍的是Elasticsearch聚合操作.聚合操作一般来说是解决一下复杂的业务,比如mysql中的求和和分组,由于博主踩的坑比较多,所以博客可能 ...
随机推荐
- delphi中Tkbmmemtable数据转成SQL脚本
unit UMemtableToSql; interface uses SysUtils, Classes, DB, kbmMemTable, Variants, Dialogs, SuperObje ...
- Java线程之如何分析死锁及避免死锁
什么是死锁 java中的死锁是一种编程情况,其中两个或多个线程被永久阻塞,Java死锁情况出现至少两个线程和两个或更多资源. 在这里,我们将写了一个简单的程序,它将导致java死锁场景,然后我们将分析 ...
- getFieldDecorator用法(三)——Table增删改
后台管理系统常用到表单的增删改,这里也做了个封装 例如:user/index.js import React from 'react' import { Card, Button, Table, Fo ...
- 认识HTML语言(CodePen)
认识HTML语言 1.一个网页的加载过程 2.Web技术全览 3.HTML语法 HTML语法(一):标签 HTML语法(四):网页结构 4.HTML常用元素 展示元素 (1)块级元素div (2)内联 ...
- legend3---lavarel多对多模型操作实例
legend3---lavarel多对多模型操作实例 一.总结 一句话总结: 在多对多模型中,增加关系表的数据 需要 弄一个和关系表一对多的模型关系 1.在lavarel关系模型中,课程和标签表是多对 ...
- EBS 修改系统名称
修改EBS登录系统的左上角名称 方法: 修改 配置文件: 地点名称 ,在地点层输入相应的名称即可
- 微信小程序之生成二维码
最近项目中涉及到小程序的生成二维码,很是头疼,经过多次摸索,整理出了自己的一些思想方法,如有不足,欢迎指正. 首先完全按照小程序的结构依次填坑. pages--index.wxml <view ...
- [常用的Cmd运行命令]
打开命令提示符,有很多与系统有关的命令都可以在命令提示符中完成,比如输入ipconfig查看电脑的IP osk 打开屏幕键盘 calc 打开计算器的功能 notepad 打开记事本 mspaint ...
- Canvas学习:封装Canvas绘制基本图形API
Canvas学习:封装Canvas绘制基本图形API Canvas Canvas学习 从前面的文章中我们了解到,通过Canvas中的CanvasRenderingContext2D对象中的属性和方 ...
- 非监督的降维算法--PCA
PCA是一种非监督学习算法,它能够在保留大多数有用信息的情况下,有效降低数据纬度. 它主要应用在以下三个方面: 1. 提升算法速度 2. 压缩数据,减小内存.硬盘空间的消耗 3. 图示化数据,将高纬数 ...