0

让我们称我的根级别foo和我的子级别events。我想在级别上进行聚合,但使用具有颜色“橙色”或父级具有 customerId“35”events的过滤器。eventfoo

所以,我想要一个嵌套聚合内的过滤聚合。在这个过滤器的查询子句中,我有一个孩子指的是一个字段 onfoo而另一个指的是一个字段 on events。但是,第一个孩子无法像那样实际引用父母!我不能使用 reverse_nested 聚合,因为我不能将其中一个作为复合查询的子级,并且我不能在嵌套之前进行过滤,因为那样我会失去 OR 语义。如何引用该字段foo

如果有帮助,请举个具体的例子。映射:

{
  "foo": {
    "properties": {
      "customer_id": { "type": "long" },
      "events": {
        "type": "nested",
        "properties": {
          "color": { "type": "keyword" },
          "coord_y": { "type": "double" }
        }
      }
    }
  }
}

foo(为清楚起见更新:这是一个以根映射命名的索引foo

我希望能够进行的查询:

{
  "aggs": {
    "OP0_nest": {
      "nested": { "path": "events" },
      "aggs": {
        "OP0_custom_filter": {
          "filter": {
            "bool": {
              "should": [
                { "term": { "events.color": "orange" } },
                { "term": { "customer_id": 35 } }
              ]
            }
          },
          "aggs": {
            "OP0_op": {
              "avg": { "field": "events.coord_y" }
            }
          }
        }
      }
    }
  }
}

当然,这不起作用,因为should包含子句的子句customer_id不起作用。该术语查询始终为假,因为customer_id无法在嵌套聚合中访问。

提前致谢!

4

1 回答 1

3

由于您要应用过滤器的字段位于不同的级别,您需要分别对每个级别进行查询并将它们放在查询should子句中,bool这将成为filter我们过滤器聚合的子句。在这个聚合中,我们然后添加一个嵌套聚合来获取coord_y.

聚合将是(更新:因为foo索引名称已从foo字段名称中删除):

{
  "aggs": {
    "OP0_custom_filter": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "customer_id": 35
              }
            },
            {
              "nested": {
                "path": "events",
                "query": {
                  "term": {
                    "events.color": "orange"
                  }
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "OP0_op": {
          "nested": {
            "path": "events"
          },
          "aggs": {
            "OP0_op_avg": {
              "avg": {
                "field": "events.coord_y"
              }
            }
          }
        }
      }
    }
  }
}
于 2018-12-14T03:15:42.610 回答