elasticsearch
elasticsearch copied to clipboard
what about aggregations
This repository looks really great. Is there also a solution for aggregation metrics?
regards Ppeer
+1
any updates on this?
The problem seems to be in \src\Query.php in the getAll() functions, that only takes $result["hits"]["hits"] and ignores "aggregations".
A workaround is to use the the elasticsearch-php basic functionality: $client = $this->elasticsearch->connection; $client ->search([params]).
But then there is no point in using a different library ...
Is there any chance to fix this issue soon?
+1
Yes, you can.
$aggregation = [
'aggs' => [
'hat_prices' => ['sum' => ['field' => 'price']],
],
];
$data = Audience::body($aggregation)->response();
dd($data);
Will return :
array:5 [▼
"took" => 40
"timed_out" => false
"_shards" => array:4 [▶]
"hits" => array:3 [▶]
"aggregations" => array:1 [▼
"hat_prices" => array:1 [▼
"value" => 17038.9000594
]
]
]
Thank you @Kristories, this only solves part of the problem, I will still have sometimes arrays, sometimes objects returned by getAll() function.
The problem with @Kristories approach is consistency. Calling "->response()" instead of "->get()" will return an array instead of a "Basemkhirat\Elasticsearch\Collection" object, so you need to adapt your parsing method. We should always expect a Collection object from the response, but right now the library's Query->getAll() class doesn't parse the "aggs" from the response, only the "hits". This should be added to the getAll() method, or somewhere in there.