Skip to content

Commit

Permalink
Refactored ResultExtractor::extract method (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Feb 3, 2021
1 parent 5b2baab commit c98d1fd
Showing 1 changed file with 87 additions and 58 deletions.
145 changes: 87 additions & 58 deletions lib/ResultExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use EzSystems\EzPlatformSolrSearchEngine\ResultExtractor\AggregationResultExtractor;
use stdClass;

/**
* Abstract implementation of Search Extractor, which extracts search result
Expand Down Expand Up @@ -62,64 +63,27 @@ public function extract($data, array $facetBuilders = [], array $aggregations =
]
);

if (isset($data->facet_counts)) {
// We'll first need to generate id's for facet builders to match against fields, as also done for
// visit stage in NativeQueryConverter.
$facetBuildersById = [];
foreach ($facetBuilders as $facetBuilder) {
$facetBuildersById[spl_object_hash($facetBuilder)] = $facetBuilder;
}

foreach ($data->facet_counts as $facetCounts) {
foreach ($facetCounts as $field => $facet) {
if (empty($facetBuildersById[$field])) {
@trigger_error(
'Not setting id of field using FacetFieldVisitor::visitBuilder will not be supported in 2.0'
. ', as it makes it impossible to exactly identify which facets belongs to which builder.'
. "\nMake sure to adapt your visitor for the following field: ${field}"
. "\nExample: 'facet.field' => \"{!ex=dt key=\${id}}${field}\",",
E_USER_DEPRECATED);
}

$result->facets[] = $this->facetBuilderVisitor->mapField(
$field,
(array)$facet,
isset($facetBuildersById[$field]) ? $facetBuildersById[$field] : null
);
}
}
}

$aggregationsResults = [];
foreach ($aggregations as $aggregation) {
$name = $aggregation->getName();

if (isset($data->facets->{$name})) {
$aggregationsResults[] = $this->aggregationResultExtractor->extract(
$aggregation,
$languageFilter,
$data->facets->{$name}
);
}
}

$result->aggregations = new AggregationResultCollection($aggregationsResults);
$result->facets = $this->extractFacets($data, $facetBuilders, $languageFilter);
$result->aggregations = $this->extractAggregations($data, $aggregations, $languageFilter);

foreach ($data->response->docs as $doc) {
$searchHit = new SearchHit(
[
'score' => $doc->score,
'index' => $this->getIndexIdentifier($doc),
'matchedTranslation' => $this->getMatchedLanguageCode($doc),
'valueObject' => $this->extractHit($doc),
]
);
$result->searchHits[] = $searchHit;
$result->searchHits[] = $this->extractSearchHit($doc, $languageFilter);
}

return $result;
}

/**
* Extracts value object from $hit returned by Solr backend.
*
* Needs to be implemented by the concrete ResultExtractor.
*
* @param mixed $hit
*
* @return \eZ\Publish\API\Repository\Values\ValueObject
*/
abstract public function extractHit($hit);

/**
* Returns language code of the Content's translation of the matched document.
*
Expand Down Expand Up @@ -151,13 +115,78 @@ protected function getIndexIdentifier($hit)
}

/**
* Extracts value object from $hit returned by Solr backend.
*
* Needs to be implemented by the concrete ResultExtractor.
*
* @param mixed $hit
* @param \eZ\Publish\API\Repository\Values\Content\Query\Aggregation[] $aggregations
*/
protected function extractAggregations(
stdClass $data,
array $aggregations,
array $languageFilter
): AggregationResultCollection {
$aggregationsResults = [];
foreach ($aggregations as $aggregation) {
$name = $aggregation->getName();

if (isset($data->facets->{$name})) {
$aggregationsResults[] = $this->aggregationResultExtractor->extract(
$aggregation,
$languageFilter,
$data->facets->{$name}
);
}
}

return new AggregationResultCollection($aggregationsResults);
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder[] $facetBuilders
*
* @return \eZ\Publish\API\Repository\Values\ValueObject
* @return \eZ\Publish\API\Repository\Values\Content\Search\Facet[]
*/
abstract public function extractHit($hit);
protected function extractFacets(stdClass $data, array $facetBuilders, array $languageFilter): array
{
$facets = [];

if (isset($data->facet_counts)) {
// We'll first need to generate id's for facet builders to match against fields, as also done for
// visit stage in NativeQueryConverter.
$facetBuildersById = [];
foreach ($facetBuilders as $facetBuilder) {
$facetBuildersById[spl_object_hash($facetBuilder)] = $facetBuilder;
}

foreach ($data->facet_counts as $facetCounts) {
foreach ($facetCounts as $field => $facet) {
if (empty($facetBuildersById[$field])) {
@trigger_error(
'Not setting id of field using FacetFieldVisitor::visitBuilder will not be supported in 4.0'
. ', as it makes it impossible to exactly identify which facets belongs to which builder.'
. "\nMake sure to adapt your visitor for the following field: ${field}"
. "\nExample: 'facet.field' => \"{!ex=dt key=\${id}}${field}\",",
E_USER_DEPRECATED);
}

$facets[] = $this->facetBuilderVisitor->mapField(
$field,
(array)$facet,
$facetBuildersById[$field] ?? null
);
}
}
}

return $facets;
}

protected function extractSearchHit(stdClass $doc, array $languageFilter): SearchHit
{
return new SearchHit(
[
'score' => $doc->score,
'index' => $this->getIndexIdentifier($doc),
'matchedTranslation' => $this->getMatchedLanguageCode($doc),
'valueObject' => $this->extractHit($doc),
]
);
}
}

0 comments on commit c98d1fd

Please sign in to comment.