使用NEST我有一个看起来像这样的请求
var searchRequest = new SearchRequest<T>()
{
...
Aggregations = new TermsAggregation("field1")
{
Field = "field1",
Aggregations = new TermsAggregation("field2")
{
Field = "field2",
}
},
...
}
await _elasticClient.SearchAsync<T>(searchRequest)
我得到的结果与我预期的一样,“field1”上的每个替代项都在“field2”上进行了汇总
我需要其他聚合,因此将术语“聚合”放在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:将密钥添加到for 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/