我将添加免责声明,即只运行两个单独的计数并在客户端执行计算,如下所示:
GET /INDEX/_search
{
"size": 0,
"aggs": {
"types": {
"terms": {
"field": "type",
"size": 10
}
}
}
}
这会返回类似的东西(除了在我的示例中使用不同的键而不是类型):
"aggregations": {
"types": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Article",
"doc_count": 881
},
{
"key": "Page",
"doc_count": 301
}
]
}
使用它,进行不同的计数并计算平均值。
如上所述,这是我从(通过单个请求)this 整理出来的 hacky 方式
GET /INDEX/_search
{
"size": 0,
"aggs": {
"parent_agg": {
"terms": {
"script": "'This approach is a weird hack'"
},
"aggs": {
"four_oh_fours": {
"filter": {
"term": {
"message": "404 not found"
}
},
"aggs": {
"count": {
"value_count": {
"field": "_index"
}
}
}
},
"five_hundreds": {
"filter": {
"term": {
"message": "500 error"
}
},
"aggs": {
"count": {
"value_count": {
"field": "_index"
}
}
}
},
"404s_over_500s": {
"bucket_script": {
"buckets_path": {
"four_oh_fours": "four_oh_fours.count",
"five_hundreds": "five_hundreds.count"
},
"script": "return params.four_oh_fours / (params.five_hundreds == 0 ? 1: params.five_hundreds)"
}
}
}
}
}
}
这应该根据脚本中的计算返回一个聚合值。
如果有人可以提供这两种方法之外的方法,我很乐意看到它。希望这会有所帮助。
编辑 - 通过“表达式”类型而不是无痛(默认)完成的相同脚本。只需将上面的脚本值替换为以下内容:
"script": {
"inline": "four_oh_fours / (five_hundreds == 0 ? 1 : five_hundreds)",
"lang": "expression"
}
在这里更新了脚本以通过 Lucene 表达式完成同样的事情