gpt4 book ai didi

elasticsearch - 编写NEST查询以按分数对聚合存储桶进行排序

转载 作者:行者123 更新时间:2023-12-02 23:15:34 25 4
gpt4 key购买 nike

我想要的是为每个unitId(这是我的文档中的一个字段)创建一个聚合存储桶。我希望每个存储桶按该存储桶中的最高得分排序。我写了下面的查询,它可以满足我的要求:

 "aggs": {
"UnitAggregationBucket": {
"terms": {
"field": "unitId",
"size": 10,
"order": {
"max_score": "desc"
}
},
"aggs": {
"max_score": {
"max": {
"script": "_score"
}
}
}
}

我正在使用脚本在子聚合中查找每个存储桶的最高得分。我不知道如何使用NEST编写上述查询?

最佳答案

更新:

这是我从Elastic Community得到的答案:

With 6.x, this would be something like:

var client = new ElasticClient();

var searchResponse = client.Search<object>(s => s
.Aggregations(a => a
.Terms("UnitAggregationBucket", t => t
.Field("unitId")
.Size(10)
.Order(o => o
.Descending("maximum_score")
)
.Aggregations(aa => aa
.Max("maximum_score", m => m
.Script("_score")
)
)
)
)
);

var termsAgg = searchResponse.Aggregations.Terms("UnitAggregationBucket");

foreach(var bucket in termsAgg.Buckets)
{
// do something with buckets
var maxScore = bucket.Max("maximum_score").Value;
}

Note that you can't use max_score for the name of the aggregation as it's a reserved keyword name in the client, which the client uses in its heuristics based aggregation response JSON deserialization method.



原始答案

我设法编写了以下NEST查询:
var unitAggregations = new TermsAggregation("UnitAggregationBucket")
{
Size 10,
Field = Field<MyDocument>(p => p.UnitId),
Order = new List<TermsOrder>
{
new TermsOrder()
{
Key = "max_score_in_bucket",
Order = SortOrder.Descending
}
},
Aggregations = new MaxAggregation("max_score_in_bucket", string.Empty)
{
Script = new InlineScript("_score")
}
};

产生以下json:
"aggs": {
"UnitAggregationBucket": {
"aggs": {
"max_score_in_bucket": {
"max": {
"script": {
"source": "_score"
}
}
}
},
"terms": {
"field": "unitId",
"order": [
{
"max_score_in_bucket": "desc"
}
],
"size": 10
}
}
}

它不是我想要的确切json,但它可以满足我的要求。

注意: max_score是Elasticsearch中的保留关键字,因此我不得不使用其他名称: max_score_in_bucket

关于elasticsearch - 编写NEST查询以按分数对聚合存储桶进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970379/

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