# elasticsearch 笔记四 之聚合查询之去重计数、基础统计、百分位、字符串统计

1. 去重统计 cardinality
2. 基础统计 stats
3. 百分位 percentiles
4. 字符串统计 string_stats

### 1、去重统计 cardinality

PUT /exam/_doc/1
{
"name" : "hunter",
}
PUT /exam/_doc/2
{
"name" : "paul",
}
PUT /exam/_doc/3
{
"name" : "jack",
}
PUT /exam/_doc/4
{
"name" : "tom",
}


GET /exam/_search
{
"size": 0,
"aggs": {
"cardinality": {
}
}
}
}


  "aggregations" : {
"value" : 3
}
}


SELECT COUNT(DISTINCT grade) FROM table_name;


### 2、统计 stats

GET /bank/_search
{
"size": 0,
"aggs": {
"age_stats": {
"stats": {
"field": "age"
}
}
}
}


  "aggregations" : {
"age_stats" : {
"count" : 1000,
"min" : 20.0,
"max" : 40.0,
"avg" : 30.171,
"sum" : 30171.0
}
}


GET /bank/_search
{
"size": 0,
"aggs": {
"agg_stats": {
"stats": {
"script": {
"lang": "painless",
"source": "doc['age'].value"
}
}
}
}
}


  "aggregations" : {
"agg_stats" : {
"count" : 1000,
"min" : 20.0,
"max" : 40.0,
"avg" : 30.171,
"sum" : 30171.0
}
}


GET /bank/_search
{
"size": 0,
"aggs": {
"agg_stats": {
"stats": {
"field": "age",
"script": {
"lang": "painless",
"source": "_value * params.correction",
"params": {
"correction": 1.2
}
}
}
}
}
}


  "aggregations" : {
"agg_stats" : {
"count" : 1000,
"min" : 24.0,
"max" : 48.0,
"avg" : 36.2052,
"sum" : 36205.2
}
}


### 3、百分位 percentiles

GET /bank/_search
{
"size": 0,
"aggs": {
"age_percentiles": {
"percentiles": {
"field": "age"
}
}
}
}


  "aggregations" : {
"age_percentiles" : {
"values" : {
"1.0" : 20.0,
"5.0" : 21.0,
"25.0" : 25.0,
"50.0" : 31.0,
"75.0" : 35.0,
"95.0" : 39.0,
"99.0" : 40.0
}
}
}


GET /bank/_search
{
"size": 0,
"aggs": {
"age_percentiles": {
"percentiles": {
"field": "age",
"percents": [
1,
50,
95,
99,
99.5
]
}
}
}
}


GET /bank/_search
{
"size": 0,
"aggs": {
"age_percentiles": {
"percentiles": {
"field": "age",
"keyed": false,
"percents": [
95,
99
]
}
}
}
}


GET /bank/_search
{
"size": 0,
"aggs": {
"age_percent_ranks": {
"percentile_ranks": {
"field": "age",
"values": [
35,
37,
40
]
}
}
}
}


  "aggregations" : {
"age_percent_ranks" : {
"values" : {
"35.0" : 75.5,
"37.0" : 85.3,
"40.0" : 100.0
}
}
}


### 4、字符串统计 string_stats

GET /exam/_search
{
"size": 0,
"aggs": {
"message_stats": {
"string_stats": {
"field": "name.keyword"
}
}
}
}


GET /exam/_search
{
"size": 0,
"aggs": {
"name_stats": {
"string_stats": {
"field": "name.keyword",
"show_distribution": true
}
}
}
}


"aggregations" : {
"name_stats" : {
"count" : 4,
"min_length" : 3,
"max_length" : 6,
"avg_length" : 4.25,
"entropy" : 3.7345216647797517,
"distribution" : {
"a" : 0.11764705882352941,
"t" : 0.11764705882352941,
"u" : 0.11764705882352941,
"c" : 0.058823529411764705,
"e" : 0.058823529411764705,
"h" : 0.058823529411764705,
"j" : 0.058823529411764705,
"k" : 0.058823529411764705,
"l" : 0.058823529411764705,
"m" : 0.058823529411764705,
"n" : 0.058823529411764705,
"o" : 0.058823529411764705,
"p" : 0.058823529411764705,
"r" : 0.058823529411764705
}
}
}


GET /exam/_search
{
"size": 0,
"aggs": {
"name_stats": {
"string_stats": {
"script": {
"lang": "painless",
"source": "doc['name.keyword'].value"
},
"show_distribution": true
}
}
}
}

GET /exam/_search
{
"size": 0,
"aggs": {
"name_stats": {
"string_stats": {
"field": "name.keyword",
"script": {
"lang": "painless",
"source": "params.prefix + _value",
"params": {
"prefix": "name: "
}
}
}
}
}
}


01-08 9738

05-17 93

11-21 488

02-28 5万+

10-23 1885

03-02 9602

11-20 7453

06-20 1万+

12-16

01-27 1万+

03-11 1132

10-22 302

04-28 113

04-26 3万+

05-12 31

05-13 43

03-15 887

08-21 141

12-08 17

01-15 96

#### Elasticsearch实战——聚合分析

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客