ElasticSearch C#客户端(NEST):访问嵌套聚合结果

5

我在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))
                        )
                    )                        
                )
                );

这将正确生成以下查询:
{
  "size": 0,
  "aggs": {
    "incidentID": {
      "terms": {
        "field": "incidentID",
        "size": 5
      },
      "aggs": {
        "authDateStats": {
          "stats": {
            "field": "authEventDate"
          }
        }
      }
    }
  }
}

这给我带来了以下结果:
"aggregations" : {
    "incidentID" : {
        "buckets" : [{
                "key" : "0A631EB1-01EF-DC28-9503-FC28FE695C6D",
                "doc_count" : 233,
                "authDateStats" : {
                    "count" : 233,
                    "min" : 1401167036075,
                    "max" : 1401168969907,
                    "avg" : 1401167885682.6782,
                    "sum" : 326472117364064
                }
            }
        ]
    }
}

我无法弄清楚如何访问“authDateStats”部分。当我调试时,我看不到任何访问数据的方式。

以下是使用Nest进行嵌套聚合的示例。 https://nest.azurewebsites.net/nest/aggregations/nested.html。 - Purushoth
3个回答

6

官方文档和这里的回答并不能完全适用于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);
        }

3

我猜你已经发现了,你可以访问嵌套聚合,它只是在一个基类中,你可以在调试器中查看 Nest.KeyItem.base.base.Aggregations。


1
这是一个完整的工作示例,用于访问内部聚合:
        const string aggName = "LocationIDAgg";
        const string aggNameTopHits = "LatestForLoc";
        var response = await ElasticClient.SearchAsync<PlacementVerificationES>(s => s
            .Query(BuildQuery(filter, null))                
            .Size(int.MaxValue)
            .Aggregations(a=> a
                .Terms(aggName, t=> t
                    .Field(f=>f.LocationID)
                    .Size(100)
                    .Aggregations(innerAgg => innerAgg
                        .TopHits(aggNameTopHits, th=> th
                            .Size(1)
                            .Sort(x=>x.OnField(f=> f.Date).Descending())
                        )
                    )
                )
            )
        ).VerifySuccessfulResponse();

        //var debug = response.GetRequestString();
        var agBucket = (Bucket)response.Aggregations[aggName];

        var output = new List<PlacementVerificationForReporting>();
        // ReSharper disable once LoopCanBeConvertedToQuery
        // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
        foreach (KeyItem i in agBucket.Items)
        {
            var topHits = (TopHitsMetric)i.Aggregations[aggNameTopHits];
            var top1 = topHits.Hits<PlacementVerificationES>().Single();
            var reportingObject = RepoToReporting(top1);
            output.Add(reportingObject);
        }

        return output;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接