elasticsearch - 如何在elasticsearch中比较父字段与子聚合?
问题描述
目前我可以将子聚合值与常量进行比较。但我需要将父文档率(字段)与子文档率的总和进行比较?
下面是索引映射,
{
"my-index-000001": {
"mappings": {
"properties": {
"my_id": {
"type": "keyword"
},
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
},
"rate": {
"type": "long"
},
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
以下是数据:
http://localhost:9200/my-index-000001/_doc/1?refresh
{
"my_id": "1",
"text": "This is a question",
"rate": 9,
"my_join_field": {
"name": "question"
}
}
http://localhost:9200/my-index-000001/_doc/2?refresh
{
"my_id": "2",
"text": "This is another question",
"my_join_field": {
"name": "question"
}
}
http://localhost:9200/my-index-000001/_doc/3?routing=1&refresh
{
"my_id": "3",
"text": "This is an answer",
"rate": 5,
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
http://localhost:9200/my-index-000001/_doc/4?routing=1&refresh
{
"my_id": "4",
"text": "This is another answer",
"rate":3,
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
以下是查询:
{
"aggs": {
"top-child-rate": {
"aggs": {
"number_of_child": {
"aggs": {
"sum_rate_of_child": {
"sum": {
"field": "rate"
}
}
},
"children": {
"type": "answer"
}
},
"avg_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"total_child_rate": "number_of_child>sum_rate_of_child"
},
"script": "params.total_child_rate > 7"
}
},
"top_hits_parent": {
"top_hits": {
"size":1,
"_source": {
"include": [
"text",
"rate"
]
}
}
}
},
"terms": {
"field": "text.keyword",
// "field": "_id",
"size": 10
}
}
}
}
解决方案
sum
您可以使用标准聚合计算父费率总和,然后在以下内容中引用它bucket_selector
:
POST my-index-000001/_search
{
"size": 0,
"aggs": {
"top-child-rate": {
"aggs": {
"total_parent_rate": { <---
"sum": {
"field": "rate"
}
},
"number_of_child": {
"aggs": {
"sum_rate_of_child": {
"sum": {
"field": "rate"
}
}
},
"children": {
"type": "answer"
}
},
"avg_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"total_parent_rate": "total_parent_rate", <---
"total_child_rate": "number_of_child>sum_rate_of_child"
},
"script": "params.total_child_rate > params.total_parent_rate"
}
},
"top_hits_parent": {
"top_hits": {
"size": 1,
"_source": {
"include": [
"text",
"rate"
]
}
}
}
},
"terms": {
"field": "text.keyword",
"size": 10
}
}
}
}
推荐阅读
- java - 使用 System.out.write 方法将 int 转换为字节数组并打印到控制台
- jasper-reports - 导出为 PDF 时,如何在 ireport 的文本字段中的文本后添加空格?
- jenkins - 如何在声明性并行步骤中为每个阶段使用执行器
- arrays - 如何解决“失败:在量角器测试中将循环结构转换为 JSON”
- java - 如何在 javax.lang.model.element TypeElement 中模拟 getDeclaredAnnotation
- c# - C#:如何在没有“复制/粘贴”代码的情况下重写它
- oracle - 将 blob 列中的 pdf 存储到数据库表中,以顶点形式/区域的 pdf 预览?
- python-3.x - 如何知道呼出电话的状态?
- google-apps-script - 谷歌脚本:ui.prompt,按回车而不是点击按钮提交
- asp.net-mvc - 如何使用 C# 通过异步方法使用 Task.WaitAll