gpt4 book ai didi

c# - ElasticSearch 过滤器聚合

转载 作者:行者123 更新时间:2023-11-30 17:43:19 27 4
gpt4 key购买 nike

我需要返回每个 parent 的 child 数量。这是我的解决方案:

foreach (var person in someList)
{
var countingFields = _elasticsearchClient.Search<SomeModel>(esModel=> esModel
.Aggregations(aggregation => aggregation
.Filter("Parents", filter => filter
.Filter(innerFilter => innerFilter
.Term(field => field.ParentId, person.Id))
.Aggregations(innerAggregation => innerAggregation
.ValueCount("Counting", count => count
.Field(field => field.ParentId))))));
}

我需要帮助来改进这一点,我想通过与 ElasticSearch 的唯一连接获得相同的数据。

最佳答案

您可以将 ValueCount 替换为 terms 聚合。所以你会得到如下结果:

ParentId Count
1 4
2 3

我的测试数据集:

client.Index(new SomeModel {Id = 1, ParentId = 1});
client.Index(new SomeModel {Id = 2, ParentId = 2});
client.Index(new SomeModel {Id = 3, ParentId = 3});
client.Index(new SomeModel {Id = 4, ParentId = 1});

NEST 术语聚合语法:

var someList = new List<int>{1,2,3,4};

var countingFields = client.Search<SomeModel>(esModel => esModel
.Aggregations(aggregation => aggregation
.Filter("Parents", filter => filter
.Filter(innerFilter => innerFilter
.Terms(field => field.ParentId, someList))
.Aggregations(innerAggregation => innerAggregation
.Terms("Counting", count => count
.Field(field => field.ParentId))))));

响应:

"aggregations": {
"Parents": {
"doc_count": 4,
"Counting": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 2
},
{
"key": 2,
"doc_count": 1
},
{
"key": 3,
"doc_count": 1
}
]
}
}
}

希望对你有帮助。

关于c# - ElasticSearch 过滤器聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31138170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com