首页 > 解决方案 > 在 `must` 中使用 `aggs` 的弹性查询

问题描述

有什么办法可以让我们aggs在里面must.?? 我有一个包含多个条件和一个聚合的查询,聚合必须是AND子句的一部分(我的意思是must

查询看起来像这样......

{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "actualIndex",
            "min_children": 1,
            "query": {
              "range": {
                "actualIndex.datetime": {
                  "gte": "now-364d/d",
                  "lt": "now/d"
                }
              }
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "has_child": {
                  "type": "actualIndex",
                  "min_children": 3,
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "actualIndex.array1.cloumA": "abc"
                          }
                        },
                        {
                          "term": {
                            "actualIndex.array1.cloumB": "xyz"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "has_child": {
                  "type": "actualIndex",
                  "min_children": 2,
                  "query": {
                    "terms": {
                      "actualIndex.array1.mno": [
                        "value1",
                        "value2",
                        "value3"
                      ]
                    }
                  }
                }
              },
              {
                "query": {
                  "range": {
                    "actualIndex.datetime": {
                      "gte": "now-364d/d",
                      "lt": "now/d"
                    }
                  }
                },
                "aggs": {
                  "groupby": {
                    "composite": {
                      "sources": [
                        {
                          "ids": {
                            "terms": {
                              "field": "ids"
                            }
                          }
                        }
                      ]
                    },
                    "aggs": {
                      "total": {
                        "children": {
                          "type": "actualIndex"
                        },
                        "aggs": {
                          "range_bucket": {
                            "date_range": {
                              "field": "actualIndex.datetime",
                              "keyed": true,
                              "ranges": {
                                "from": "now-364d/d",
                                "to": "now",
                                "key": "bucket"
                              }
                            },
                            "aggs": {
                              "BAS_TOTAL": {
                                "sum": {
                                  "field": "actualIndex.amountField"
                                }
                              }
                            }
                          },
                          "sum_intermediate": {
                            "max_bucket": {
                              "buckets_path": "range_bucket>BAS_TOTAL"
                            }
                          }
                        }
                      },
                      "inventory_selector": {
                        "bucket_selector": {
                          "buckets_path": {
                            "total": "total > sum_intermediate"
                          },
                          "script": "params.total > 1000"
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我只需要的结果也应该aggs是的一部分must

任何人都可以在这里寻求帮助。

PS分数并不重要。

标签: elasticsearch-aggregationelasticsearch-query

解决方案


我们可以使用多个aggs,这个问题可以通过脚本解决。

{
  "inventory_selector": {
    "bucket_selector": {
      "buckets_path": {
        "total": "total > sum_intermediate",
        "actualPrice" : "price>sum_intermediate"
      },
      "script": "params.total > 1000 && params.actualPrice <= 500"
    }
  }
}

&&可以用于must操作,||也可以用于应该操作。


推荐阅读