我有以下数据集
[
{
"rating": "10",
"subject": "maths"
},
{
"rating": "9",
"subject": "physics"
},
{
"rating": "10",
"subject": "chemistry"
},
{
"rating": "5",
"subject": "physics"
},
{
"rating": "2",
"subject": "geography"
},
{
"rating": "5",
"subject": "maths"
},
{
"rating": "1",
"subject": "geography"
},
{
"rating": "5",
"subject": "maths"
},
{
"rating": "8",
"subject": "chemistry"
}
]
我需要做的是找到每个主题的平均评分,然后通过弹性搜索查询计算评分范围(0-2,2-5,5-8,8-10)中的主题数
我到目前为止的查询为每个主题创建存储桶,以计算每个存储桶的平均值。但是我找不到如何对复合聚合的结果进行范围聚合。可能吗有其他选择吗?
这是我的查询,用于根据主题存储数据并计算平均评级。
GET kibana_sample/_search
{
"size":0,
"aggs" : {
"my_buckets": {
"composite" : {
"sources" : [
{ "subject": { "terms" : { "field": "subject" } } }
]
},
"aggs": {
"avg_rating": {
"avg" : { "field" : "rating" }
}
}
}
}
}
结果如下。
"aggregations": {
"my_buckets": {
"buckets": [
{
"key": {
"subject": "maths"
},
"doc_count": 3,
"avg_rating": {
"value": 6.66666667
}
},
{
"key": {
"subject": "physics"
},
"doc_count": 2,
"avg_rating": {
"value": 7
}
},
{
"key": {
"subject": "chemistry"
},
"doc_count": 2,
"avg_rating": {
"value": 9
}
},
{
"key": {
"subject": "geography"
},
"doc_count": 2,
"avg_rating": {
"value": 1.5
}
}
]
}
}
这一切都很好,但是现在我需要在此结果的基础上进行范围汇总,以得出评分范围内的主题数 例如:
评分范围:{0-2}:1个主题,{2-5}:0个主题,{5-8}:2个主题, {8-10}:1个主题
答案 0 :(得分:0)
您可以使用管道聚合,以便通过更多聚合来合并一个聚合结果。您可以做的另一件事是在管道中使用脚本,以便仅过滤相关结果。
在此处查看脚本示例: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html