结合流利和对象初始化语法

Combining fluent and object initializer syntax

使用 .NET 客户端 NEST,是否可以组合两种语法,如下所示,其中查询用一种语法编写,聚合用另一种语法编写?

var request = new SearchRequest();
request.Query = new MatchAllQuery();
request.Aggregations = new AggregationContainerDescriptor<Car>().Terms("color", x => x.Field(doc => doc.Color));
_elasticClient.Search<Car>(request);

此处的编译错误是 AggregationContainerDescriptor 无法攻击 AggregationDictionary

您可以将描述符转换为 IAggregationContainer 并从那里得到 Aggregations

var request = new SearchRequest();
request.Query = new MatchAllQuery();
var aggregationContainer = (IAggregationContainer)new AggregationContainerDescriptor<Car>().Terms("color", x => x.Field(doc => doc.Color));
request.Aggregations = aggregationContainer.Aggregations;
var searchResponse = _elasticClient.Search<Car>(request);

希望对您有所帮助。