c# - ElasticSearch C# 客户端 (NEST) : access nested aggregation results

标签 c# elasticsearch aggregation

我在 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”部分。当我调试时,我看不到任何访问数据的方法。

enter image description here

最佳答案

官方文档和此处的答案都不能完全适用于 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);
        }

关于c# - ElasticSearch C# 客户端 (NEST) : access nested aggregation results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28096723/

相关文章:

c# - 为什么我在 Windows 10 UWP 上收到 "package could not be registered"部署错误?

c# - 在 C# 中拆分字符串值

elasticsearch - 如何配置Elasticsearch Marvel将其数据发送到其他集群

elasticsearch - Elasticsearch 使用范围聚合,基于术语聚合结果

java - 组合和聚合关系 UML 存在问题

elasticsearch - Elasticsearch:在搜索查询期间生成聚合字段

c# 将http请求发送到azure存储

c# - 每 0.1 毫秒和 0.3 毫秒重复两个任务(Systems.Timer.Timer 不够精确)

elasticsearch - 并发文件解析并插入到 Elastic Search 中

Elasticsearch 查询显示奇怪的行为 : bug?