我试图在c#中运行聚合查询(使用嵌套5),但是 我不知道我得到多少聚合作为输入,什么是聚合类型。
例如,一个查询是: { “AGGS”:{ “type_count”:{ “术语”:{ “字段”: “类型”}}}}
其他查询将是: { “AGGS”:{ “type_count”:{ “术语”:{ “字段”: “类型”}}, “salary_count”: { “字段”: “工资”}}}
其他查询可能根本不包含聚合。
如何在c#中编写动态代码?
这就是我尝试过的(我对选定的聚合类型有所了解。 问题是这段代码只支持一个聚合。
AggregationContainerDescriptor<SearchRequest> agg = new
AggregationContainerDescriptor<SearchRequest>();
agg.Terms("bucket", tm=> tm.Field("field"));
agg &= new AggregationContainerDescriptor<SearchRequest>().Terms("bucket2", tm=> tm.Field("field2"));
编辑:
我使用此代码成功添加了多个聚合:
johnsmith@gmail.com
由于
答案 0 :(得分:3)
一般来说,在NEST中使用Fluent lambda表达式语法的方法调用执行赋值而不是相加,这意味着相同方法的连续调用将覆盖分配的内容。在你的例子中
SearchDescriptor<object> SearchAgg = new SearchDescriptor<object>();
for (i=0;i < aggList.length;i++)
{
SearchAgg.Aggregations(a => a.terms (aggList[i]), t=> t.Field(aggList[i]));
}
只有最后一次拨打SearchAgg.Aggregations(...)
才能分配。
writing aggregations documentation包含发布多个聚合的示例。鉴于以下POCO
public class Project
{
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartedOn { get; set; }
public DateTime LastActivity { get; set; }
public IList<string> Tags { get; set; }
public IList<string> Branches { get; set; }
public IList<CommitActivity> Commits { get; set; }
}
public class CommitActivity
{
public string Id { get; set; }
public string Message { get; set; }
public long SizeInBytes { get; set; }
}
其中CommitActivity
被映射为nested
类型,在提交时发布两个带有嵌套聚合的聚合聚合,以聚合有关每个项目的提交的统计信息
var searchResponse = client.Search<Project>(s => s
.Aggregations(aggs => aggs
.Terms("project_tags", t => t.Field(p => p.Tags))
.Terms("project_branches", t => t.Field(p => p.Branches))
.Nested("commits", n => n
.Path(p => p.Commits)
.Aggregations(aa => aa
.Stats("commit_size_stats", m => m.Field(p => p.Commits.First().SizeInBytes))
)
)
)
);
var searchRequest = new SearchRequest<Project>
{
Aggregations = new AggregationDictionary
{
{ "project_tags", new TermsAggregation("project_tags") { Field = Nest.Infer.Field<Project>(p => p.Tags) } },
{ "project_branches", new TermsAggregation("project_branches") { Field = Nest.Infer.Field<Project>(p => p.Branches) } },
{ "commits", new NestedAggregation("commits")
{
Path = Nest.Infer.Field<Project>(p => p.Commits),
Aggregations = new AggregationDictionary
{
{ "commit_size_stats", new StatsAggregation("commit_size_stats", Nest.Infer.Field<Project>(p => p.Commits.First().SizeInBytes)) },
}
}
}
}
};
var searchResponse = client.Search<Project>(searchRequest);
由于搜索请求上的聚合最终只是聚合名称和聚合类型的字典,因此使用此语法可以非常快速地增长。因此,NEST重载逻辑&&
运算符并实现隐式转换,以允许以更简洁的方式组合聚合
var searchRequest = new SearchRequest<Project>
{
Aggregations =
new TermsAggregation("project_tags") { Field = Nest.Infer.Field<Project>(p => p.Tags) } &&
new TermsAggregation("project_branches") { Field = Nest.Infer.Field<Project>(p => p.Branches) } &&
new NestedAggregation("commits")
{
Path = Nest.Infer.Field<Project>(p => p.Commits),
Aggregations =
new StatsAggregation("commit_size_stats", Nest.Infer.Field<Project>(p => p.Commits.First().SizeInBytes))
}
};
var searchResponse = client.Search<Project>(searchRequest);