Skip to content

Commit c98d1fd

Browse files
authored
Refactored ResultExtractor::extract method (#208)
1 parent 5b2baab commit c98d1fd

File tree

1 file changed

+87
-58
lines changed

1 file changed

+87
-58
lines changed

lib/ResultExtractor.php

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
1717
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
1818
use EzSystems\EzPlatformSolrSearchEngine\ResultExtractor\AggregationResultExtractor;
19+
use stdClass;
1920

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

65-
if (isset($data->facet_counts)) {
66-
// We'll first need to generate id's for facet builders to match against fields, as also done for
67-
// visit stage in NativeQueryConverter.
68-
$facetBuildersById = [];
69-
foreach ($facetBuilders as $facetBuilder) {
70-
$facetBuildersById[spl_object_hash($facetBuilder)] = $facetBuilder;
71-
}
72-
73-
foreach ($data->facet_counts as $facetCounts) {
74-
foreach ($facetCounts as $field => $facet) {
75-
if (empty($facetBuildersById[$field])) {
76-
@trigger_error(
77-
'Not setting id of field using FacetFieldVisitor::visitBuilder will not be supported in 2.0'
78-
. ', as it makes it impossible to exactly identify which facets belongs to which builder.'
79-
. "\nMake sure to adapt your visitor for the following field: ${field}"
80-
. "\nExample: 'facet.field' => \"{!ex=dt key=\${id}}${field}\",",
81-
E_USER_DEPRECATED);
82-
}
83-
84-
$result->facets[] = $this->facetBuilderVisitor->mapField(
85-
$field,
86-
(array)$facet,
87-
isset($facetBuildersById[$field]) ? $facetBuildersById[$field] : null
88-
);
89-
}
90-
}
91-
}
92-
93-
$aggregationsResults = [];
94-
foreach ($aggregations as $aggregation) {
95-
$name = $aggregation->getName();
96-
97-
if (isset($data->facets->{$name})) {
98-
$aggregationsResults[] = $this->aggregationResultExtractor->extract(
99-
$aggregation,
100-
$languageFilter,
101-
$data->facets->{$name}
102-
);
103-
}
104-
}
105-
106-
$result->aggregations = new AggregationResultCollection($aggregationsResults);
66+
$result->facets = $this->extractFacets($data, $facetBuilders, $languageFilter);
67+
$result->aggregations = $this->extractAggregations($data, $aggregations, $languageFilter);
10768

10869
foreach ($data->response->docs as $doc) {
109-
$searchHit = new SearchHit(
110-
[
111-
'score' => $doc->score,
112-
'index' => $this->getIndexIdentifier($doc),
113-
'matchedTranslation' => $this->getMatchedLanguageCode($doc),
114-
'valueObject' => $this->extractHit($doc),
115-
]
116-
);
117-
$result->searchHits[] = $searchHit;
70+
$result->searchHits[] = $this->extractSearchHit($doc, $languageFilter);
11871
}
11972

12073
return $result;
12174
}
12275

76+
/**
77+
* Extracts value object from $hit returned by Solr backend.
78+
*
79+
* Needs to be implemented by the concrete ResultExtractor.
80+
*
81+
* @param mixed $hit
82+
*
83+
* @return \eZ\Publish\API\Repository\Values\ValueObject
84+
*/
85+
abstract public function extractHit($hit);
86+
12387
/**
12488
* Returns language code of the Content's translation of the matched document.
12589
*
@@ -151,13 +115,78 @@ protected function getIndexIdentifier($hit)
151115
}
152116

153117
/**
154-
* Extracts value object from $hit returned by Solr backend.
155-
*
156-
* Needs to be implemented by the concrete ResultExtractor.
157-
*
158-
* @param mixed $hit
118+
* @param \eZ\Publish\API\Repository\Values\Content\Query\Aggregation[] $aggregations
119+
*/
120+
protected function extractAggregations(
121+
stdClass $data,
122+
array $aggregations,
123+
array $languageFilter
124+
): AggregationResultCollection {
125+
$aggregationsResults = [];
126+
foreach ($aggregations as $aggregation) {
127+
$name = $aggregation->getName();
128+
129+
if (isset($data->facets->{$name})) {
130+
$aggregationsResults[] = $this->aggregationResultExtractor->extract(
131+
$aggregation,
132+
$languageFilter,
133+
$data->facets->{$name}
134+
);
135+
}
136+
}
137+
138+
return new AggregationResultCollection($aggregationsResults);
139+
}
140+
141+
/**
142+
* @param \eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder[] $facetBuilders
159143
*
160-
* @return \eZ\Publish\API\Repository\Values\ValueObject
144+
* @return \eZ\Publish\API\Repository\Values\Content\Search\Facet[]
161145
*/
162-
abstract public function extractHit($hit);
146+
protected function extractFacets(stdClass $data, array $facetBuilders, array $languageFilter): array
147+
{
148+
$facets = [];
149+
150+
if (isset($data->facet_counts)) {
151+
// We'll first need to generate id's for facet builders to match against fields, as also done for
152+
// visit stage in NativeQueryConverter.
153+
$facetBuildersById = [];
154+
foreach ($facetBuilders as $facetBuilder) {
155+
$facetBuildersById[spl_object_hash($facetBuilder)] = $facetBuilder;
156+
}
157+
158+
foreach ($data->facet_counts as $facetCounts) {
159+
foreach ($facetCounts as $field => $facet) {
160+
if (empty($facetBuildersById[$field])) {
161+
@trigger_error(
162+
'Not setting id of field using FacetFieldVisitor::visitBuilder will not be supported in 4.0'
163+
. ', as it makes it impossible to exactly identify which facets belongs to which builder.'
164+
. "\nMake sure to adapt your visitor for the following field: ${field}"
165+
. "\nExample: 'facet.field' => \"{!ex=dt key=\${id}}${field}\",",
166+
E_USER_DEPRECATED);
167+
}
168+
169+
$facets[] = $this->facetBuilderVisitor->mapField(
170+
$field,
171+
(array)$facet,
172+
$facetBuildersById[$field] ?? null
173+
);
174+
}
175+
}
176+
}
177+
178+
return $facets;
179+
}
180+
181+
protected function extractSearchHit(stdClass $doc, array $languageFilter): SearchHit
182+
{
183+
return new SearchHit(
184+
[
185+
'score' => $doc->score,
186+
'index' => $this->getIndexIdentifier($doc),
187+
'matchedTranslation' => $this->getMatchedLanguageCode($doc),
188+
'valueObject' => $this->extractHit($doc),
189+
]
190+
);
191+
}
163192
}

0 commit comments

Comments
 (0)