-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathBuilder.php
295 lines (260 loc) · 11.2 KB
/
Builder.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCore\Search\Request;
use Magento\Framework\Search\Request\DimensionFactory;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfiguration\AggregationResolverInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\Builder as QueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\SortOrderBuilder;
use Smile\ElasticsuiteCore\Search\Request\Aggregation\AggregationBuilder;
use Smile\ElasticsuiteCore\Search\RequestInterface;
use Smile\ElasticsuiteCore\Search\RequestFactory;
use Magento\Framework\Search\Request\Dimension;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteCore\Api\Search\Spellchecker\RequestInterfaceFactory as SpellcheckRequestFactory;
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
/**
* ElasticSuite search requests builder.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
*/
class Builder
{
/**
* @var ContainerConfigurationFactory
*/
private $containerConfigFactory;
/**
* @var QueryBuilder
*/
private $queryBuilder;
/**
* @var SortOrderBuilder
*/
private $sortOrderBuilder;
/**
* @var AggregationBuilder
*/
private $aggregationBuilder;
/**
* @var RequestFactory
*/
private $requestFactory;
/**
* @var SpellcheckRequestFactory
*/
private $spellcheckRequestFactory;
/**
* @var SpellcheckerInterface
*/
private $spellchecker;
/**
* @var DimensionFactory
*/
private $dimensionFactory;
/**
* @var \Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfiguration\AggregationResolverInterface
*/
private $aggregationResolver;
/**
* Constructor.
*
* @param RequestFactory $requestFactory Factory used to build the request.
* @param DimensionFactory $dimensionFactory Factory used to dimensions of the request.
* @param QueryBuilder $queryBuilder Builder for the query part of the request.
* @param SortOrderBuilder $sortOrderBuilder Builder for the sort part of the request.
* @param AggregationBuilder $aggregationBuilder Builder for the aggregation part of the request.
* @param ContainerConfigurationFactory $containerConfigFactory Search requests configuration.
* @param SpellcheckRequestFactory $spellcheckRequestFactory Spellchecking request factory.
* @param SpellcheckerInterface $spellchecker Spellchecker.
* @param AggregationResolverInterface $aggregationResolver Aggregation Resolver.
*/
public function __construct(
RequestFactory $requestFactory,
DimensionFactory $dimensionFactory,
QueryBuilder $queryBuilder,
SortOrderBuilder $sortOrderBuilder,
AggregationBuilder $aggregationBuilder,
ContainerConfigurationFactory $containerConfigFactory,
SpellcheckRequestFactory $spellcheckRequestFactory,
SpellcheckerInterface $spellchecker,
AggregationResolverInterface $aggregationResolver
) {
$this->spellcheckRequestFactory = $spellcheckRequestFactory;
$this->spellchecker = $spellchecker;
$this->requestFactory = $requestFactory;
$this->dimensionFactory = $dimensionFactory;
$this->queryBuilder = $queryBuilder;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->aggregationBuilder = $aggregationBuilder;
$this->containerConfigFactory = $containerConfigFactory;
$this->aggregationResolver = $aggregationResolver;
}
/**
* Create a new search request.
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param integer $storeId Search request store id.
* @param string $containerName Search request name.
* @param integer $from Search request pagination from clause.
* @param integer $size Search request pagination size.
* @param string|QueryInterface $query Search request query.
* @param array $sortOrders Search request sort orders.
* @param array $filters Search request filters.
* @param QueryInterface[] $queryFilters Search request filters prebuilt as QueryInterface.
* @param array $facets Search request facets.
* @param bool|int|null $trackTotalHits If total hits should be tracked.
*
* @return RequestInterface
*/
public function create(
$storeId,
$containerName,
$from,
$size,
$query = null,
$sortOrders = [],
$filters = [],
$queryFilters = [],
$facets = [],
$trackTotalHits = null
) {
$containerConfig = $this->getRequestContainerConfiguration($storeId, $containerName);
$containerFilters = $this->getContainerFilters($containerConfig);
$containerAggs = [];
// If "track_total_hits" is explicitely true, we are just "counting" the result, and we do not care about the aggregations.
if ($trackTotalHits !== true) {
$containerAggs = $this->getContainerAggregations($containerConfig, $query, $filters, $queryFilters);
}
$facets = array_merge($facets, $containerAggs);
$facetFilters = array_intersect_key($filters, $facets);
$queryFilters = array_merge($queryFilters, $containerFilters, array_diff_key($filters, $facetFilters));
$spellingType = SpellcheckerInterface::SPELLING_TYPE_EXACT;
if ($query && is_string($query)) {
$spellingType = $this->getSpellingType($containerConfig, $query);
}
if (null === $trackTotalHits) {
$trackTotalHits = $containerConfig->getTrackTotalHits();
}
$requestParams = [
'name' => $containerName,
'indexName' => $containerConfig->getIndexName(),
'from' => $from,
'size' => $size,
'dimensions' => $this->buildDimensions($storeId),
'query' => $this->queryBuilder->createQuery($containerConfig, $query, $queryFilters, $spellingType),
'sortOrders' => $this->sortOrderBuilder->buildSordOrders($containerConfig, $sortOrders),
'buckets' => $this->aggregationBuilder->buildAggregations($containerConfig, $facets, $facetFilters),
'spellingType' => $spellingType,
'trackTotalHits' => $trackTotalHits,
];
// Use min_score only for fulltext queries.
if ($query !== null) {
$requestParams['minScore'] = $containerConfig->getRelevanceConfig()->getMinScore();
}
if (!empty($facetFilters)) {
$requestParams['filter'] = $this->queryBuilder->createFilterQuery($containerConfig, $facetFilters);
}
$request = $this->requestFactory->create($requestParams);
return $request;
}
/**
* Returns search request applied to each request for a given search container.
*
* @param ContainerConfigurationInterface $containerConfig Search request configuration.
*
* @return \Smile\ElasticsuiteCore\Search\Request\QueryInterface[]
*/
private function getContainerFilters(ContainerConfigurationInterface $containerConfig)
{
return $containerConfig->getFilters();
}
/**
* Returns aggregations configured in the search container.
*
* @param ContainerConfigurationInterface $containerConfig Search request configuration.
* @param string|QueryInterface $query Search Query.
* @param array $filters Search request filters.
* @param QueryInterface[] $queryFilters Search request filters prebuilt as QueryInterface.
*
* @return array
*/
private function getContainerAggregations(ContainerConfigurationInterface $containerConfig, $query, $filters, $queryFilters)
{
return $this->aggregationResolver->getContainerAggregations($containerConfig, $query, $filters, $queryFilters);
}
/**
* Retireve the spelling type for a fulltext query.
*
* @param ContainerConfigurationInterface $containerConfig Search request configuration.
* @param string|string[] $queryText Query text.
*
* @return int
*/
private function getSpellingType(ContainerConfigurationInterface $containerConfig, $queryText)
{
if (is_array($queryText)) {
$queryText = implode(" ", $queryText);
}
$spellcheckRequestParams = [
'index' => $containerConfig->getIndexName(),
'queryText' => $queryText,
'cutoffFrequency' => $containerConfig->getRelevanceConfig()->getCutOffFrequency(),
'isUsingAllTokens' => $containerConfig->getRelevanceConfig()->isUsingAllTokens(),
'isUsingReference' => $containerConfig->getRelevanceConfig()->isUsingReferenceAnalyzer(),
'isUsingEdgeNgram' => $containerConfig->getRelevanceConfig()->isUsingEdgeNgramAnalyzer(),
];
$spellcheckRequest = $this->spellcheckRequestFactory->create($spellcheckRequestParams);
$spellingType = $this->spellchecker->getSpellingType($spellcheckRequest);
return $spellingType;
}
/**
* Load the search request configuration (index, type, mapping, ...) using the search request container name.
*
* @throws \LogicException Thrown when the search container is not found into the configuration.
*
* @param integer $storeId Store id.
* @param string $containerName Search request container name.
*
* @return ContainerConfigurationInterface
*/
private function getRequestContainerConfiguration($storeId, $containerName)
{
if ($containerName === null) {
throw new \LogicException('Request name is not set');
}
$config = $this->containerConfigFactory->create(
['containerName' => $containerName, 'storeId' => $storeId]
);
if ($config === null) {
throw new \LogicException("No configuration exists for request {$containerName}");
}
return $config;
}
/**
* Build a dimenstion object from
* It is quite useless since we have a per store index but required by the RequestInterface specification.
*
* @param integer $storeId Store id.
*
* @return Dimension[]
*/
private function buildDimensions($storeId)
{
$dimensions = ['scope' => $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId])];
return $dimensions;
}
}