Elasticsearch NEST:读取多级深度聚合

Elastic Search NEST: Read Multi level deep Aggregrations

我确实需要一些有关 C# NEST 的帮助,我已经尝试了好几天了:

情况: 我有一个返回多种类型文档的查询,我进行了聚合以列出具有相应 doc_counts 的所有类型。因此,对于即时类型 Ticket,此搜索操作中有 299 张票。

现在我需要获取数据,根据我需要执行以下操作的文档:(https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nested-aggregation-usage.html#_handling_responses_25)

var tags = response.Aggs.Nested("tags");
var tagNames = tags.Terms("tag_names");

这将适用于一级深度聚合,但我的更深一些。

这是 Aggs 子句:

"aggs": {
        "filtered_types": {
            "terms": {
                "field": "_type"
            }
        },
        "all_types": {
            "global": {},
            "aggs": {
                "all_result_types": {
                    "filter": {
                        "bool": {
                            "must": [
                               {
                                   "query_string": {
                                      "default_field": "_all",
                                      "query": "\"test\""
                                   }
                               }
                            ]
                        }
                    },
                    "aggs": {
                        "result_types": {
                            "terms": {
                                "field": "_type"
                            }
                        }
                    }
                } 
           }
        }
    }

我的回复是:

"aggregations": {
      "filtered_types": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "ticket",
               "doc_count": 105
            },
            {
               "key": "iteration",
               "doc_count": 10
            }
         ]
      },
      "all_types": {
         "doc_count": 2516,
         "all_result_types": {
            "doc_count": 193,
            "result_types": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": [
                  {
                     "key": "ticket",
                     "doc_count": 105
                  },
                  {
                     "key": "comment",
                     "doc_count": 67
                  },
                  {
                     "key": "iteration",
                     "doc_count": 10
                  },
                  {
                     "key": "profile",
                     "doc_count": 6
                  },
                  {
                     "key": "project",
                     "doc_count": 3
                  },
                  {
                     "key": "company",
                     "doc_count": 1
                  },
                  {
                     "key": "sla",
                     "doc_count": 1
                  }
               ]
            }
         }
      }
   }

响应是正确的,这正是我所需要的,但是使用 NEST 我似乎无法到达我的数据所在的 "result_types"。

我希望任何人都可以指出获取数据的解决方案。

"result_types" 术语聚合的桶是 "all_result_types" 过滤器聚合的子聚合,它本身是 "all_types" 全局聚合的子聚合。然后,为了得到桶,我们只需要向下遍历响应类型中的聚合

var searchResponse = client.Search<Document>(s => s
    .Aggregations(a => a
        .Terms("filtered_types", t => t
            .Field("_type")
        )
        .Global("all_types", g => g
            .Aggregations(aa => aa
                .Filter("all_result_types", f => f
                    .Filter(ff => ff
                        .Bool(b => b
                            .Must(q => q
                                .QueryString(qs => qs
                                    .DefaultField("_all")
                                    .Query("\"test\"")
                                )
                            )
                        )
                    )
                    .Aggregations(aaa => aaa
                        .Terms("result_types", t => t
                            .Field("_type")
                        )
                    )
                )
            )
        )
    )
);

// get the global aggregation 
var globalAggs = searchResponse.Aggs.Global("all_types");

// get the filter aggregation
var filterAggs = globalAggs.Filter("all_result_types");

// get the terms aggregation
var termsAggs = filterAggs.Terms("result_types");

// do something with the buckets
foreach (var bucket in termsAggs.Buckets)
{
    Console.WriteLine($"key: {bucket.Key}, count: {bucket.DocCount}");
}