I have this mock data which I want grouped by name then have a total field that is the sum of the value fields only if the status is won.
[{
name: 'Foo',
value: 12,
status: 'won'
},
{
name: 'Foo',
value: 2,
status: 'lost'
},
{
name: 'Foo',
value: 10,
status: 'won'
},
{
name: 'Bar',
value: 4,
status: 'won'
}]
I am able to group by name and obtain the total sum of the value fields but have not figured out how to only sum the won cases.
aggs: {
by_name: {
terms: {
field: 'name'
},
aggs: {
total_value: {
sum: {
field: 'value' // What I want is value if status == 'won'
}
}
}
}
My desired result should look like:
[{
name: 'Foo',
total_value: 22 // Currently 24
}, {
name: 'Bar',
total_value: 4
}]
This seems like a common used case but while I have found lots of info on filtering but not this particular case.