我试图在 c# 中运行聚合查询(使用 nest 5)但是 我不知道我得到了多少聚合作为输入以及聚合类型是什么。
例如一个查询是: {"aggs":{"type_count":{"terms":{"field":"type"}}}}
其他查询将是: {"aggs":{"type_count":{"terms":{"field":"type"}},"salary_count": {“字段”:“薪水”}}}
其他查询可能根本不包含聚合。
如何在 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]));
}
编辑:
我使用此代码成功添加了多个聚合:
AggregationContainerDescriptor<SearchRequest> agg = new
AggregationContainerDescriptor<SearchRequest>();
agg.Terms("bucket", tm=> tm.Field("field"));
agg &= new AggregationContainerDescriptor<SearchRequest>().Terms("bucket2", tm=> tm.Field("field2"));
谢谢
一般来说,在 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(...)
才会分配。
编写聚合文档有发布多个聚合的示例。给定以下 POCOs
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);
我知道问这个问题已经有几年了,但我遇到了同样的问题。
使用 NEST 7.17,我想根据用户的选择动态创建多层聚合查询。 Fluent 样式查询非常适合固定查询,但我一直在努力以这种方式动态添加多个聚合级别。我最终逐位创建查询,示例如下所示。
这 比 Fluent 风格冗长很多;但我最终将这里的代码包装在添加桶聚合和值聚合的每一层的方法中,允许我随意动态创建复杂的多层查询。
最初让我感到困惑的一件事是 AggregationDictionary() 构造函数复制了所有细节,因此请确保在其他细节完全组装后最后调用它们。
/*
Index for sample will be:
country - keyword
region - keyword
price - float
*/
// Level 1 - Break out 'Country' into buckets
var aggDictL1 = new Dictionary<string, IAggregationContainer>();
var terms = new TermsAggregation("country_level");
terms.Size = 100;
terms.Field = new Field("country.keyword");
aggDictL1["country_level"] = new AggregationContainer { Terms = terms };
// Total cost for each country
aggDictL1["total_cost"] = new AggregationContainer
{
Sum = new SumAggregation("total_cost", new Field("price"))
};
// Level 2 - Break out 'Region' within each country
var aggDictL2 = new Dictionary<string, IAggregationContainer>();
var terms2 = new TermsAggregation("region_level");
terms2.Size = 100;
terms2.Field = new Field("region.keyword");
aggDictL2["region_level"] = new AggregationContainer { Terms = terms2 };
// Total cost for each bucket within layer above ('Country' in this case)
aggDictL2["country_cost"] = new AggregationContainer
{
Sum = new SumAggregation("country_cost", new Field("price"))
};
// Level 3 - Total cost for each region
var aggDictL3 = new Dictionary<string, IAggregationContainer>();
// Total cost for each bucket within layer above ('Region' in this case)
aggDictL3["region_cost"] = new AggregationContainer
{
Sum = new SumAggregation("region_cost", new Field("price"))
};
// Assemble layers - do this last as AggregationDictionary() will copy details
aggDictL2["region_level"].Aggregations = new AggregationDictionary(aggDictL3);
aggDictL1["country_level"].Aggregations = new AggregationDictionary(aggDictL2);
// Assemble request
var sd = new SearchRequest<SpotRecord>(Indices.Index(indexName));
sd.Size = 0;
sd.Query = <your filter here...>
sd.Aggregations = new AggregationDictionary(aggDictL1);
// Run it
var ret = _client.Search<SpotRecord>(sd);