@@ -93,7 +93,6 @@ public function processIndicesDsl($method, $params): Results
9393 */
9494 public function processFind ($ wheres , $ options , $ columns ): Results
9595 {
96-
9796 $ params = $ this ->buildParams ($ this ->index , $ wheres , $ options , $ columns );
9897
9998 return $ this ->_returnSearch ($ params , __FUNCTION__ );
@@ -104,9 +103,9 @@ public function processFind($wheres, $options, $columns): Results
104103 */
105104 public function processSearch ($ searchParams , $ searchOptions , $ wheres , $ opts , $ fields , $ cols )
106105 {
107-
108106 $ params = $ this ->buildSearchParams ($ this ->index , $ searchParams , $ searchOptions , $ wheres , $ opts , $ fields , $ cols );
109107
108+
110109 return $ this ->_returnSearch ($ params , __FUNCTION__ );
111110
112111 }
@@ -117,6 +116,7 @@ protected function _returnSearch($params, $source)
117116 $ params ['size ' ] = $ this ->maxSize ;
118117 }
119118 try {
119+
120120 $ process = $ this ->client ->search ($ params );
121121
122122 return $ this ->_sanitizeSearchResponse ($ process , $ params , $ this ->_queryTag ($ source ));
@@ -141,6 +141,13 @@ public function processDistinct($wheres, $options, $columns, $includeDocCount =
141141 unset($ options ['skip ' ]);
142142 unset($ options ['limit ' ]);
143143
144+ if ($ sort ) {
145+ $ sortField = key ($ sort );
146+ $ sortDir = $ sort [$ sortField ]['order ' ] ?? 'asc ' ;
147+ $ sort = [$ sortField => $ sortDir ];
148+ }
149+
150+
144151 $ params = $ this ->buildParams ($ this ->index , $ wheres , $ options );
145152 $ params ['body ' ]['aggs ' ] = $ this ->createNestedAggs ($ columns , $ sort );
146153
@@ -649,6 +656,25 @@ private function _matrixAggregate($wheres, $options, $columns): Results
649656
650657 }
651658
659+ public function parseRequiredKeywordMapping ($ field )
660+ {
661+ $ mappings = $ this ->processIndexMappings ($ this ->index );
662+ $ map = reset ($ mappings );
663+ if (!empty ($ map ['mappings ' ]['properties ' ][$ field ])) {
664+ $ fieldMap = $ map ['mappings ' ]['properties ' ][$ field ];
665+ if (!empty ($ fieldMap ['type ' ]) && $ fieldMap ['type ' ] === 'keyword ' ) {
666+ //primary Map is field. Use as is
667+ return $ field ;
668+ }
669+ if (!empty ($ fieldMap ['fields ' ]['keyword ' ])) {
670+ return $ field .'.keyword ' ;
671+ }
672+ }
673+
674+ return false ;
675+
676+ }
677+
652678 //----------------------------------------------------------------------
653679 // Distinct Aggregates
654680 //----------------------------------------------------------------------
@@ -805,6 +831,7 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
805831 $ meta ['timed_out ' ] = $ response ['timed_out ' ];
806832 $ meta ['total ' ] = $ response ['hits ' ]['total ' ]['value ' ] ?? 0 ;
807833 $ meta ['max_score ' ] = $ response ['hits ' ]['max_score ' ] ?? 0 ;
834+ $ meta ['sorts ' ] = [];
808835 $ data = [];
809836 if (!empty ($ response ['hits ' ]['hits ' ])) {
810837 foreach ($ response ['hits ' ]['hits ' ] as $ hit ) {
@@ -816,13 +843,55 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
816843 $ datum [$ key ] = $ value ;
817844 }
818845 }
819- $ data [] = $ datum ;
846+ if (!empty ($ hit ['inner_hits ' ])) {
847+ foreach ($ hit ['inner_hits ' ] as $ innerKey => $ innerHit ) {
848+ $ datum [$ innerKey ] = $ this ->_filterInnerHits ($ innerHit );
849+ }
850+ }
851+
852+ //------------------------ later, maybe ------------------------------
853+ // if (!empty($hit['sort'])) {
854+ // $datum['_meta']['sort'] = $this->_parseSort($hit['sort'], $params['body']['sort'] ?? []);
855+ // }
856+ //----------------------------------------------------------------------
857+
858+
859+ {
860+ $ data [] = $ datum ;
861+ }
820862 }
821863 }
822864
823865 return $ this ->_return ($ data , $ meta , $ params , $ queryTag );
824866 }
825867
868+ private function _filterInnerHits ($ innerHit )
869+ {
870+ $ hits = [];
871+ foreach ($ innerHit ['hits ' ]['hits ' ] as $ inner ) {
872+ $ innerDatum = [];
873+ if (!empty ($ inner ['_source ' ])) {
874+ foreach ($ inner ['_source ' ] as $ innerSourceKey => $ innerSourceValue ) {
875+ $ innerDatum [$ innerSourceKey ] = $ innerSourceValue ;
876+ }
877+ }
878+ $ hits [] = $ innerDatum ;
879+ }
880+
881+ return $ hits ;
882+ }
883+
884+
885+ private function _parseSort ($ sort , $ sortParams )
886+ {
887+ $ sortValues = [];
888+ foreach ($ sort as $ key => $ value ) {
889+ $ sortValues [array_key_first ($ sortParams [$ key ])] = $ value ;
890+ }
891+
892+ return $ sortValues ;
893+ }
894+
826895 private function _sanitizeDistinctResponse ($ response , $ columns , $ includeDocCount )
827896 {
828897 $ keys = [];
@@ -837,17 +906,18 @@ private function _sanitizeDistinctResponse($response, $columns, $includeDocCount
837906 private function processBuckets ($ columns , $ keys , $ response , $ index , $ includeDocCount , $ currentData = [])
838907 {
839908 $ data = [];
840-
841909 if (!empty ($ response [$ keys [$ index ]]['buckets ' ])) {
842910 foreach ($ response [$ keys [$ index ]]['buckets ' ] as $ res ) {
911+
843912 $ datum = $ currentData ;
844- //clear keyword from column name
913+
845914 $ col = $ columns [$ index ];
846915 if (str_contains ($ col , '.keyword ' )) {
847916 $ col = str_replace ('.keyword ' , '' , $ col );
848917 }
849918
850919 $ datum [$ col ] = $ res ['key ' ];
920+
851921 if ($ includeDocCount ) {
852922 $ datum [$ col .'_count ' ] = $ res ['doc_count ' ];
853923 }
0 commit comments