Elasticsearch 作用域嵌套聚合到查询

Elasticsearch scope nested aggregation to the query

我有一个嵌套数据类型的查询,并且想要 return 嵌套数据类型的统计聚合(由查询过滤)。这是代码:

    GET dan-created/_search
{
  "_source" : ["m_iID", "m_iYear"],
   "query": {
      "nested": {
         "path": "m_PeopleList",
         "query": {
            "match": {
               "m_PeopleList.name": "Daniel"
            }
         },
         "inner_hits" : {}
      }
   },
   "aggregations" : {
    "people" : {
        "nested" : {
          "path" : "m_PeopleList"
        },
        "aggregations" : {
          "averageDist": {
            "stats" : {
              "field":"m_PeopleList.value"
            }
          }
        }
    }
   }
  }

统计信息 returned 是针对整个索引的,但我希望它们仅 return 用于上述查询的匹配项。我在其他地方看到过这方面的例子,但没有看到最新版本的 elasticsearch,我似乎无法让它们工作。

谢谢, 丹尼尔

你可以使用 filter aggregation

"aggregations" : {
  "people" : {
      "nested" : {
        "path" : "m_PeopleList"
      },
      "aggregations" : {
        "myFilter": {
          "filter" : { "match": { "m_PeopleList.name": "Daniel" } },
          "aggregations": {
            "averageDist": {
              "stats" : {
                "field":"m_PeopleList.value"
              }
            }
          }
        }
      }
  }
}