Skip to content

Commit d713d57

Browse files
committed
Base aggs on more than one field in a single call
1 parent cccd236 commit d713d57

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/DSL/Bridge.php

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function processAggregationRaw($bodyParams): Results
164164
try {
165165
$process = $this->client->search($params);
166166

167-
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
167+
return $this->_sanitizeRawAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
168168
} catch (Exception $e) {
169169

170170
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -738,11 +738,17 @@ public function _countAggregate($wheres, $options, $columns): Results
738738
private function _maxAggregate($wheres, $options, $columns): Results
739739
{
740740
$params = $this->buildParams($this->index, $wheres, $options);
741+
if (is_array($columns[0])) {
742+
$columns = $columns[0];
743+
}
741744
try {
742-
$params['body']['aggs']['max_value'] = ParameterBuilder::maxAggregation($columns[0]);
745+
foreach ($columns as $column) {
746+
$params['body']['aggs']['max_'.$column] = ParameterBuilder::maxAggregation($column);
747+
}
743748
$process = $this->client->search($params);
744749

745-
return $this->_return($process['aggregations']['max_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
750+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
751+
746752
} catch (Exception $e) {
747753

748754
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -756,11 +762,16 @@ private function _maxAggregate($wheres, $options, $columns): Results
756762
private function _minAggregate($wheres, $options, $columns): Results
757763
{
758764
$params = $this->buildParams($this->index, $wheres, $options);
765+
if (is_array($columns[0])) {
766+
$columns = $columns[0];
767+
}
759768
try {
760-
$params['body']['aggs']['min_value'] = ParameterBuilder::minAggregation($columns[0]);
769+
foreach ($columns as $column) {
770+
$params['body']['aggs']['min_'.$column] = ParameterBuilder::minAggregation($column);
771+
}
761772
$process = $this->client->search($params);
762773

763-
return $this->_return($process['aggregations']['min_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
774+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
764775
} catch (Exception $e) {
765776
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
766777
}
@@ -772,13 +783,18 @@ private function _minAggregate($wheres, $options, $columns): Results
772783
*/
773784
private function _sumAggregate($wheres, $options, $columns): Results
774785
{
775-
776786
$params = $this->buildParams($this->index, $wheres, $options);
787+
if (is_array($columns[0])) {
788+
$columns = $columns[0];
789+
}
777790
try {
778-
$params['body']['aggs']['sum_value'] = ParameterBuilder::sumAggregation($columns[0]);
791+
foreach ($columns as $column) {
792+
$params['body']['aggs']['sum_'.$column] = ParameterBuilder::sumAggregation($column);
793+
}
779794
$process = $this->client->search($params);
780795

781-
return $this->_return($process['aggregations']['sum_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
796+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
797+
782798
} catch (Exception $e) {
783799

784800
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -793,11 +809,17 @@ private function _sumAggregate($wheres, $options, $columns): Results
793809
private function _avgAggregate($wheres, $options, $columns): Results
794810
{
795811
$params = $this->buildParams($this->index, $wheres, $options);
812+
if (is_array($columns[0])) {
813+
$columns = $columns[0];
814+
}
796815
try {
797-
$params['body']['aggs']['avg_value'] = ParameterBuilder::avgAggregation($columns[0]);
816+
foreach ($columns as $column) {
817+
$params['body']['aggs']['avg_'.$column] = ParameterBuilder::avgAggregation($column);
818+
}
798819
$process = $this->client->search($params);
799820

800-
return $this->_return($process['aggregations']['avg_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
821+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
822+
801823
} catch (Exception $e) {
802824
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
803825
}
@@ -1075,7 +1097,22 @@ private function _sanitizeHighlights($highlights)
10751097
return $highlights;
10761098
}
10771099

1078-
private function _sanitizeAggsResponse($response, $params, $queryTag)
1100+
public function _sanitizeAggsResponse($response, $params, $queryTag)
1101+
{
1102+
$meta['timed_out'] = $response['timed_out'];
1103+
$meta['total'] = $response['hits']['total']['value'] ?? 0;
1104+
$meta['max_score'] = $response['hits']['max_score'] ?? 0;
1105+
$meta['sorts'] = [];
1106+
1107+
$aggs = $response['aggregations'];
1108+
$data = (count($aggs) === 1)
1109+
? reset($aggs)['value'] ?? 0
1110+
: array_map(fn($value) => $value['value'] ?? 0, $aggs);
1111+
1112+
return $this->_return($data, $meta, $params, $queryTag);
1113+
}
1114+
1115+
private function _sanitizeRawAggsResponse($response, $params, $queryTag)
10791116
{
10801117
$meta['timed_out'] = $response['timed_out'];
10811118
$meta['total'] = $response['hits']['total']['value'] ?? 0;

src/Eloquent/Docs/ModelDocs.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* @method $this minScore(float $value)
2020
* @method $this field(string $field, int $boostFactor = null)
2121
* @method $this fields(array $fields)
22+
* @method sum(array|string $columns)
23+
* @method min(array|string $columns)
24+
* @method max(array|string $columns)
25+
* @method avg(array|string $columns)
2226
* @method search(array $columns = '*')
2327
* @method query(array $columns = '*')
2428
* @method toDsl(array $columns = '*')

0 commit comments

Comments
 (0)