无法在嵌套中选择子聚合

rm5edbpk  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(305)

我在弹性查询中得到以下结果:

"Results" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "73c47133-8656-45e7-9499-14f52df07b70",
          "doc_count" : 1,
          "foo" : {
            "doc_count" : 40,
            "bar" : {
              "doc_count" : 1,
              "customscore" : {
                "value" : 10.496919917864476
              }
            }
          }
        }
      ]

我正试图得到一个匿名对象的列表 key 字段作为键 customscore 字段作为值。
无论我尝试什么,我似乎都无法在访问 customscore 价值观。显然,我是世界上第一个将嵌套聚合与嵌套库结合使用的人。不是这样,就是文件资料非常缺乏。我很容易够到水桶:

response?.Aggregations.Terms("Results").Buckets;

但我不知道该怎么处理这个东西。 Buckets 包含多个对象,我假设可以通过执行以下操作来导航这些对象:

bucketObject["foo"]["bar"]["customscore"]

但显然不是。我找到了用于循环的解决方案,使用长linq查询的解决方案,而且所有这些解决方案似乎都会返回 null 为了我。我错过了什么?

k97glaaz

k97glaaz1#

假设下面的查询与问题中的回答相匹配

var client = new ElasticClient();

var response = client.Search<object>(s => s
    .Index("some_index")
    .Aggregations(a => a
        .Terms("Results", t => t
            .Field("some_field")
            .Aggregations(aa => aa  
                .Filter("foo", f => f
                    .Filter(q => q.MatchAll())
                    .Aggregations(aaa => aaa
                        .Filter("bar", ff => ff
                            .Filter(q => q.MatchAll())
                            .Aggregations(aaaa => aaaa
                                .ValueCount("customscore", vc => vc
                                    .Field("some_other_field")
                                )
                            )
                        )
                    )
                )
            )
        )
    )
);

获取匿名类型的集合

var kvs = response.Aggregations.Terms("Results").Buckets
    .Select(b => new 
    { 
        key = b.Key, 
        value = b.Filter("foo").Filter("bar").ValueCount("customscore").Value 
    });
``` `.Aggregations` 公开转换 `IAggregate` 对预期类型的响应

相关问题