Skip to content

Commit c036b22

Browse files
committed
Nested objects upgrade
1 parent 4003e47 commit c036b22

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

src/Query/Builder.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,15 +1205,20 @@ public function orWhereNotGeoDistance($field, string $distance, array $location,
12051205
*
12061206
* @param string $column
12071207
* @param callable|BaseBuilder|static $query
1208+
* @param bool $filterInnerHits
1209+
* @param array $options
12081210
* @param string $boolean
1211+
* @param bool $not
12091212
*/
1210-
public function whereNestedObject($column, $query, $innerHits = true, $options = [], $boolean = 'and', $not = false): self
1213+
public function whereNestedObject($column, $query, $filterInnerHits = false, $options = [], $boolean = 'and', $not = false): self
12111214
{
12121215

12131216
$from = $this->from;
12141217
$type = 'NestedObject';
12151218
$options = $this->setOptions($options, 'nested');
1216-
$options->innerHits($innerHits);
1219+
if ($filterInnerHits) {
1220+
$options->innerHits(true);
1221+
}
12171222
$options = $options->toArray();
12181223

12191224
$this->options()->add('parentField', $column);
@@ -1226,19 +1231,19 @@ public function whereNestedObject($column, $query, $innerHits = true, $options =
12261231
return $this;
12271232
}
12281233

1229-
public function orWhereNestedObject($column, $query, $innerHits = true, $options = []): self
1234+
public function orWhereNestedObject($column, $query, $filterInnerHits = false, $options = []): self
12301235
{
1231-
return $this->whereNestedObject($column, $query, $innerHits, $options, 'or');
1236+
return $this->whereNestedObject($column, $query, $filterInnerHits, $options, 'or');
12321237
}
12331238

1234-
public function whereNotNestedObject($column, $query, $innerHits = true, $options = []): self
1239+
public function whereNotNestedObject($column, $query, $filterInnerHits = false, $options = []): self
12351240
{
1236-
return $this->whereNestedObject($column, $query, $innerHits, $options, 'and', true);
1241+
return $this->whereNestedObject($column, $query, $filterInnerHits, $options, 'and', true);
12371242
}
12381243

1239-
public function orWhereNotNestedObject($column, $query, $innerHits = true, $options = []): self
1244+
public function orWhereNotNestedObject($column, $query, $filterInnerHits = false, $options = []): self
12401245
{
1241-
return $this->whereNestedObject($column, $query, $innerHits, $options, 'or', true);
1246+
return $this->whereNestedObject($column, $query, $filterInnerHits, $options, 'or', true);
12421247
}
12431248

12441249
public function filterNested($column, $query, $options = [])
@@ -1564,11 +1569,9 @@ public function orderByGeoDesc(string $column, array $coordinates, array $option
15641569
return $this->orderBy($column, -1, $options);
15651570
}
15661571

1567-
public function orderByNested(string $column, $direction = 1, mixed $options = []): self
1572+
public function orderByNested(string $column, $direction = 1, string $mode = 'avg'): self
15681573
{
1569-
if (is_string($options)) {
1570-
$options = ['mode' => $options];
1571-
}
1574+
$options = ['mode' => $mode];
15721575
$options = [
15731576
...$options,
15741577
'nested' => ['path' => Str::beforeLast($column, '.')],

src/Query/DSL/DslFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,11 @@ public static function applyBoost(array $clause, $boostValue): array
540540
return $clause;
541541
}
542542

543-
public static function applyInnerHits(array $clause, $options)
543+
public static function applyInnerHits(array $clause, $innerHits)
544544
{
545545
$firstKey = key($clause);
546546

547-
$clause[$firstKey]['inner_hits'] = empty($options) || $options === true ? (object) [] : (array) $options;
547+
$clause[$firstKey]['inner_hits'] = empty($innerHits) ? (object) [] : (array) $innerHits;
548548

549549
return $clause;
550550
}

src/Query/Grammar.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ protected function compileWhereNestedObject(Builder $builder, $where): array
517517
// Compile the inner query
518518
$wheres = $this->compileWheres($where['query']);
519519
$path = $where['column'];
520+
if (! $wheres['query']) {
521+
$wheres['query'] = DslFactory::exists($path);
522+
}
523+
520524
$options = $where['options'] ?? [];
521525
$wheres = array_filter($wheres);
522526

@@ -525,18 +529,9 @@ protected function compileWhereNestedObject(Builder $builder, $where): array
525529
return $this->applyOptionsToClause($query, $where);
526530
}
527531

528-
/**
529-
* Compile a where nested clause
530-
*
531-
* @param array $where
532-
*
533-
* @throws BuilderException
534-
*/
535-
protected function compileWhereInnerNested(Builder $builder, $where): array
532+
public function _buildInnerHits($innerQuery)
536533
{
537-
// Compile the inner filter
538-
$innerQuery = $where['query'];
539-
$query = $this->compileWheres($innerQuery);
534+
540535
$innerHits = [];
541536
$compiledOrders = [];
542537
if ($innerQuery->orders) {
@@ -554,10 +549,23 @@ protected function compileWhereInnerNested(Builder $builder, $where): array
554549
}
555550
if ($size = $innerQuery->getSetLimit()) {
556551
$innerHits['size'] = $size;
557-
} else {
558-
$innerHits['size'] = 100;
559552
}
560553

554+
return $innerHits;
555+
}
556+
557+
/**
558+
* Compile a where nested clause
559+
*
560+
* @param array $where
561+
*/
562+
protected function compileWhereInnerNested(Builder $builder, $where): array
563+
{
564+
// Compile the inner filter
565+
$innerQuery = $where['query'];
566+
$query = $this->compileWheres($innerQuery);
567+
$innerHits = $this->_buildInnerHits($innerQuery);
568+
561569
return DslFactory::innerNested($where['column'], $query['query'], $innerHits);
562570

563571
}
@@ -765,7 +773,7 @@ protected function compileOrders(Builder|BaseBuilder $builder, $orders = []): ar
765773
if (Str::startsWith($column, $builder->from.'.')) {
766774
$column = Str::replaceFirst($builder->from.'.', '', $column);
767775
}
768-
776+
$column = $this->prependParentField($column, $builder);
769777
$column = $this->getIndexableField($column, $builder);
770778

771779
$type = $order['type'] ?? 'basic';
@@ -808,6 +816,7 @@ protected function compileSorts(Builder $builder, $sorts, $compiledOrders): arra
808816
{
809817
foreach ($sorts as $column => $sort) {
810818
$found = false;
819+
$column = $this->prependParentField($column, $builder);
811820
$column = $this->getIndexableField($column, $builder);
812821
if ($compiledOrders) {
813822
foreach ($compiledOrders as $i => $compiledOrder) {
@@ -1324,9 +1333,11 @@ protected function compileTermsAggregation(Builder $builder, array $aggregation)
13241333
/**
13251334
* Apply inner hits options to the clause
13261335
*/
1327-
protected function applyInnerHitsOption(array $clause, $options): array
1336+
protected function applyInnerHitsOption(array $clause, $options, $where): array
13281337
{
1329-
return DslFactory::applyInnerHits($clause, $options);
1338+
$innerHits = $this->_buildInnerHits($where['query']);
1339+
1340+
return DslFactory::applyInnerHits($clause, $innerHits);
13301341
}
13311342

13321343
/**
@@ -1385,13 +1396,11 @@ protected function applyOptionsToClause(array $clause, array $where): array
13851396
if (empty($where['options'])) {
13861397
return $clause;
13871398
}
1388-
13891399
$optionsToApply = ['inner_hits'];
13901400
$options = array_intersect_key($where['options'], array_flip($optionsToApply));
13911401

13921402
foreach ($options as $option => $value) {
13931403
$method = 'apply'.Str::studly($option).'Option';
1394-
13951404
if (method_exists($this, $method)) {
13961405
$clause = $this->$method($clause, $value, $where);
13971406
}

src/Query/Options/NestedOptions.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/**
66
* @method $this scoreMode(string $value)
77
* @method $this ignoreUnmapped(bool $value)
8-
* @method $this innerHits(bool $value)
98
*/
109
class NestedOptions extends QueryOptions
1110
{
@@ -14,7 +13,6 @@ public function allowedOptions(): array
1413
return [
1514
'score_mode',
1615
'ignore_unmapped',
17-
'inner_hits',
1816
];
1917
}
2018
}

0 commit comments

Comments
 (0)