How to calc percentage $$$%%%?


(Nativ At) #1

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.


(Shane Connelly) #2

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


(Nativ At) #3

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"
						}
					}
				}
			}
		}
	}
}

(system) #4