22
33namespace PDPhilip \Elasticsearch \DSL ;
44
5+ use Elastic \Elasticsearch \Exception \ClientResponseException ;
6+ use Elastic \Elasticsearch \Exception \MissingParameterException ;
7+ use Elastic \Elasticsearch \Exception \ServerResponseException ;
58use Exception ;
69use Elastic \Elasticsearch \Client ;
710
@@ -87,6 +90,7 @@ public function processIndicesDsl($method, $params)
8790 */
8891 public function processFind ($ wheres , $ options , $ columns )
8992 {
93+
9094 $ params = $ this ->buildParams ($ this ->index , $ wheres , $ options , $ columns );
9195
9296 return $ this ->_returnSearch ($ params , __FUNCTION__ );
@@ -97,9 +101,8 @@ public function processFind($wheres, $options, $columns)
97101 */
98102 public function processSearch ($ searchParams , $ searchOptions , $ wheres , $ opts , $ fields , $ cols )
99103 {
104+
100105 $ params = $ this ->buildSearchParams ($ this ->index , $ searchParams , $ searchOptions , $ wheres , $ opts , $ fields , $ cols );
101-
102- // dd($params);
103106
104107 return $ this ->_returnSearch ($ params , __FUNCTION__ );
105108
@@ -114,10 +117,11 @@ protected function _returnSearch($params, $source)
114117 $ process = $ this ->client ->search ($ params );
115118
116119 return $ this ->_sanitizeSearchResponse ($ process , $ params , $ this ->_queryTag ($ source ));
120+
117121 } catch (Exception $ e ) {
118122
119- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
120- throw new Exception ($ error ->errorMessage );
123+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
124+ throw new Exception ($ result ->errorMessage );
121125 }
122126 }
123127
@@ -147,8 +151,8 @@ public function processDistinct($column, $wheres)
147151 return $ this ->_return ($ data , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
148152 } catch (Exception $ e ) {
149153
150- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
151- throw new Exception ($ error ->errorMessage );
154+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
155+ throw new Exception ($ result ->errorMessage );
152156 }
153157
154158
@@ -190,15 +194,15 @@ public function processSave($data, $refresh)
190194 if ($ refresh ) {
191195 $ params ['refresh ' ] = $ refresh ;
192196 }
197+
193198 try {
194199 $ response = $ this ->client ->index ($ params );
195-
196200 $ savedData = ['_id ' => $ response ['_id ' ]] + $ data ;
197201
198202 return $ this ->_return ($ savedData , $ response , $ params , $ this ->_queryTag (__FUNCTION__ ));
199203 } catch (Exception $ e ) {
200- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
201- throw new Exception ($ error ->errorMessage );
204+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
205+ throw new Exception ($ result ->errorMessage );
202206 }
203207
204208
@@ -346,18 +350,34 @@ public function processScript($id, $script)
346350 // Index administration
347351 //----------------------------------------------------------------------
348352
349- public function processGetIndices ($ all )
353+ /**
354+ * @throws ClientResponseException
355+ * @throws ServerResponseException
356+ * @throws MissingParameterException
357+ */
358+ public function processGetIndices ($ all ): array
350359 {
351- $ response = $ this ->client ->cat ()->indices ();
360+ $ index = $ this ->index ;
361+ if ($ all ) {
362+ $ index = '* ' ;
363+ }
364+ $ response = $ this ->client ->indices ()->get (['index ' => $ index ]);
352365
353- return $ this -> catIndices ( $ response , $ all );
366+ return $ response -> asArray ( );
354367 }
355368
356- public function processIndexExists ($ index )
369+ public function processIndexExists ($ index ): bool
357370 {
358371 $ params = ['index ' => $ index ];
359372
360- return $ this ->client ->indices ()->exists ($ params );
373+ try {
374+ $ test = $ this ->client ->indices ()->exists ($ params );
375+
376+ return $ test ->getStatusCode () == 200 ;
377+ } catch (Exception $ e ) {
378+ return false ;
379+ }
380+
361381 }
362382
363383 /**
@@ -385,11 +405,10 @@ public function processIndexSettings($index)
385405 {
386406 $ params = ['index ' => $ index ];
387407 try {
388- $ responseObject = $ this ->client ->indices ()->getSettings ($ params );
389- $ response = $ responseObject ->asArray ();
408+ $ response = $ this ->client ->indices ()->getSettings ($ params );
390409 $ result = $ this ->_return ($ response , $ response , $ params , $ this ->_queryTag (__FUNCTION__ ));
391410
392- return $ result ->data ;
411+ return $ result ->data -> asArray () ;
393412 } catch (Exception $ e ) {
394413 $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
395414 throw new Exception ($ result ->errorMessage );
@@ -401,15 +420,15 @@ public function processIndexSettings($index)
401420 */
402421 public function processIndexCreate ($ settings )
403422 {
423+ $ params = $ this ->buildIndexMap ($ this ->index , $ settings );
404424 try {
405- $ params = $ this ->buildIndexMap ($ this ->index , $ settings );
406425 $ response = $ this ->client ->indices ()->create ($ params );
407426
408427 $ result = $ this ->_return (true , $ response , $ params , $ this ->_queryTag (__FUNCTION__ ));
409428
410429 return true ;
411430 } catch (Exception $ e ) {
412- $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
431+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [] , $ this ->_queryTag (__FUNCTION__ ));
413432 throw new Exception ($ result ->errorMessage );
414433 }
415434
@@ -421,7 +440,6 @@ public function processIndexCreate($settings)
421440 public function processIndexDelete ()
422441 {
423442 $ params = ['index ' => $ this ->index ];
424-
425443 try {
426444 $ response = $ this ->client ->indices ()->delete ($ params );
427445 $ this ->_return (true , $ response , $ params , $ this ->_queryTag (__FUNCTION__ ));
@@ -462,15 +480,32 @@ public function processIndexModify($settings)
462480 /**
463481 * @throws Exception
464482 */
465- public function processReIndex ($ newIndex , $ oldIndex )
483+ public function processReIndex ($ oldIndex , $ newIndex )
466484 {
467- $ params ['source ' ]['index ' ] = $ oldIndex ;
468- $ params ['dest ' ]['index ' ] = $ newIndex ;
485+ $ prefix = str_replace ('* ' , '' , $ this ->index );
486+ if ($ prefix ) {
487+ $ oldIndex = $ prefix .'_ ' .$ oldIndex ;
488+ $ newIndex = $ prefix .'_ ' .$ newIndex ;
489+ }
490+ $ params ['body ' ]['source ' ]['index ' ] = $ oldIndex ;
491+ $ params ['body ' ]['dest ' ]['index ' ] = $ newIndex ;
469492 try {
470493 $ response = $ this ->client ->reindex ($ params );
471- $ result = $ this ->_return (true , $ response , $ params , $ this ->_queryTag (__FUNCTION__ ));
494+ $ result = $ response ->asArray ();
495+ $ resultData = [
496+ 'took ' => $ result ['took ' ],
497+ 'total ' => $ result ['total ' ],
498+ 'created ' => $ result ['created ' ],
499+ 'updated ' => $ result ['updated ' ],
500+ 'deleted ' => $ result ['deleted ' ],
501+ 'batches ' => $ result ['batches ' ],
502+ 'version_conflicts ' => $ result ['version_conflicts ' ],
503+ 'noops ' => $ result ['noops ' ],
504+ 'retries ' => $ result ['retries ' ],
505+ ];
506+
507+ return $ this ->_return ($ resultData , $ result , $ params , $ this ->_queryTag (__FUNCTION__ ));
472508
473- return true ;
474509 } catch (Exception $ e ) {
475510 $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), $ params , $ this ->_queryTag (__FUNCTION__ ));
476511 throw new Exception ($ result ->errorMessage );
@@ -525,8 +560,8 @@ public function _countAggregate($wheres, $options, $columns)
525560 return $ this ->_return ($ process ['count ' ] ?? 0 , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
526561 } catch (Exception $ e ) {
527562
528- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
529- throw new Exception ($ error ->errorMessage );
563+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
564+ throw new Exception ($ result ->errorMessage );
530565 }
531566
532567 }
@@ -541,8 +576,8 @@ private function _maxAggregate($wheres, $options, $columns)
541576 return $ this ->_return ($ process ['aggregations ' ]['max_value ' ]['value ' ] ?? 0 , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
542577 } catch (Exception $ e ) {
543578
544- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
545- throw new Exception ($ error ->errorMessage );
579+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
580+ throw new Exception ($ result ->errorMessage );
546581 }
547582 }
548583
@@ -555,8 +590,8 @@ private function _minAggregate($wheres, $options, $columns)
555590
556591 return $ this ->_return ($ process ['aggregations ' ]['min_value ' ]['value ' ] ?? 0 , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
557592 } catch (Exception $ e ) {
558- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
559- throw new Exception ($ error ->errorMessage );
593+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
594+ throw new Exception ($ result ->errorMessage );
560595 }
561596 }
562597
@@ -571,8 +606,8 @@ private function _sumAggregate($wheres, $options, $columns)
571606 return $ this ->_return ($ process ['aggregations ' ]['sum_value ' ]['value ' ] ?? 0 , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
572607 } catch (Exception $ e ) {
573608
574- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
575- throw new Exception ($ error ->errorMessage );
609+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
610+ throw new Exception ($ result ->errorMessage );
576611 }
577612
578613 }
@@ -586,8 +621,8 @@ private function _avgAggregate($wheres, $options, $columns)
586621
587622 return $ this ->_return ($ process ['aggregations ' ]['avg_value ' ]['value ' ] ?? 0 , $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
588623 } catch (Exception $ e ) {
589- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
590- throw new Exception ($ error ->errorMessage );
624+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
625+ throw new Exception ($ result ->errorMessage );
591626 }
592627 }
593628
@@ -600,8 +635,8 @@ private function _matrixAggregate($wheres, $options, $columns)
600635
601636 return $ this ->_return ($ process ['aggregations ' ]['statistics ' ] ?? [], $ process , $ params , $ this ->_queryTag (__FUNCTION__ ));
602637 } catch (Exception $ e ) {
603- $ error = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
604- throw new Exception ($ error ->errorMessage );
638+ $ result = $ this ->_returnError ($ e ->getMessage (), $ e ->getCode (), [], $ this ->_queryTag (__FUNCTION__ ));
639+ throw new Exception ($ result ->errorMessage );
605640 }
606641
607642 }
@@ -651,7 +686,6 @@ private function _return($data, $meta, $params, $queryTag)
651686 return $ results ;
652687 }
653688
654-
655689 private function _returnError ($ errorMsg , $ errorCode , $ params , $ queryTag )
656690 {
657691 $ error = new Results ([], [], $ params , $ queryTag );
0 commit comments