我正在尝试将以下查询转换为NEST DSL语法:
{
"aggs": {
"color": {
"terms": {
"field": "Color"
}
},
"weight": {
"terms": {
"field": "Weight"
}
}
}
}
每个术语都来自包含名称和fieldId的列表。到目前为止,我已经做到了:
var request2 = _client.Search<T>(s => s
.Aggregations(aggs =>
aggs.Terms("Weight", x => x.Field("Weight")).Terms("Color", x => x.Field("Weight"))));
这可以按预期工作,但是我需要能够动态提供参数Weight
和Color
,因为可以用不同的参数集调用该方法。有没有办法使用类似的东西:
aggs.Terms(x => x.field( myList.foreach(y.value))));
我猜想这与对象初始值设定项语法一起使用会更好,但是我宁愿在dsl中使用它。
答案 0 :(得分:1)
类似以下的方法会起作用
var client = new ElasticClient();
var fields = new List<string>
{
"color",
"weight",
"foo",
"bar"
};
var response = client.Search<object>(s => s
.Aggregations(aggs =>
{
foreach (var field in fields)
{
aggs.Terms(field, t => t.Field(field));
}
return aggs;
})
);
它将生成以下请求
{
"aggs": {
"color": {
"terms": {
"field": "color"
}
},
"weight": {
"terms": {
"field": "weight"
}
},
"foo": {
"terms": {
"field": "foo"
}
},
"bar": {
"terms": {
"field": "bar"
}
}
}
}