-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathResultExtractor.php
215 lines (187 loc) · 7.48 KB
/
ResultExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Solr;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Spellcheck;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResultCollection;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SpellcheckResult;
use Ibexa\Contracts\Solr\ResultExtractor\AggregationResultExtractor;
use Ibexa\Solr\Gateway\EndpointRegistry;
use Ibexa\Solr\Query\FacetFieldVisitor;
use stdClass;
/**
* Abstract implementation of Search Extractor, which extracts search result
* from the data returned by Solr backend.
*/
abstract class ResultExtractor
{
/** @var \Ibexa\Solr\Query\FacetFieldVisitor */
protected $facetBuilderVisitor;
/** @var \Ibexa\Contracts\Solr\ResultExtractor\AggregationResultExtractor */
protected $aggregationResultExtractor;
/** @var \Ibexa\Solr\Gateway\EndpointRegistry */
protected $endpointRegistry;
public function __construct(
FacetFieldVisitor $facetBuilderVisitor,
AggregationResultExtractor $aggregationResultExtractor,
EndpointRegistry $endpointRegistry
) {
$this->facetBuilderVisitor = $facetBuilderVisitor;
$this->aggregationResultExtractor = $aggregationResultExtractor;
$this->endpointRegistry = $endpointRegistry;
}
/**
* Extracts search result from $data returned by Solr backend.
*
* @param mixed $data
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\FacetBuilder[] $facetBuilders
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation[] $aggregations
* @param array $languageFilter
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult
*/
public function extract(
$data,
array $facetBuilders = [],
array $aggregations = [],
array $languageFilter = [],
?Spellcheck $spellcheck = null
) {
$result = new SearchResult(
[
'time' => $data->responseHeader->QTime / 1000,
'maxScore' => $data->response->maxScore,
'totalCount' => $data->response->numFound,
]
);
$result->facets = $this->extractFacets($data, $facetBuilders, $languageFilter);
$result->aggregations = $this->extractAggregations($data, $aggregations, $languageFilter);
$result->spellcheck = $this->extractSpellcheck($data, $spellcheck);
foreach ($data->response->docs as $doc) {
$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 \Ibexa\Contracts\Core\Repository\Values\ValueObject
*/
abstract public function extractHit($hit);
/**
* Returns language code of the Content's translation of the matched document.
*
* @param mixed $hit
*
* @return string
*/
protected function getMatchedLanguageCode($hit)
{
return $hit->meta_indexed_language_code_s;
}
/**
* Returns the identifier of the logical index (shard) of the matched document.
*
* @param mixed $hit
*
* @return string
*/
protected function getIndexIdentifier($hit)
{
// In single core setup, shard parameter is not set on request to avoid issues in environments that does not
// know about own dns, which means it's not set here either
if ($hit->{'[shard]'} === '[not a shard request]') {
return $this->endpointRegistry->getFirstEndpoint()->getIdentifier();
}
return $hit->{'[shard]'};
}
/**
* @param \Ibexa\Contracts\Core\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 \Ibexa\Contracts\Core\Repository\Values\Content\Query\FacetBuilder[] $facetBuilders
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Search\Facet[]
*/
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),
]
);
}
protected function extractSpellcheck(stdClass $data, ?Spellcheck $spellcheck): ?SpellcheckResult
{
if ($spellcheck === null) {
return null;
}
if (isset($data->spellcheck)) {
$incorrect = !empty($data->spellcheck->collations);
$query = $data->spellcheck->collations[1] ?? $spellcheck->getQuery();
return new SpellcheckResult($query, $incorrect);
}
return new SpellcheckResult($spellcheck->getQuery(), false);
}
}
class_alias(ResultExtractor::class, 'EzSystems\EzPlatformSolrSearchEngine\ResultExtractor');