@@ -38,6 +38,7 @@ class ElasticSearch
3838 protected $ sort = [];
3939 protected $ ignores = [];
4040 protected $ attributesToHighlight = [];
41+ protected $ attributesAggregations = [];
4142 protected $ searchable = [];
4243 protected $ facetsDistribution = [];
4344 protected $ query = '' ;
@@ -316,6 +317,10 @@ public function highlight(array $attributes = [])
316317 $ this ->attributesToHighlight = $ attributes ;
317318 return $ this ;
318319 }
320+ public function aggs (array $ attributes = []){
321+ $ this ->attributesAggregations = $ attributes ;
322+ return $ this ;
323+ }
319324
320325 public function facets (array $ attributes = ['* ' ])
321326 {
@@ -416,10 +421,11 @@ protected function getFirst($result = [])
416421 $ model = new Collection ($ result ["_source " ]);
417422 // match earlier version
418423 $ model ->_index = $ result ["_index " ];
419- $ model ->_type = $ result ["_type " ];
424+ $ model ->_type = $ result ["_type " ] ?? '' ;
420425 $ model ->_id = $ result ["_id " ];
421426 $ model ->_score = $ result ["_score " ] ?? [];
422427 $ model ->_highlight = isset ($ result ["highlight " ]) ? $ result ["highlight " ] : [];
428+ $ model ->raw = $ result ;
423429 $ new = $ model ;
424430 } else {
425431 $ new = NULL ;
@@ -446,6 +452,7 @@ protected function getAll($result = []){
446452 }
447453 $ collect = new Collection ([]);
448454 $ collect ->items = $ new ;
455+ $ collect ->raw = $ result ;
449456 $ total = $ result ["hits " ]["total " ];
450457 $ collect ->total = is_array ($ total ) ? $ total ["value " ] : $ total ;
451458 $ collect ->page = request () ? request ()->input ('page ' ,1 ) : $ this ->page ;
@@ -487,7 +494,7 @@ protected function filters()
487494 $ body ["query " ]["bool " ]["filter " ][] = ["range " => [$ item ['column ' ] => ["lte " => $ item ['value ' ]]]];
488495 }
489496 if ($ item ['operator ' ] == "like " ) {
490- $ body ["query " ]["bool " ]["must " ][] = ["match " => [$ item ['column ' ] => $ item ['value ' ]]];
497+ $ body ["query " ]["bool " ]["filter " ][] = ["match " => [$ item ['column ' ] => $ item ['value ' ]]];
491498 }
492499 if ($ item ['operator ' ] == "exists " ) {
493500 if (!$ item ['value ' ]) {
@@ -504,10 +511,10 @@ protected function filters()
504511 $ body ["query " ]["bool " ]["must_not " ][] = ["terms " => [$ item ['column ' ] => $ item ['value ' ]]];
505512 break ;
506513 case 'Raw ' :
507-
514+ $ body [ " query " ] = array_merge_recursive ( $ body [ " query " ] ?? [], $ item [ ' sql ' ]) ;
508515 break ;
509516 case 'Between ' :
510- $ body ["query " ]["bool " ]["filter " ][] = ["range " => [$ item ['column ' ] => ["gte " => $ item ['values ' ][0 ], "lte " => $ item ['values ' ][1 ]]]];
517+ $ body ["query " ]["bool " ]["must " ][] = ["range " => [$ item ['column ' ] => ["gte " => $ item ['values ' ][0 ], "lte " => $ item ['values ' ][1 ]]]];
511518 break ;
512519 case 'NotBetween ' :
513520 $ body ["query " ]["bool " ]["must_not " ][] = ["range " => [$ item ['column ' ] => ["gte " => $ item ['values ' ][0 ], "lte " => $ item ['values ' ][1 ]]]];
@@ -528,9 +535,12 @@ protected function filters()
528535 if (count ($ body ["query " ]) == 0 ){
529536 unset($ body ["query " ]);
530537 }
531- // if(count($this->attributesToHighlight)){
532- // $body["highlight"]['fields'] = $this->attributesToHighlight;
533- // }
538+ if (!empty ($ this ->attributesToHighlight )){
539+ $ body ["highlight " ]['fields ' ] = $ this ->attributesToHighlight ;
540+ }
541+ if (!empty ($ this ->attributesAggregations )){
542+ $ body ['aggs ' ] = $ this ->attributesAggregations ;
543+ }
534544 if (count ($ this ->sort )) {
535545 $ sortFields = array_key_exists ("sort " , $ body ) ? $ body ["sort " ] : [];
536546 $ body ["sort " ] = array_unique (array_merge ($ sortFields , $ this ->sort ), SORT_REGULAR );
0 commit comments