Elasticsearch嵌套动态聚合

qyswt5oh  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(2)|浏览(127)

我试图在c#中运行聚合查询(使用nest 5),但我不知道有多少聚合作为输入,聚合类型是什么。
例如,一个查询为:{“合计”:{“类型计数”:{“术语”:{“字段”:“类型”}}}
其他查询将是:{“合计”:{“类型_计数”:{“条款”:{“字段”:“类型”}},“薪金_计数”:{“字段”:“薪资”}}}
而其他查询可能根本不包含聚集。
我怎样用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"));

谢谢

jc3wubiy

jc3wubiy1#

一般来说,在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有发出多个聚合的示例。

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被Map为nested类型,发出两个术语聚合,其中在提交上具有嵌套聚合,以聚合关于每个项目的提交的统计信息

使用流畅的lambda表达式语法

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重载逻辑&&运算符并实现隐式转换,以便以更简洁的方式组合聚合

Terse对象初始值设定项语法

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);
kqhtkvqz

kqhtkvqz2#

我知道这个问题已经提出好几年了,但我也有同样的问题。
使用NEST 7.17,我想根据用户选择动态创建多层聚合查询。Fluent样式的查询非常适合固定查询,但我很难用这种方式动态添加多个聚合级别。我最终逐位创建了查询,示例如下所示。
这比Fluent样式要冗长得多;但我最终将代码 Package 在方法中,以添加每一层的桶聚合和值聚合,从而允许我随意动态创建复杂的多层查询。
最初让我感到困惑的一件事是,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);

相关问题