ElasticSearch C#client(NEST):访问嵌套聚合结果

jhi*_*den 5 c# aggregation elasticsearch

我在NEST(ElasticSearch C#客户端)中有以下查询,请注意嵌套聚合:

            var query = _elasticClient.Search<Auth5209>(s => s
                .Size(0)
                .Aggregations(a=> a
                    .Terms("incidentID", t=> t
                        .Field(f=>f.IncidentID)
                        .Size(5)
                        .Aggregations(a2 => a2
                            .Stats("authDateStats", s1=>s1.Field(f=>f.AuthEventDate))
                        )
                    )                        
                )
                );
Run Code Online (Sandbox Code Playgroud)

这正确地生成以下查询:

{
  "size": 0,
  "aggs": {
    "incidentID": {
      "terms": {
        "field": "incidentID",
        "size": 5
      },
      "aggs": {
        "authDateStats": {
          "stats": {
            "field": "authEventDate"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下结果:

"aggregations" : {
    "incidentID" : {
        "buckets" : [{
                "key" : "0A631EB1-01EF-DC28-9503-FC28FE695C6D",
                "doc_count" : 233,
                "authDateStats" : {
                    "count" : 233,
                    "min" : 1401167036075,
                    "max" : 1401168969907,
                    "avg" : 1401167885682.6782,
                    "sum" : 326472117364064
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚我是如何访问"authDateStats"部分的.当我调试时,我没有看到任何方式来访问数据.

在此输入图像描述

Jam*_*ake 5

官方文档或此处的答案都不适用于Nest 2.0+。尽管jhilden的回答确实使我走上了正确的道路。

这是可以与nest 2.0+一起使用的类似查询的工作示例:

        const string termsAggregation = "device_number";
        const string topHitsAggregation = "top_hits";

        var response = await _elasticsearchClient.Client.SearchAsync<CustomerDeviceModel>(s => s
            .Aggregations(a => a
                .Terms(termsAggregation, ta => ta
                    .Field(o => o.DeviceNumber)
                    .Size(int.MaxValue)
                    .Aggregations(sa => sa
                        .TopHits(topHitsAggregation, th => th
                            .Size(1)
                            .Sort(x => x.Field(f => f.Modified).Descending())
                        )
                    )
                )
            )
        );

        if (!response.IsValid)
        {
            throw new ElasticsearchException(response.DebugInformation);
        }

        var results = new List<CustomerDeviceModel>();
        var terms = response.Aggs.Terms(termsAggregation);

        foreach (var bucket in terms.Buckets)
        {
            var hit = bucket.TopHits(topHitsAggregation);
            var device = hit.Documents<CustomerDeviceModel>().First();
            results.Add(device);
        }
Run Code Online (Sandbox Code Playgroud)


zxc*_*cvb 3

我猜您已经弄清楚了这一点,但您可以访问嵌套聚合,它只是在基类中,您可以在调试器的 Nest.KeyItem.base.base.Aggregations 中看到它。