【问题标题】:ElasticSearch - .NET NEST API building multiple aggregrations with object initialization API seems to create an incorrect requestElasticSearch - 使用对象初始化 API 构建多个聚合的 .NET NEST API 似乎会创建不正确的请求
【发布时间】:2016-05-12 20:48:50
【问题描述】:

我正在动态构建弹性搜索查询的组件,该查询可能包含多个聚合。

.Net 4.5.2 嵌套 2.3.1 Elasticsearch.Net 2.3.1

我可以通过重复以下结构成功添加多个聚合:

 var aggregations = new AggregationDictionary();
 aggregations["yyy"] = new AggregationContainer {
    Terms = new TermsAggregation("xxx")
    {
       Field = "afield"
    }
 };

然后将搜索的 Aggregrations 属性设置为聚合变量。一切都很好。

我可以成功创建单个嵌套聚合,如下所示:

var aggregations=new NestedAggregation("Countries") {
   Path = "MetaData.GeographicCoverage.Countries",
   Aggregations =
      new TermsAggregation("Country") {
         Field = "MetaData.GeographicCoverage.Countries.Country"
      }
 };

再次将搜索中的 Aggregrations 属性设置为聚合变量,一切都很好。

当我将这两种方法结合起来创建一个包含许多聚合的查询时,问题就出现了,其中一个(或多个)是嵌套的。所以上面嵌套示例生成的Json看起来像:

{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
            "path": "MetaData.GeographicCoverage.Countries"
      },
      "aggs": {
        "Country": {
          "terms": {
            "field": "MetaData.GeographicCoverage.Countries.Country"
          }
        }
      }
    }
  }
}

现在,当我组合这些方法以便添加嵌套聚合时,就像在第一个代码 sn-p 中一样:

var aggregations = new AggregationDictionary();

var nested = new NestedAggregation("Countries") {
   Path = "MetaData.GeographicCoverage.Countries",
   Aggregations =
      new TermsAggregation("Country") {
         Field = "MetaData.GeographicCoverage.Countries.Country"
      }
   };

aggregations["Countries"] = new AggregationContainer {
   Nested = nested
};

那么生成的查询的Json错过了实际的“Country”聚合:

{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
        "path": "MetaData.GeographicCoverage.Countries"
      }
    }
  }
}

那么 - 这是一个错误还是我错误地使用了 Nest 类?如果我不正确地使用类,我该如何修复代码?

感谢您的帮助。

【问题讨论】:

    标签: c# .net elasticsearch


    【解决方案1】:

    原来解决问题的方法是将 NestedAggregration 的显式转换添加到 AggregationContainer,然后将其直接添加到 AggregationsDictionary。

    var aggregations = new AggregationDictionary();
    var nested =
       new NestedAggregation("Countries") {
          Path = "MetaData.GeographicCoverage.Countries",
          Aggregations =
             new TermsAggregation("Country") {
                Field = "MetaData.GeographicCoverage.Countries.Country"
             }
           };
    
     aggregations["Countries"] = (AggregationContainer)nested;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2023-03-03
      • 1970-01-01
      • 2023-03-31
      • 2015-08-06
      • 1970-01-01
      • 2019-10-07
      相关资源
      最近更新 更多