【发布时间】:2021-03-18 16:51:06
【问题描述】:
目前我可以将子聚合值与常量进行比较。但是我需要将父文档率(字段)与子文档率的总和进行比较?
下面是索引映射,
{
"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
}
}
}
}
【问题讨论】:
-
你能分享映射和示例文档吗?
-
我分享了映射和数据。换句话说=父母率>总和(孩子率)
标签: elasticsearch