需求:
分组求年龄段的年龄
版本: 6.3.3
关键字:
aggs : 聚合关键字 range :范围关键字 请戳这里 关键字详细解释
GET 索引名称/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"按照年龄进行处理": {
"range": {
"script": {
"source": "Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR) - doc['natdate'].value.getYear()"
},
"ranges": [
{
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
},
{
"from": 50,
"to": 60
},
{
"from": 60
}
]
}
},
"按照来源分析进行处理": {
"terms": {
"script": {
"source": "doc['ext_fromnode'].value.substring(0,2)",
"lang": "painless"
}
}
}
}
}
注释: doc['natdate'].value.getYear() 获取 当前字段年 数值 注意 natdate 字段的类型是 date
获取当前年 : Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR)
doc['ext_fromnode'].value.substring(0,2) 截取字符串