Hi @francieliton_araujo,
You can go to dev tools and perform below poc:
Create an Index
PUT calc-avg
{
"mappings": {
"properties": {
"location": {
"type": "keyword"
},
"quantity": {
"type": "long"
}
}
}
}
Insert documents
POST calc-avg/_doc
{
"location": "A",
"quantity": 20
}
POST calc-avg/_doc
{
"location": "B",
"quantity": 35
}
POST calc-avg/_doc
{
"location": "C",
"quantity": 32
}
POST calc-avg/_doc
{
"location": "B",
"quantity": 15
}
POST calc-avg/_doc
{
"location": "A",
"quantity": 40
}
POST calc-avg/_doc
{
"location": "C",
"quantity": 8
}
Calculate avg with sum using pipeline aggregation
GET calc-avg/_search?size=0
{
"aggs": {
"avg_location": {
"avg": {
"field": "quantity"
}
}
}
}
GET calc-avg/_search?size=0
{
"aggs": {
"terms_location": {
"terms": {
"field": "location"
},
"aggs": {
"avg_location": {
"avg": {
"field": "quantity"
}
}
}
},
"total": {
"sum_bucket": {
"buckets_path": "terms_location>avg_location"
}
}
}
}
Response
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"terms_location": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "A",
"doc_count": 2,
"avg_location": {
"value": 30
}
},
{
"key": "B",
"doc_count": 2,
"avg_location": {
"value": 25
}
},
{
"key": "C",
"doc_count": 2,
"avg_location": {
"value": 20
}
}
]
},
"total": {
"value": 75
}
}
}