Following is the data stored in ElasticSearch:
[
{
"id": 1,
"class": "class 1",
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2011, "score": 80 },
{ "year": 2003, "score": 70 }
]
},
{
"id": 2,
"class": "class 1",
"name": "Gabriel",
"scores": [
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 70 }
]
},
{
"id": 3,
"class": "class 2",
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2021, "score": 100 },
{ "year": 2003, "score": 80 }
]
},
{
"id": 4,
"class": "class 2",
"name": "Pierce",
"scores": [
{ "year": 2022, "score": 70 }
]
}
]
Is there a way in ElasticSearch to merge/combine scores into one array by specific group? (keep the duplicates values)
For example:
- Group by class, it will show the scores of class 1 and class 2, and just keep the class and scores fields, the result would be:
[
{
"class": "class 1",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 80 },
{ "year": 2011, "score": 70 },
{ "year": 2003, "score": 70 }
]
},
{
"class": "class 2",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2022, "score": 70 },
{ "year": 2021, "score": 100 },
{ "year": 2003, "score": 80 }
]
}
]
- Group by name, it will put all the scores of Scott into one array, and keep only name and scores fields:
[
{
"name": "Scott",
"scores": [
{ "year": 2022, "score": 100 },
{ "year": 2022, "score": 100 },
{ "year": 2021, "score": 100 },
{ "year": 2011, "score": 80 },
{ "year": 2003, "score": 80 },
{ "year": 2003, "score": 70 }
]
},
{
"name": "Gabriel",
"scores": [
{ "year": 2015, "score": 90 },
{ "year": 2011, "score": 70 }
]
},
{
"name": "Pierce",
"scores": [
{ "year": 2022, "score": 70 }
]
}
]
Thanks!