Skip to content

Commit b49738a

Browse files
committed
TASK: Extract result evaluation
1 parent 2fe22e7 commit b49738a

2 files changed

Lines changed: 71 additions & 12 deletions

File tree

Classes/Dto/SearchResult.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Dto;
3+
4+
/*
5+
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
6+
*
7+
* (c) Contributors of the Neos Project - www.neos.io
8+
*
9+
* This package is Open Source Software. For the full copyright and license
10+
* information, please view the LICENSE file which was distributed with this
11+
* source code.
12+
*/
13+
14+
class SearchResult
15+
{
16+
/**
17+
* @var int
18+
*/
19+
protected $total;
20+
21+
/**
22+
* @var array
23+
*/
24+
protected $hits;
25+
26+
public function __construct(array $hits, int $total)
27+
{
28+
$this->hits = $hits;
29+
$this->total = $total;
30+
}
31+
32+
/**
33+
* @return int
34+
*/
35+
public function getTotal(): int
36+
{
37+
return $this->total;
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getHits(): array
44+
{
45+
return $this->hits;
46+
}
47+
}

Classes/Eel/ElasticSearchQueryBuilder.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\QueryInterface;
15+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Dto\SearchResult;
1516
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
1617
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
1718
use 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

Comments
 (0)