【发布时间】: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