I need to calc percentage of 2 aggs fields.

example:

sum(maple_tree) / sum(all_types_of_trees);

result:

maple tree percent % from all all types of trees.

P.S.

I prefer to do the calc on the query itself and not in the code.

I need to calc percentage of 2 aggs fields.

example:

sum(maple_tree) / sum(all_types_of_trees);

result:

maple tree percent % from all all types of trees.

P.S.

I prefer to do the calc on the query itself and not in the code.

Have you looked at pipeline aggregations? Specifically, the bucket script aggregation has an example that calculates a percentage

Yes, but I need to calc percentage from the "Total" of 2 fields, and not per bucket computations.

For example: get the the "total" sales percentage of red cars.

```
GET /sales/_search
{
"size": 0,
"aggs": {
"sales_per_month": {
"date_histogram": {
"field": "date",
"interval": "month"
},
"aggs": {
"red_cars": {
"sum": {
"field": "red"
},
"sold_red_card": {
"value_count": {
"field": "sold"
}
}
}
}
}
}
}
```

© 2020. All Rights Reserved - Elasticsearch

- Elasticsearch is a trademark of Elasticsearch BV, registered in the U.S. and in other countries
- Trademarks
- Terms
- Privacy
- Brand
- Code of Conduct

Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant logo are trademarks of the Apache Software Foundation in the United States and/or other countries.