一.聚合分析
1.根据interests的值分组
先将interests的fielddata属性设置为true
PUT ip:9200/test/_mapping/test/
语法:
{
"properties":{
"interests":{
"type":"text",
"fielddata":true
}
}
}
POST ip:9200/test/test/_search
语法:
{
"size":0, # 只展示聚合分析结果,不展示所有文档
"aggs":{ # 聚合
"group_by_interests":{ # 聚合命名
"terms":{
"field":"interests" # 以interests这个字段的值分组
}
}
}
}
结果:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 7
},
{
"key": "sports",
"doc_count": 5 # 有5个文档的interests中含有sports
},
{
"key": "chuiniu",
"doc_count": 1
},
{
"key": "dancing",
"doc_count": 1
}
]
}
}
}
2.搜索+聚合 :查询last_name中含有cui,并对interes的值分组
POST ip:9200/test/test/_search
语法:
{
"size":0,
"query":{
"match":{
"last_name":"cui"
}
},
"aggs":{
"group_by_interests":{
"terms":{
"field":"interests"
}
}
}
}
结果:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 2
},
{
"key": "sports",
"doc_count": 2
}
]
}
}
}
3.聚合嵌套: 先分组+求age的平均值
POST ip:9200/test/test/_search
语法:
{
"size":0,
"aggs":{
"group_by_interests":{
"terms":{
"field":"interests"
},
"aggs":{
"avg_age":{
"avg":{
"field": "age"
}
}
}
}
}
}
结果:
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 7,
"age": {
"value": 21.428571428571427
}
},
{
"key": "sports",
"doc_count": 5,
"age": {
"value": 19
}
},
{
"key": "chuiniu",
"doc_count": 1,
"age": {
"value": 35
}
},
{
"key": "dancing",
"doc_count": 1,
"age": {
"value": 20
}
}
]
}
}
}
4.聚合嵌套: 先分组+求age的平均值+根据age平均值降序排列
POST ip:9200/test/test/_search
语法:
{
"size":0,
"aggs":{
"group_by_interests":{
"terms":{
"field":"interests",
"order":{ # 排序
"avg_age":"desc" #下方的聚合名称
}
},
"aggs":{
"avg_age":{
"avg":{
"field": "age"
}
}
}
}
}
}
结果:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "chuiniu",
"doc_count": 1,
"avg_age": {
"value": 35
}
},
{
"key": "music",
"doc_count": 7,
"avg_age": {
"value": 21.428571428571427
}
},
{
"key": "dancing",
"doc_count": 1,
"avg_age": {
"value": 20
}
},
{
"key": "sports",
"doc_count": 5,
"avg_age": {
"value": 19
}
}
]
}
}
}
5.聚合分析: 年龄区间分组+再按照interests分组+在计算每组的平均年龄
POST ip:9200/test/test/_search
语法:
{
"size":0,
"aggs":{
"range_by_age":{
"range":{
"field":"age",
"ranges":[
{
"from":0,
"to":16
},
{
"from":16,
"to":20
},
{
"from":20,
"to":30
},
{
"from":30,
"to":50
}
]
},
"aggs":{
"group_by_interests":{
"terms":{
"field": "interests"
},
"aggs":{
"avg_age":{
"avg":{
"field":"age"
}
}
}
}
}
}
}
}
结果:
{
"took": 25,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"range_by_age": {
"buckets": [
{
"key": "0.0-16.0",
"from": 0, # 范围:左闭右开 即 [0,16)
"to": 16,
"doc_count": 0,
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key": "16.0-20.0",
"from": 16,
"to": 20,
"doc_count": 4,
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 4,
"avg_age": {
"value": 17.5
}
},
{
"key": "sports",
"doc_count": 4,
"avg_age": {
"value": 17.5
}
}
]
}
},
{
"key": "20.0-30.0",
"from": 20,
"to": 30,
"doc_count": 2,
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "music",
"doc_count": 2,
"avg_age": {
"value": 22.5
}
},
{
"key": "dancing",
"doc_count": 1,
"avg_age": {
"value": 20
}
},
{
"key": "sports",
"doc_count": 1,
"avg_age": {
"value": 25
}
}
]
}
},
{
"key": "30.0-50.0",
"from": 30,
"to": 50,
"doc_count": 1,
"group_by_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "chuiniu",
"doc_count": 1,
"avg_age": {
"value": 35
}
},
{
"key": "music",
"doc_count": 1,
"avg_age": {
"value": 35
}
}
]
}
}
]
}
}
}