Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ElasticSearch-.NET嵌套API使用对象初始化API构建多个聚合似乎创建了错误的请求_C#_.net_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,C#,.net,elasticsearch" /> elasticsearch,C#,.net,elasticsearch" />

C# ElasticSearch-.NET嵌套API使用对象初始化API构建多个聚合似乎创建了错误的请求

C# ElasticSearch-.NET嵌套API使用对象初始化API构建多个聚合似乎创建了错误的请求,c#,.net,elasticsearch,C#,.net,elasticsearch,我正在动态构建弹性搜索查询的组件,该查询可能包含多个聚合 .Net 4.5.2 Nest 2.3.1 Elasticsearch.Net 2.3.1 通过重复以下结构,我可以成功添加多个聚合: var aggregations = new AggregationDictionary(); aggregations["yyy"] = new AggregationContainer { Terms = new TermsAggregation("xxx") { F

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

.Net 4.5.2 Nest 2.3.1 Elasticsearch.Net 2.3.1

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

 var aggregations = new AggregationDictionary();
 aggregations["yyy"] = new AggregationContainer {
    Terms = new TermsAggregation("xxx")
    {
       Field = "afield"
    }
 };
然后将搜索上的aggregations属性设置为aggregations变量。一切都很好

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

var aggregations=new NestedAggregation("Countries") {
   Path = "MetaData.GeographicCoverage.Countries",
   Aggregations =
      new TermsAggregation("Country") {
         Field = "MetaData.GeographicCoverage.Countries.Country"
      }
 };
再次将搜索上的aggregations属性设置为aggregations变量,一切正常

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

{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
            "path": "MetaData.GeographicCoverage.Countries"
      },
      "aggs": {
        "Country": {
          "terms": {
            "field": "MetaData.GeographicCoverage.Countries.Country"
          }
        }
      }
    }
  }
}
现在,当我组合这些方法,以便添加嵌套聚合时,就像第一个代码段中的那些方法一样:

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将忽略实际的“国家”聚合:

{
  "size": 0,
  "aggs": {
    "Countries": {
      "nested": {
        "path": "MetaData.GeographicCoverage.Countries"
      }
    }
  }
}
那么,这是一个bug还是我使用的嵌套类不正确?如果我不正确地使用这些类,如何修复代码


感谢您的帮助。

解决此问题的方法是将NestedAggregration的显式强制转换添加到AggregationContainer,然后将其直接添加到AggregationDictionary

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;