I have a type product in elasticsearch that contains a column containing multiple ids, some of them are the same.
There are also columns containing current price and the quantity. I want the to get the sum of price * avg of each unique id.
id price qte
__ _____ _________
1 25 4
1 25 4
1 25 4
2 38 2
2 38 2
3 12 3
3 12 3
3 12 3
3 12 3
4 33 6
5 64 8
5 64 8
(if you're wondering why it's like that, it's cuz there are other columns with different values for each)
So I created my aggregation :
$id = new \Elastica\Aggregation\Terms('id');
$id->setField('id')->setSize(0);
$qte_price = new \Elastica\Aggregation\Sum('qte_price');
$qte_price->setScript('doc["price"].value * doc["qte"].value');
$id->addAggregation($qte_price);
The problem here, is that qte_price
doesn't use the first aggregation on the id
before performing the setScript()
, thus summing the total of price * qte
on all ids even the repeated ones. What am I doing wrong?