gpt4 book ai didi

elasticsearch - 如何获得特定于每一行的总成绩

转载 作者:行者123 更新时间:2023-12-03 02:02:08 29 4
gpt4 key购买 nike

我需要使用Elasticsearch GET查询,通过将每个学生在所有学科中获得的分数加起来来查看每个学生的总分数,而我正在获得每个学科中所有学生的总分数。

GET /testindex/testindex/_search
{
"query" : {
"filtered" : {
"query" : {
"match_all" : {}
}
}
},
"aggs": {
"total": {
"sum": {
"script" : "doc['physics'].value + doc['maths'].value + doc['chemistry'].value"
}
}
}
}

输出量
  {
....
"hits": [
{
"_index": "testindex",
"_type": "testindex",
"_id": "1",
"_score": 1,
"_source": {
"personalDetails": {
"name": "viswa",
"age": "33"
},
"marks": {
"physics": 18,
"maths": 5,
"chemistry": 34
},
"remarks": [
"hard working",
"intelligent"
]
}
},
{
"_index": "testindex",
"_type": "testindex",
"_id": "2",
"_score": 1,
"_source": {
"personalDetails": {
"name": "bob",
"age": "13"
},
"marks": {
"physics": 48,
"maths": 45,
"chemistry": 44
},
"remarks": [
"hard working",
"intelligent"
]
}
}
]
},
"aggregations": {
"total": {
"value": 194
}
}
}

预期产量:

我希望获得每位学生的总成绩,而不是所有学生的总成绩。

为此,我需要在查询中进行哪些更改。

最佳答案

{
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
},
"aggs": {
"student": {
"terms": {
"field": "personalDetails.name",
"size": 10
},
"aggs": {
"total": {
"sum": {
"script": "doc['physics'].value + doc['maths'].value + doc['chemistry'].value"
}
}
}
}
}
}

但是,请注意,对于 student terms聚合,您需要一个“唯一”(使该学生变得独特的东西,例如个人ID或其他内容),也许是 _id本身,但您需要 store

关于elasticsearch - 如何获得特定于每一行的总成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262054/

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