gpt4 book ai didi

c# - 如何获取Elasticsearch NEST响应的聚合类属性?

转载 作者:行者123 更新时间:2023-12-03 01:30:11 25 4
gpt4 key购买 nike

运行此命令时:

searchResponse = _elasticClient.Search<Assets>(s => s
.Aggregations(a => a
.Filter("myFilter", f => f
.Filter(fi => fi
.Bool(b => b
.Must(
m => m.Term(t => t.TitleType, type),
m => m.Term(t => t.Categories, category))))
.Aggregations(aggs => aggs
.Terms("myUnique", te => te
.Field(f1 => f1.Genres))))));

我得到这个作为回应:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 50751,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"myFilter" : {
"doc_count" : 50281,
"myUnique" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Other",
"doc_count" : 9879
},
{
"key" : "Anime",
"doc_count" : 5948
},
{
"key" : "Reality",
"doc_count" : 4354
},
{
"key" : "Drama",
"doc_count" : 3333
}
]
}
}
}
}

这正是我想要的。但是,我不知道如何在响应中访问类属性。我认为这样的事情会起作用:
agg = searchResponse.Aggregations.Terms("myFilter.myUnique"); // Does not work!

我想在上面找到的汇总响应中找到所有“键”,例如戏剧,现实等

我的目标是在上面的代码行之后运行此代码:
foreach (var genre in agg.Buckets)
{
var value = genre.Key;
var count = genre.DocCount;

DoSomething(value, count);

}

我敢肯定有办法,但是在网上找不到任何例子。有人做过类似的事情吗?

最佳答案

我完全错过了docs中的一些重要内容:

var filterAgg = searchResponse.Aggregations.Filter("myFilter");

var terms = filterAgg.Terms("myUnique");

然后我可以做:

foreach (var genre in terms.Buckets)
{
var value = genre.Key;
var count = genre.DocCount;

DoSomething(value, count);
}

关于c# - 如何获取Elasticsearch NEST响应的聚合类属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427571/

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