I have following document list in ElasticSearch (where scores are nested):
{
'type': 'typeA',
'scores': [
{'type': 'A', 'val': 45},
{'type': 'A', 'val': 55},
{'type': 'B', 'val': 65},
]
},
{
'type': 'typeA',
'scores': [
{'type': 'A', 'val': 55},
{'type': 'A', 'val': 50},
{'type': 'A', 'val': 57},
]
},
{
'type': 'typeB',
'scores': [
{'type': 'B', 'val': 40},
{'type': 'A', 'val': 50},
{'type': 'A', 'val': 60},
]
}
Is it possible to have a query that returns average scores per type, but only if scores.type is "A"?
Explanation (if I did it manually):
1) filter only "A" scores (simplified):
{'type': 'typeA', 'scores': [45, 55]},
{'type': 'typeA', 'scores': [55, 50, 57]},
{'type': 'typeB', 'scores': [50, 60]},
2) find AVG per document:
{'type': 'typeA', 'avg': 50}, // (45+55) / 2
{'type': 'typeA', 'avg': 54}, // (55+50+57) / 3
{'type': 'typeB', 'avg': 55}, // (50 + 60) / 2
3) Final aggregation per type:
'typeA' : 52, // (50+54) / 2
'typeB': 55, // (55) / 1
Is it possible or I should stick to client side for this?