I have data indexed as follows:
{"index":{"_id":1}}
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"born":"1993/08/13"}
{"index":{"_id":2}}
{"first":"sean","last":"monohan","goals":[7,54,26],"born":"1994/10/12"}
What I want is to calculate the total goals for each player, so I use scripted fields as follows:
GET hockey/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_goals": {
"script": {
"lang": "painless",
"source": """
int total = 0;
for (int i = 0; i < doc['goals'].length; ++i) {
total += doc['goals'][i];
}
return total;
"""
}
}
}
}
the result:
"hits" : [
{
"_index" : "hockey",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"total_goals" : [
37
]
}
},
{
"_index" : "hockey",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"fields" : {
"total_goals" : [
87
]
}
}
This works fine, but in the result I have "total_goals" for each player, but not the first name and last name. How do I include the first and last name in the results ?