Elasticsearch 来自数组的多个聚合
Elasticsearch multiple aggregations from array
如何在一个查询中动态组合多个聚合?
我知道怎么做"hard-coded":
request.Aggregations = new TermsAggregation("agg_field1") {
Field = "field1"
}
&& new TermsAggregation("agg_field2") {
Field = "field2"
};
但是因为我使用了一个包含字段的数组,所以我需要如下内容。但是我收到编译器错误,因为 'AggregationDictionary' 和 'TermsAggregation' 不能由运算符 '&=':
组合
string[] Fields = new[]{ "field1", "field2" }
foreach (string sField in Fields)
{
request.Aggregations &= new TermsAggregation("agg_" + sField)
{
Field = sField
};
}
我的文章数据包含 "product"、"brand"、"modell" 等字段。
对于这个字段,我想通过聚合获得每个不同的值。它用于显示三个组合框,每个字段都有可能的值。
您可以通过几种不同的方式来构建聚合集合。最接近您要执行的操作的是
var request = new SearchRequest<object>();
string[] Fields = new[] { "field1", "field2" };
AggregationBase aggregations = null;
foreach (string sField in Fields)
{
var termsAggregation = new TermsAggregation("agg_" + sField)
{
Field = sField
};
if (aggregations == null)
{
aggregations = termsAggregation;
}
else
{
aggregations &= termsAggregation;
}
}
request.Aggregations = aggregations;
client.Search<object>(request);
构建以下查询
{
"aggs": {
"agg_field1": {
"terms": {
"field": "field1"
}
},
"agg_field2": {
"terms": {
"field": "field2"
}
}
}
}
如何在一个查询中动态组合多个聚合?
我知道怎么做"hard-coded":
request.Aggregations = new TermsAggregation("agg_field1") {
Field = "field1"
}
&& new TermsAggregation("agg_field2") {
Field = "field2"
};
但是因为我使用了一个包含字段的数组,所以我需要如下内容。但是我收到编译器错误,因为 'AggregationDictionary' 和 'TermsAggregation' 不能由运算符 '&=':
组合string[] Fields = new[]{ "field1", "field2" }
foreach (string sField in Fields)
{
request.Aggregations &= new TermsAggregation("agg_" + sField)
{
Field = sField
};
}
我的文章数据包含 "product"、"brand"、"modell" 等字段。 对于这个字段,我想通过聚合获得每个不同的值。它用于显示三个组合框,每个字段都有可能的值。
您可以通过几种不同的方式来构建聚合集合。最接近您要执行的操作的是
var request = new SearchRequest<object>();
string[] Fields = new[] { "field1", "field2" };
AggregationBase aggregations = null;
foreach (string sField in Fields)
{
var termsAggregation = new TermsAggregation("agg_" + sField)
{
Field = sField
};
if (aggregations == null)
{
aggregations = termsAggregation;
}
else
{
aggregations &= termsAggregation;
}
}
request.Aggregations = aggregations;
client.Search<object>(request);
构建以下查询
{
"aggs": {
"agg_field1": {
"terms": {
"field": "field1"
}
},
"agg_field2": {
"terms": {
"field": "field2"
}
}
}
}