forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
242 lines (211 loc) · 5.9 KB
/
Request.php
File metadata and controls
242 lines (211 loc) · 5.9 KB
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
<?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 <aurelien.foucret@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCore\Search;
use Magento\Framework\Search\Request\Dimension;
use Magento\Framework\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
use Smile\ElasticsuiteCore\Search\Request\CollapseInterface;
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;
/**
* Default implementation of ElasticSuite search request.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class Request extends \Magento\Framework\Search\Request implements RequestInterface
{
/**
* @var SortOrderInterface
*/
private $sortOrders;
/**
* @var QueryInterface
*/
private $filter;
/**
* @var CollapseInterface
*/
private $collapse;
/**
* @var integer
*/
private $spellingType = SpellcheckerInterface::SPELLING_TYPE_EXACT;
/**
* @var boolean|integer
*/
private $trackTotalHits = \Smile\ElasticsuiteCore\Helper\IndexSettings::PER_SHARD_MAX_RESULT_WINDOW;
/**
* @var boolean|integer
*/
private $minScore;
/**
* @var array
*/
private $sourceConfig = [];
/**
* Constructor.
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param string $name Search request name.
* @param string $indexName Index name.
* @param QueryInterface $query Search query.
* @param QueryInterface $filter Search filter.
* @param SortOrderInterface[] $sortOrders Sort orders specification.
* @param int|null $from Pagination from clause.
* @param int|null $size Pagination page size clause.
* @param Dimension[] $dimensions Searched store.
* @param BucketInterface[] $buckets Search request aggregations definition.
* @param string $spellingType For fulltext query : the type of spellchecked applied.
* @param bool|int $trackTotalHits Value of the 'track_total_hits' ES parameter.
* @param bool|int $minScore Value of the 'min_score' ES parameter.
*/
public function __construct(
$name,
$indexName,
QueryInterface $query,
?QueryInterface $filter = null,
?array $sortOrders = null,
$from = null,
$size = null,
array $dimensions = [],
array $buckets = [],
$spellingType = null,
$trackTotalHits = null,
$minScore = null
) {
parent::__construct($name, $indexName, $query, $from, $size, $dimensions, $buckets);
$this->filter = $filter;
$this->sortOrders = $sortOrders;
if ($spellingType !== null) {
$this->spellingType = $spellingType;
}
if ($trackTotalHits !== null) {
$this->trackTotalHits = $this->parseTrackTotalHits($trackTotalHits);
}
if ($minScore !== null) {
$this->minScore = $minScore;
}
}
/**
* {@inheritDoc}
*/
public function getFilter()
{
return $this->filter;
}
/**
* {@inheritDoc}
*/
public function getSortOrders()
{
return $this->sortOrders;
}
/**
* {@inheritDoc}
*/
public function getTrackTotalHits()
{
return $this->trackTotalHits;
}
/**
* {@inheritDoc}
*/
public function getMinScore()
{
return $this->minScore;
}
/**
* {@inheritDoc}
*/
public function isSpellchecked()
{
$fuzzySpellingTypes = [
SpellcheckerInterface::SPELLING_TYPE_FUZZY,
SpellcheckerInterface::SPELLING_TYPE_MOST_FUZZY,
];
return in_array($this->spellingType, $fuzzySpellingTypes);
}
/**
* {@inheritDoc}
*/
public function getSpellingType()
{
return $this->spellingType;
}
/**
* {@inheritDoc}
*/
public function setCollapse(CollapseInterface $collapse)
{
$this->collapse = $collapse;
return $this;
}
/**
* {@inheritDoc}
*/
public function hasCollapse()
{
return ($this->collapse instanceof CollapseInterface);
}
/**
* {@inheritDoc}
*/
public function getCollapse()
{
return $this->collapse;
}
/**
* {@inheritDoc}
*/
public function setSourceConfig($sourceConfig)
{
$this->sourceConfig = $sourceConfig;
return $this;
}
/**
* {@inheritDoc}
*/
public function hasSourceConfig()
{
return !empty($this->sourceConfig);
}
/**
* {@inheritDoc}
*/
public function getSourceConfig()
{
return $this->sourceConfig;
}
/**
* Parse the track_total_hits directive to appropriate type : either int or bool.
* It's actually passed as a string when coming from the configuration file reader.
*
* @param int|bool|string $trackTotalHits The track_total_hits value
*
* @return int|bool
*/
private function parseTrackTotalHits($trackTotalHits)
{
// @codingStandardsIgnoreStart
$trackTotalHits = is_numeric($trackTotalHits) ? (int) $trackTotalHits : filter_var($trackTotalHits, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
// @codingStandardsIgnoreEnd
if ($trackTotalHits === false || $trackTotalHits === null) {
$trackTotalHits = 0;
}
return $trackTotalHits;
}
}