@@ -129,14 +129,14 @@ public function createNestedAggs($columns, $sort)
129129 if (!isset ($ terms ['terms ' ]['order ' ])) {
130130 $ terms ['terms ' ]['order ' ] = [];
131131 }
132- if ($ sort ['_count ' ] == 1 ) {
132+ if ($ sort ['_count ' ] == ' asc ' ) {
133133 $ terms ['terms ' ]['order ' ][] = ['_count ' => 'asc ' ];
134134 } else {
135135 $ terms ['terms ' ]['order ' ][] = ['_count ' => 'desc ' ];
136136 }
137137 }
138138 if (isset ($ sort [$ columns [0 ]])) {
139- if ($ sort [$ columns [0 ]] == 1 ) {
139+ if ($ sort [$ columns [0 ]] == ' asc ' ) {
140140 $ terms ['terms ' ]['order ' ][] = ['_key ' => 'asc ' ];
141141 } else {
142142 $ terms ['terms ' ]['order ' ][] = ['_key ' => 'desc ' ];
@@ -205,15 +205,15 @@ private function _buildQuery($wheres): array
205205 }
206206
207207
208- public function _convertWheresToDSL ($ wheres ): array
208+ public function _convertWheresToDSL ($ wheres, $ parentField = false ): array
209209 {
210210 $ dsl = ['bool ' => []];
211211 foreach ($ wheres as $ logicalOperator => $ conditions ) {
212212 switch ($ logicalOperator ) {
213213 case 'and ' :
214214 $ dsl ['bool ' ]['must ' ] = [];
215215 foreach ($ conditions as $ condition ) {
216- $ parsedCondition = $ this ->_parseCondition ($ condition );
216+ $ parsedCondition = $ this ->_parseCondition ($ condition, $ parentField );
217217 if (!empty ($ parsedCondition )) {
218218 $ dsl ['bool ' ]['must ' ][] = $ parsedCondition ;
219219 }
@@ -225,7 +225,7 @@ public function _convertWheresToDSL($wheres): array
225225 $ boolClause = ['bool ' => ['must ' => []]];
226226 foreach ($ conditionGroup as $ subConditions ) {
227227 foreach ($ subConditions as $ subCondition ) {
228- $ parsedCondition = $ this ->_parseCondition ($ subCondition );
228+ $ parsedCondition = $ this ->_parseCondition ($ subCondition, $ parentField );
229229 if (!empty ($ parsedCondition )) {
230230 $ boolClause ['bool ' ]['must ' ][] = $ parsedCondition ;
231231 }
@@ -237,17 +237,22 @@ public function _convertWheresToDSL($wheres): array
237237 }
238238 break ;
239239 default :
240- return $ this ->_parseCondition ($ wheres );
240+ return $ this ->_parseCondition ($ wheres, $ parentField );
241241 }
242242 }
243243
244244 return $ dsl ;
245245 }
246246
247- private function _parseCondition ($ condition ): array
247+ private function _parseCondition ($ condition, $ parentField = null ): array
248248 {
249- // dd($condition);
250249 $ field = key ($ condition );
250+ if ($ parentField ) {
251+ if (!str_starts_with ($ field , $ parentField .'. ' )) {
252+ $ field = $ parentField .'. ' .$ field ;
253+ }
254+ }
255+
251256 $ value = current ($ condition );
252257
253258
@@ -345,7 +350,7 @@ private function _parseCondition($condition): array
345350 $ queryPart = [
346351 'nested ' => [
347352 'path ' => $ field ,
348- 'query ' => $ this ->_convertWheresToDSL ($ operand ['wheres ' ]),
353+ 'query ' => $ this ->_convertWheresToDSL ($ operand ['wheres ' ], $ field ),
349354 'score_mode ' => $ operand ['score_mode ' ],
350355 ],
351356 ];
@@ -365,6 +370,21 @@ private function _parseCondition($condition): array
365370 ],
366371
367372 ];
373+ break ;
374+ case 'innerNested ' :
375+ $ options = $ this ->_buildNestedOptions ($ operand ['options ' ], $ field );
376+ $ query = ParameterBuilder::matchAll ()['query ' ];
377+ if (!empty ($ operand ['wheres ' ])) {
378+ $ query = $ this ->_convertWheresToDSL ($ operand ['wheres ' ], $ field );
379+ }
380+ $ queryPart = [
381+ 'nested ' => [
382+ 'path ' => $ field ,
383+ 'query ' => $ query ,
384+ 'inner_hits ' => $ options ,
385+ ],
386+ ];
387+
368388 break ;
369389 default :
370390 abort ('400 ' , 'Invalid operator [ ' .$ operator .'] provided for condition. ' );
@@ -390,8 +410,8 @@ private function _buildOptions($options): array
390410 if (!isset ($ return ['body ' ]['sort ' ])) {
391411 $ return ['body ' ]['sort ' ] = [];
392412 }
393- foreach ($ value as $ field => $ direction ) {
394- $ return ['body ' ]['sort ' ][] = $ this -> _parseSortOrder ($ field , $ direction );
413+ foreach ($ value as $ field => $ sortPayload ) {
414+ $ return ['body ' ]['sort ' ][] = ParameterBuilder:: fieldSort ($ field , $ sortPayload );
395415 }
396416 break ;
397417 case 'skip ' :
@@ -405,8 +425,10 @@ private function _buildOptions($options): array
405425 $ this ->_parseFilter ($ filterType , $ filerValues );
406426 }
407427 break ;
428+
408429 case 'multiple ' :
409430 case 'searchOptions ' :
431+
410432 //Pass through
411433 break ;
412434 default :
@@ -418,6 +440,32 @@ private function _buildOptions($options): array
418440 return $ return ;
419441 }
420442
443+ private function _buildNestedOptions ($ options , $ field )
444+ {
445+ $ options = $ this ->_buildOptions ($ options );
446+ if (!empty ($ options ['body ' ])) {
447+ $ body = $ options ['body ' ];
448+ unset($ options ['body ' ]);
449+ $ options = array_merge ($ options , $ body );
450+ }
451+ if (!empty ($ options ['sort ' ])) {
452+ //ensure that the sort field is prefixed with the nested field
453+ $ sorts = [];
454+ foreach ($ options ['sort ' ] as $ sort ) {
455+ foreach ($ sort as $ sortField => $ sortPayload ) {
456+ if (!str_starts_with ($ sortField , $ field .'. ' )) {
457+ $ sortField = $ field .'. ' .$ sortField ;
458+ }
459+ $ sorts [] = [$ sortField => $ sortPayload ];
460+ }
461+ }
462+
463+ $ options ['sort ' ] = $ sorts ;
464+ }
465+
466+ return $ options ;
467+ }
468+
421469 public function _parseFilter ($ filterType , $ filterPayload ): void
422470 {
423471 switch ($ filterType ) {
@@ -440,15 +488,6 @@ public function _parseFilter($filterType, $filterPayload): void
440488 }
441489 }
442490
443- private function _parseSortOrder ($ field , $ direction ): array
444- {
445- $ dir = 'desc ' ;
446- if ($ direction == 1 ) {
447- $ dir = 'asc ' ;
448- }
449-
450- return ParameterBuilder::fieldSort ($ field , $ dir );
451- }
452491
453492 public function _parseFilterParameter ($ params , $ filer )
454493 {
0 commit comments