Skip to content

Commit 2d3fedf

Browse files
committed
Base aggs on more than one field in a single call
1 parent 38b1d25 commit 2d3fedf

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/DSL/Bridge.php

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

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

169169
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -730,11 +730,16 @@ public function _countAggregate($wheres, $options, $columns): Results
730730
private function _maxAggregate($wheres, $options, $columns): Results
731731
{
732732
$params = $this->buildParams($this->index, $wheres, $options);
733+
if (is_array($columns[0])) {
734+
$columns = $columns[0];
735+
}
733736
try {
734-
$params['body']['aggs']['max_value'] = ParameterBuilder::maxAggregation($columns[0]);
737+
foreach ($columns as $column) {
738+
$params['body']['aggs']['max_'.$column] = ParameterBuilder::maxAggregation($column);
739+
}
735740
$process = $this->client->search($params);
736741

737-
return $this->_return($process['aggregations']['max_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
742+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
738743
} catch (Exception $e) {
739744

740745
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -748,11 +753,16 @@ private function _maxAggregate($wheres, $options, $columns): Results
748753
private function _minAggregate($wheres, $options, $columns): Results
749754
{
750755
$params = $this->buildParams($this->index, $wheres, $options);
756+
if (is_array($columns[0])) {
757+
$columns = $columns[0];
758+
}
751759
try {
752-
$params['body']['aggs']['min_value'] = ParameterBuilder::minAggregation($columns[0]);
760+
foreach ($columns as $column) {
761+
$params['body']['aggs']['min_'.$column] = ParameterBuilder::minAggregation($column);
762+
}
753763
$process = $this->client->search($params);
754764

755-
return $this->_return($process['aggregations']['min_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
765+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
756766
} catch (Exception $e) {
757767
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
758768
}
@@ -766,11 +776,16 @@ private function _sumAggregate($wheres, $options, $columns): Results
766776
{
767777

768778
$params = $this->buildParams($this->index, $wheres, $options);
779+
if (is_array($columns[0])) {
780+
$columns = $columns[0];
781+
}
769782
try {
770-
$params['body']['aggs']['sum_value'] = ParameterBuilder::sumAggregation($columns[0]);
783+
foreach ($columns as $column) {
784+
$params['body']['aggs']['sum_'.$column] = ParameterBuilder::sumAggregation($column);
785+
}
771786
$process = $this->client->search($params);
772787

773-
return $this->_return($process['aggregations']['sum_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
788+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
774789
} catch (Exception $e) {
775790

776791
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
@@ -785,11 +800,16 @@ private function _sumAggregate($wheres, $options, $columns): Results
785800
private function _avgAggregate($wheres, $options, $columns): Results
786801
{
787802
$params = $this->buildParams($this->index, $wheres, $options);
803+
if (is_array($columns[0])) {
804+
$columns = $columns[0];
805+
}
788806
try {
789-
$params['body']['aggs']['avg_value'] = ParameterBuilder::avgAggregation($columns[0]);
807+
foreach ($columns as $column) {
808+
$params['body']['aggs']['avg_'.$column] = ParameterBuilder::avgAggregation($column);
809+
}
790810
$process = $this->client->search($params);
791811

792-
return $this->_return($process['aggregations']['avg_value']['value'] ?? 0, $process, $params, $this->_queryTag(__FUNCTION__));
812+
return $this->_sanitizeAggsResponse($process, $params, $this->_queryTag(__FUNCTION__));
793813
} catch (Exception $e) {
794814
$this->throwError($e, $params, $this->_queryTag(__FUNCTION__));
795815
}
@@ -1067,7 +1087,22 @@ private function _sanitizeHighlights($highlights)
10671087
return $highlights;
10681088
}
10691089

1070-
private function _sanitizeAggsResponse($response, $params, $queryTag)
1090+
public function _sanitizeAggsResponse($response, $params, $queryTag)
1091+
{
1092+
$meta['timed_out'] = $response['timed_out'];
1093+
$meta['total'] = $response['hits']['total']['value'] ?? 0;
1094+
$meta['max_score'] = $response['hits']['max_score'] ?? 0;
1095+
$meta['sorts'] = [];
1096+
1097+
$aggs = $response['aggregations'];
1098+
$data = (count($aggs) === 1)
1099+
? reset($aggs)['value'] ?? 0
1100+
: array_map(fn($value) => $value['value'] ?? 0, $aggs);
1101+
1102+
return $this->_return($data, $meta, $params, $queryTag);
1103+
}
1104+
1105+
private function _sanitizeRawAggsResponse($response, $params, $queryTag)
10711106
{
10721107
$meta['timed_out'] = $response['timed_out'];
10731108
$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)