簡體   English   中英

ElasticSearch C#client(NEST):訪問嵌套聚合結果

[英]ElasticSearch C# client (NEST): access nested aggregation results

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

我猜你已經想到了這個,但你可以訪問嵌套聚合,它只是在基類中,你可以在調試器的Nest.KeyItem.base.base.Aggregations中看到它。

以下是訪問內部聚合的完整工作示例:

        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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM