guy*_*lot 4 c# elasticsearch nest
我试图在 c# 中运行聚合查询(使用 nest 5),但我不知道我得到了多少聚合作为输入以及聚合类型是什么。
例如一个查询是:{"aggs":{"type_count":{"terms":{"field":"type"}}}}
其他查询将是:{"aggs":{"type_count":{"terms":{"field":"type"}},"salary_count": {"field":"salary"}}}
其他查询可能根本不包含聚合。
如何在 c# 中动态编写此代码?
这就是我尝试过的(我有选择聚合类型的案例。问题是这段代码只支持一种聚合。
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]));
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我成功地使用以下代码添加了多个聚合:
AggregationContainerDescriptor<SearchRequest> agg = new
AggregationContainerDescriptor<SearchRequest>();
agg.Terms("bucket", tm=> tm.Field("field"));
agg &= new AggregationContainerDescriptor<SearchRequest>().Terms("bucket2", tm=> tm.Field("field2"));
Run Code Online (Sandbox Code Playgroud)
谢谢
一般来说,在 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]));
}
Run Code Online (Sandbox Code Playgroud)
只有最后一次调用SearchAgg.Aggregations(...)才会分配。
该书写聚合文档具有发出多个聚集的例子。鉴于以下 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; }
}
Run Code Online (Sandbox Code Playgroud)
whereCommitActivity被映射为一种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))
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
由于搜索请求上的最终聚合只是聚合名称和聚合类型的字典,因此使用此语法可以非常快地增长。出于这个原因,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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2563 次 |
| 最近记录: |