elasticsearch icon indicating copy to clipboard operation
elasticsearch copied to clipboard

what about aggregations

Open ppeer opened this issue 7 years ago • 8 comments

This repository looks really great. Is there also a solution for aggregation metrics?

regards Ppeer

ppeer avatar Jun 01 '17 20:06 ppeer

+1

vonec avatar Sep 15 '17 09:09 vonec

any updates on this?

vonec avatar Sep 26 '17 09:09 vonec

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 ...

asimionica avatar Oct 17 '17 08:10 asimionica

Is there any chance to fix this issue soon?

asimionica avatar Oct 17 '17 11:10 asimionica

+1

dairton avatar Oct 19 '17 15:10 dairton

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
    ]
  ]
]

Kristories avatar Oct 23 '17 05:10 Kristories

Thank you @Kristories, this only solves part of the problem, I will still have sometimes arrays, sometimes objects returned by getAll() function.

asimionica avatar Oct 23 '17 08:10 asimionica

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.

clytemnestra avatar Oct 24 '17 08:10 clytemnestra