1212 */
1313
1414use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Driver \QueryInterface ;
15+ use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Dto \SearchResult ;
1516use Flowpack \ElasticSearch \ContentRepositoryAdaptor \ElasticSearchClient ;
1617use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Exception ;
1718use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Exception \QueryBuildingException ;
@@ -515,15 +516,11 @@ public function log($message = null)
515516 }
516517
517518 /**
518- * @return integer
519+ * @return int
519520 */
520- public function getTotalItems ()
521+ public function getTotalItems (): int
521522 {
522- if (isset ($ this ->result ['hits ' ]['total ' ])) {
523- return (int )$ this ->result ['hits ' ]['total ' ];
524- }
525-
526- return 0 ;
523+ return $ this ->evaluateResult ($ this ->result )->getTotal ();
527524 }
528525
529526 /**
@@ -574,15 +571,18 @@ public function fetch()
574571 $ timeAfterwards = microtime (true );
575572
576573 $ this ->result = $ response ->getTreatedContent ();
574+ $ searchResult = $ this ->evaluateResult ($ this ->result );
577575
578576 $ this ->result ['nodes ' ] = [];
579577 if ($ this ->logThisQuery === true ) {
580578 $ this ->logger ->log (sprintf ('Query Log (%s): %s -- execution time: %s ms -- Limit: %s -- Number of results returned: %s -- Total Results: %s ' ,
581- $ this ->logMessage , $ request , (($ timeAfterwards - $ timeBefore ) * 1000 ), $ this ->limit , count ($ this -> result [ ' hits ' ][ ' hits ' ]) , $ this -> result [ ' hits ' ][ ' total ' ] ), LOG_DEBUG );
579+ $ this ->logMessage , $ request , (($ timeAfterwards - $ timeBefore ) * 1000 ), $ this ->limit , count ($ searchResult -> getHits ()) , $ searchResult -> getTotal () ), LOG_DEBUG );
582580 }
583- if (array_key_exists ('hits ' , $ this ->result ) && is_array ($ this ->result ['hits ' ]) && count ($ this ->result ['hits ' ]) > 0 ) {
584- $ this ->result ['nodes ' ] = $ this ->convertHitsToNodes ($ this ->result ['hits ' ]);
581+
582+ if (count ($ searchResult ->getHits ()) > 0 ) {
583+ $ this ->result ['nodes ' ] = $ this ->convertHitsToNodes ($ searchResult ->getHits ());
585584 }
585+
586586 } catch (ApiException $ exception ) {
587587 $ this ->logger ->logException ($ exception );
588588 $ this ->result ['nodes ' ] = [];
@@ -591,6 +591,18 @@ public function fetch()
591591 return $ this ->result ;
592592 }
593593
594+ /**
595+ * @param array $result
596+ * @return SearchResult
597+ */
598+ protected function evaluateResult (array $ result ): SearchResult
599+ {
600+ return new SearchResult (
601+ $ hits = $ result ['hits ' ]['hits ' ] ?? [],
602+ $ total = $ result ['hits ' ]['total ' ] ?? 0
603+ );
604+ }
605+
594606 /**
595607 * Get a query result object for lazy execution of the query
596608 *
@@ -734,7 +746,7 @@ public function allowsCallOfMethod($methodName)
734746 * @param array $hits
735747 * @return array Array of Node objects
736748 */
737- protected function convertHitsToNodes (array $ hits )
749+ protected function convertHitsToNodes (array $ hits ): array
738750 {
739751 $ nodes = [];
740752 $ elasticSearchHitPerNode = [];
@@ -753,7 +765,7 @@ protected function convertHitsToNodes(array $hits)
753765 * which *do exist in the user workspace but do NOT match the current query*. This has to be done somehow "recursively"; and later
754766 * we might be able to use https://github.com/elasticsearch/elasticsearch/issues/3300 as soon as it is merged.
755767 */
756- foreach ($ hits[ ' hits ' ] as $ hit ) {
768+ foreach ($ hits as $ hit ) {
757769 $ nodePath = $ hit [isset ($ hit ['fields ' ]) ? 'fields ' : '_source ' ]['__path ' ];
758770 if (is_array ($ nodePath )) {
759771 $ nodePath = current ($ nodePath );
0 commit comments