首页 > 解决方案 > 如何在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
            }
        }
    }
}

标签: elasticsearch

解决方案


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
      }
    }
  }
}

推荐阅读