@@ -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 }
0 commit comments