如何通过elasticsearch中的特定值聚合关键字字段
How to aggregate keyword field by a specific value in elasticsearch
我正在寻找通过 keyword
字段中的特定值聚合数据的解决方案。
这是数据:
"message" : "status: 123, msg: blablabla",
"message" : "start_at: 20190701, source: location_a",
"message" : "status: 456, msg: blabla",
"message" : "start_at: 20190701, source: location_b",
"message" : "status: 123, msg: blablablabla",
(message
是 keyword
字段)
而且,我通过以下方式查询了这个索引:
GET my_index/_search
{
"query": {
"match": {
"message": {
"query": "status"
}
}
}
}
然后,我得到如下结果:
{
"hits" : [
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 123, msg: blablabla""",
}
},
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 456, msg: blabla""",
}
},
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 123, msg: blablablabla""",
}
}
]
}
现在,我希望通过status
的值来聚合数据,例如:
{
"aggregations" : {
"status" : {
"buckets" : {
"123" : {
"doc_count" : 250
},
"456" : {
"doc_count" : 248
},
"789" : {
"doc_count" : 2356
}
}
}
}
}
(原始数据中有超过100+个不同的state
。)
那么,如何汇总这些数据?
(ps。我正在使用 Elasticsearch 6.5)
可以在术语聚合中使用无痛脚本
GET my_index/_search
{
"size": 0,
"aggs": {
"genres": {
"terms": {
"script": {
"inline": "def field = 'status: '; def msg = doc['message.keyword'].value; def start = msg.indexOf(field); def end = msg.indexOf(',', start); if(start > -1) {return msg.substring(start+field.length(), end)}"
}
}
}
}
}
示例输出:
"aggregations": {
"genres": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "123",
"doc_count": 2
},
{
"key": "456",
"doc_count": 1
}
]
}
}
首先脚本使用 indexOf 方法找到 status:
的位置,然后找到直接的 ,
这两个索引位置用于使用子字符串方法提取状态值
我正在寻找通过 keyword
字段中的特定值聚合数据的解决方案。
这是数据:
"message" : "status: 123, msg: blablabla",
"message" : "start_at: 20190701, source: location_a",
"message" : "status: 456, msg: blabla",
"message" : "start_at: 20190701, source: location_b",
"message" : "status: 123, msg: blablablabla",
(message
是 keyword
字段)
而且,我通过以下方式查询了这个索引:
GET my_index/_search
{
"query": {
"match": {
"message": {
"query": "status"
}
}
}
}
然后,我得到如下结果:
{
"hits" : [
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 123, msg: blablabla""",
}
},
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 456, msg: blabla""",
}
},
{
"_index" : "2019.07.25",
"_source" : {
"message" : """status: 123, msg: blablablabla""",
}
}
]
}
现在,我希望通过status
的值来聚合数据,例如:
{
"aggregations" : {
"status" : {
"buckets" : {
"123" : {
"doc_count" : 250
},
"456" : {
"doc_count" : 248
},
"789" : {
"doc_count" : 2356
}
}
}
}
}
(原始数据中有超过100+个不同的state
。)
那么,如何汇总这些数据?
(ps。我正在使用 Elasticsearch 6.5)
可以在术语聚合中使用无痛脚本
GET my_index/_search
{
"size": 0,
"aggs": {
"genres": {
"terms": {
"script": {
"inline": "def field = 'status: '; def msg = doc['message.keyword'].value; def start = msg.indexOf(field); def end = msg.indexOf(',', start); if(start > -1) {return msg.substring(start+field.length(), end)}"
}
}
}
}
}
示例输出:
"aggregations": {
"genres": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "123",
"doc_count": 2
},
{
"key": "456",
"doc_count": 1
}
]
}
}
首先脚本使用 indexOf 方法找到 status:
的位置,然后找到直接的 ,
这两个索引位置用于使用子字符串方法提取状态值