Skip to content

Commit d2d2883

Browse files
committed
Add DSL query support and multi-column aggregation
Introduces the ability to return queries as DSL via an `asDsl` flag and a new `dslQuery()` method. Updates aggregation methods to support multiple columns and adjusts return types for `get` and `count` to allow array results when using DSL mode.
1 parent a4a483a commit d2d2883

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/Eloquent/Builder.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Builder extends BaseEloquentBuilder
3737

3838
protected $type;
3939

40+
protected $asDsl = false;
41+
4042
protected $model;
4143

4244
protected $passthru = [
@@ -113,6 +115,13 @@ public function query(): QueryBuilder
113115
return $this->query;
114116
}
115117

118+
public function dslQuery(): QueryBuilder
119+
{
120+
$this->query->asDsl = true;
121+
122+
return $this->query;
123+
}
124+
116125
/**
117126
* {@inheritdoc}
118127
*/
@@ -132,8 +141,9 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
132141
/**
133142
* {@inheritdoc}
134143
*/
135-
public function get($columns = ['*']): ElasticCollection
144+
public function get($columns = ['*']): ElasticCollection|array
136145
{
146+
137147
if (! is_array($columns)) {
138148
$columns = [$columns];
139149
}
@@ -525,9 +535,9 @@ public function stringStats($columns, $options = [])
525535
return $this->hydrateAggregationResult($this->query->stringStats($columns, $options));
526536
}
527537

528-
public function agg(array $functions, string $column, array $options = [])
538+
public function agg(array $functions, string|array $columns, array $options = [])
529539
{
530-
return $this->hydrateAggregationResult($this->query->agg($functions, $column, $options));
540+
return $this->hydrateAggregationResult($this->query->agg($functions, $columns, $options));
531541
}
532542

533543
protected function hydrateAggregationResult($result)

src/Query/Builder.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,11 @@ public function orderBy($column, $direction = 1, array $options = []): self
495495
*
496496
* @param array $columns
497497
*/
498-
public function get($columns = ['*']): ElasticCollection
498+
public function get($columns = ['*']): ElasticCollection|array
499499
{
500-
500+
if ($this->asDsl) {
501+
return $this->toDsl();
502+
}
501503
$original = $this->columns;
502504

503505
if (is_null($original)) {
@@ -620,20 +622,24 @@ public function exists(): bool
620622
/**
621623
* {@inheritdoc}
622624
*/
623-
public function count($columns = null, array $options = []): int
625+
public function count($columns = null, array $options = []): int|array
624626
{
625627
// If columns are specified, we will aggregate the count of the specified columns.
626628
// if ($columns) {
627629
// return $this->aggregate(__FUNCTION__, Arr::wrap($columns), $options);
628630
// }
629631

630632
// Otherwise, we will just count the records.
633+
if ($this->asDsl) {
634+
return $this->grammar->compileCount($this);
635+
}
636+
631637
return $this->connection->count($this->grammar->compileCount($this));
632638
}
633639

634-
public function agg(array $functions, string $column, array $options = [])
640+
public function agg(array $functions, string|array $columns, array $options = [])
635641
{
636-
return $this->aggregateMultiMetric($functions, $column, $options);
642+
return $this->aggregateMultiMetric($functions, $columns, $options);
637643
}
638644

639645
/**
@@ -1851,20 +1857,28 @@ protected function aggregateMetric($function, $columns = ['*'], $options = [])
18511857
'options' => $options,
18521858
];
18531859
}
1860+
if ($this->asDsl) {
1861+
return $this->grammar->compileSelect($this);
1862+
}
18541863

18551864
return $this->processor->processAggregations($this, $this->connection->select($this->grammar->compileSelect($this), []));
18561865
}
18571866

1858-
protected function aggregateMultiMetric(array $functions, string $column, $options = [])
1867+
protected function aggregateMultiMetric(array $functions, string|array $columns, $options = [])
18591868
{
18601869
// Each column we want aggregated
1861-
foreach ($functions as $function) {
1862-
$this->metricsAggregations[] = [
1863-
'key' => $function.'_'.$column,
1864-
'args' => $column,
1865-
'type' => $function,
1866-
'options' => $options,
1867-
];
1870+
if (! is_array($columns)) {
1871+
$columns = Arr::wrap($columns);
1872+
}
1873+
foreach ($columns as $column) {
1874+
foreach ($functions as $function) {
1875+
$this->metricsAggregations[] = [
1876+
'key' => $function.'_'.$column,
1877+
'args' => $column,
1878+
'type' => $function,
1879+
'options' => $options,
1880+
];
1881+
}
18681882
}
18691883

18701884
return $this->processor->processAggregations($this, $this->connection->select($this->grammar->compileSelect($this), []));

0 commit comments

Comments
 (0)