c# - 带有附加 TermAggregation 的 TermAggregation 在 AggregationDictionary 中不起作用

标签 c# elasticsearch nest

使用 NEST 我有一个看起来像这样的请求

var searchRequest = new SearchRequest<T>()
{
...
    Aggregations = new TermsAggregation("field1")
    {
        Field = "field1",
        Aggregations = new TermsAggregation("field2")
        {
            Field = "field2",
        }
    },
...
}

await _elasticClient.SearchAsync<T>(searchRequest)

我得到了我预期的结果,“field2”上的聚合为“field1”下的每个替代项

我想要其他聚合,所以我将术语聚合放在 AggregationDictionary 中。
var aggs = new AggregationDictionary();
aggs.Add("field1", new TermsAggregation("field1")
    {
        Field = "field1",
        Aggregations = new TermsAggregation("field2")
        {
            Field = "field2",
        }
    })
...
var searchRequest = new SearchRequest<T>()
{
...
    Aggregations = aggs,
...
}
...
await _elasticClient.SearchAsync<T>(searchRequest)

现在我在“field1”上得到聚合,但在“field2”上没有子聚合。

我错过了什么还是应该以这种方式工作?

编辑 1:为 AggregationDictionary.Add 添加了键,以便我的示例代码(希望)编译并包含我的 SearchAsync 调用

最佳答案

aggs.Add( new TermsAggregation("field1")
    {
        Field = "field1",
        Aggregations = new TermsAggregation("field2")
        {
            Field = "field2",
        }
    })

此代码不会编译为 AggregationDictionary.Add() method signature

public void Add(string key, AggregationContainer value)

以下

var client = new ElasticClient();

var aggs = new AggregationDictionary();
aggs.Add("field1", new TermsAggregation("field1")
{
    Field = "field1",
    Aggregations = new TermsAggregation("field2")
    {
        Field = "field2",
    }
});

var request = new SearchRequest<object>
{
    Aggregations =aggs
};

var searchResponse = client.Search<object>(request);

发出以下请求
{
  "aggs": {
    "field1": {
      "aggs": {
        "field2": {
          "terms": {
            "field": "field2"
          }
        }
      },
      "terms": {
        "field": "field1"
      }
    }
  }
}

看看documentation on Writing Aggregations .聚合可以通过 && 在同一级别组合在一起将它们放在一起

关于c# - 带有附加 TermAggregation 的 TermAggregation 在 AggregationDictionary 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61227106/

相关文章:

c# - 在 Blazor 组件中使用 @ref 时为 "Cannot convert null literal to non-nullable reference type"

c# - 使用 CSharpCodeProvider 编译多个源

c# - 通过MVC 5 Controller 获取Azure中的客户端IP

elasticsearch - 流利的位无法解析kubernetes日志

geolocation - ElasticSearch:指定用于存储数据的分片

elasticsearch - 更新 elasticsearch 中的 search.max_buckets

sorting - elasticsearch 2.4:使用滚动条检索满足所有搜索条件的所有记录

datetime - Elasticsearch 2.0 Nest 2.0日期时间月份搜索

elasticsearch - ElasticSearch NEST 5.6.1查询单元测试

c# - 如何从该图像中删除随机生成的线和点? [Tesseract的预OCR]