中位数的计算:
(1)首先要先排序(从小到大),然后计算中位数的序号,分数据为奇数个与偶数个两种来求.
(2)中位数算出来可避免极端数据,代表着数据总体的中等情况。
(3)如果总数个数是奇数的话,按从小到大的顺序,取中间的那个数
(4)如果总数个数是偶数个的话,按从小到大的顺序,取中间那两个数的平均数
1、创建一个测试索引
PUT test
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text"
},
"score": {
"type": "double"
}
}
}
}
2、添加测试数据
POST test/_doc/1
{
"id": 1,
"name": "aaa",
"score": 96
}
POST test/_doc/2
{
"id": 2,
"name": "bbb",
"score": 90
}
POST test/_doc/3
{
"id": 3,
"name": "bbb",
"score": 90
}
POST test/_doc/4
{
"id": 4,
"name": "ccc",
"score": 87
}
POST test/_doc/5
{
"id": 5,
"name": "ddd",
"score": 80
}
POST test/_doc/6
{
"id": 6,
"name": "eee",
"score": 78
}
3、求score分数的中位数
GET test/_search?pretty
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match_all": {}
}
]
}
},
"aggs": {
"percentiles": {
"percentiles": {
"field": "score",
"percents": [
50
]
}
}
}
}
获取结果
{
"took" : 275,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"percentiles" : {
"values" : {
"50.0" : 88.5
}
}
}
}