-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathField.php
385 lines (332 loc) · 10.5 KB
/
Field.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?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\Index\Mapping;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;
/**
* Default implementation for ES mapping field (Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface).
*
* @SuppressWarnings(ExcessiveClassComplexity)
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
*/
class Field implements FieldInterface
{
/**
* @var int
*/
private const IGNORE_ABOVE_COUNT = 256;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $nestedPath;
/**
* Date formats used by the indices.
*
* @var array
*/
private $dateFormats = [
\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT,
\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT,
];
/**
* @var array
*/
private $config = [
'is_searchable' => false,
'is_filterable' => true,
'is_used_for_sort_by' => false,
'is_used_in_spellcheck' => false,
'search_weight' => 1,
'default_search_analyzer' => self::ANALYZER_STANDARD,
];
/**
* Constructor.
*
* @param string $name Field name.
* @param string $type Field type.
* @param null|string $nestedPath Path for nested fields. null by default and for non-nested fields.
* @param array $fieldConfig Field configuration (see self::$config declaration for
* available values and default values).
*/
public function __construct($name, $type = self::FIELD_TYPE_KEYWORD, $nestedPath = null, $fieldConfig = [])
{
$this->name = (string) $name;
$this->type = (string) $type;
$this->config = $fieldConfig + $this->config;
$this->nestedPath = $nestedPath;
if ($nestedPath !== null && strpos($name, $nestedPath . '.') !== 0) {
throw new \InvalidArgumentException('Invalid nested path or field name');
}
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getType(): string
{
return $this->type;
}
/**
* {@inheritdoc}
*/
public function isSearchable(): bool
{
return (bool) $this->config['is_searchable'];
}
/**
* {@inheritdoc}
*/
public function isFilterable(): bool
{
return (bool) $this->config['is_filterable'];
}
/**
* {@inheritDoc}
*/
public function isUsedForSortBy(): bool
{
return (bool) $this->config['is_used_for_sort_by'];
}
/**
* {@inheritdoc}
*/
public function isUsedInSpellcheck(): bool
{
return (bool) $this->config['is_used_in_spellcheck'] && (bool) $this->config['is_searchable'];
}
/**
* {@inheritdoc}
*/
public function getSearchWeight(): int
{
return (int) $this->config['search_weight'];
}
/**
* {@inheritdoc}
*/
public function isNested(): bool
{
return is_string($this->nestedPath) && !empty($this->nestedPath);
}
/**
* {@inheritdoc}
*/
public function getNestedPath(): ?string
{
return $this->nestedPath;
}
/**
* {@inheritdoc}
*/
public function getNestedFieldName()
{
$nestedFieldName = null;
if ($this->isNested()) {
$nestedPrefix = $this->getNestedPath() . '.';
$nestedFieldName = str_replace($nestedPrefix, '', $this->getName());
}
return $nestedFieldName;
}
/**
* {@inheritdoc}
*/
public function getMappingPropertyConfig(): array
{
$property = $this->getPropertyConfig();
if ($this->getType() === self::FIELD_TYPE_TEXT) {
$analyzers = $this->getFieldAnalyzers();
$property = $this->getPropertyConfig(current($analyzers));
if (count($analyzers) > 1) {
$property = $this->getMultiFieldMappingPropertyConfig($analyzers);
}
}
return $property;
}
/**
* {@inheritDoc}
*/
public function getMappingProperty($analyzer = self::ANALYZER_UNTOUCHED)
{
$fieldName = $this->getName();
$propertyName = $fieldName;
$property = $this->getMappingPropertyConfig();
$isDefaultAnalyzer = $analyzer === $this->getDefaultSearchAnalyzer();
if (!$isDefaultAnalyzer && isset($property['fields'])) {
$propertyName = null;
if (isset($property['fields'][$analyzer])) {
$property = $property['fields'][$analyzer];
$propertyName = $isDefaultAnalyzer ? $fieldName : sprintf('%s.%s', $fieldName, $analyzer);
}
}
if (!$this->checkAnalyzer($property, $analyzer)) {
$propertyName = null;
}
return $propertyName;
}
/**
* {@inheritDoc}
*/
public function getDefaultSearchAnalyzer()
{
return $this->config['default_search_analyzer'];
}
/**
* {@inheritDoc}
*/
public function mergeConfig(array $config = [])
{
$config = array_merge($this->config, $config);
return new static($this->name, $this->type, $this->nestedPath, $config);
}
/**
* {@inheritDoc}
*/
public function getSortMissing($direction = SortOrderInterface::SORT_ASC)
{
// @codingStandardsIgnoreStart
$missing = $direction === SortOrderInterface::SORT_ASC ? SortOrderInterface::MISSING_LAST : SortOrderInterface::MISSING_FIRST;
// @codingStandardsIgnoreEnd
if ($direction === SortOrderInterface::SORT_ASC && isset($this->config['sort_order_asc_missing'])) {
$missing = $this->config['sort_order_asc_missing'];
} elseif ($direction === SortOrderInterface::SORT_DESC && isset($this->config['sort_order_desc_missing'])) {
$missing = $this->config['sort_order_desc_missing'];
}
return $missing;
}
/**
* {@inheritDoc}
*/
public function getConfig(): array
{
return $this->config ?? [];
}
/**
* Check if an ES property as the right analyzer.
*
* @param array $property ES Property.
* @param string $expectedAnalyzer Analyzer expected for the property.
*
* @return boolean
*/
private function checkAnalyzer($property, $expectedAnalyzer): bool
{
$isAnalyzerCorrect = true;
if ($property['type'] === self::FIELD_TYPE_TEXT || $property['type'] === self::FIELD_TYPE_KEYWORD) {
$isAnalyzed = $expectedAnalyzer !== self::ANALYZER_UNTOUCHED;
if ($isAnalyzed && (!isset($property['analyzer']) || $property['analyzer'] !== $expectedAnalyzer)) {
$isAnalyzerCorrect = false;
} elseif (!$isAnalyzed && $property['type'] !== self::FIELD_TYPE_KEYWORD) {
$isAnalyzerCorrect = false;
}
}
return $isAnalyzerCorrect;
}
/**
* Build a multi_field configuration from an analyzers list.
* Standard analyzer is used as default subfield and should always be present.
*
* If the standard analyzer is not present, no default subfield is defined.
*
* @param array $analyzers List of analyzers used as subfields.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @return array
*/
private function getMultiFieldMappingPropertyConfig($analyzers): array
{
// Setting the field type to "multi_field".
$property = [];
foreach ($analyzers as $analyzer) {
if ($analyzer === $this->getDefaultSearchAnalyzer()) {
$property = array_merge($property, $this->getPropertyConfig($analyzer));
} else {
$property['fields'][$analyzer] = $this->getPropertyConfig($analyzer);
}
}
return $property;
}
/**
* Retrieve analyzers used with the current field depending of the field configuration.
*
* @return array
*/
private function getFieldAnalyzers(): array
{
$analyzers = [];
if ($this->isSearchable() || $this->isUsedForSortBy()) {
// Default search analyzer.
$analyzers = [$this->getDefaultSearchAnalyzer()];
}
if ($this->isSearchable() && $this->getSearchWeight() > 1) {
$analyzers[] = self::ANALYZER_WHITESPACE;
$analyzers[] = self::ANALYZER_SHINGLE;
}
if (empty($analyzers) || $this->isFilterable()) {
// For filterable fields or fields without analyzer : append the untouched analyzer.
$analyzers[] = self::ANALYZER_UNTOUCHED;
}
if ($this->isUsedForSortBy()) {
$analyzers[] = self::ANALYZER_SORTABLE;
}
return $analyzers;
}
/**
* Build the property config from the field type and an optional
* analyzer (used for string and detected through getAnalyzers).
*
* @param string|null $analyzer Used analyzer.
*
* @return array
*/
private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED): array
{
$fieldMapping = ['type' => $this->getType()];
switch ($this->getType()) {
case self::FIELD_TYPE_TEXT:
if ($analyzer === self::ANALYZER_UNTOUCHED) {
$fieldMapping['type'] = self::FIELD_TYPE_KEYWORD;
$fieldMapping['ignore_above'] = self::IGNORE_ABOVE_COUNT;
}
if ($analyzer !== self::ANALYZER_UNTOUCHED) {
$fieldMapping['analyzer'] = $analyzer;
if ($analyzer === self::ANALYZER_SORTABLE) {
$fieldMapping['fielddata'] = true;
}
}
break;
case self::FIELD_TYPE_DATE:
$fieldMapping['format'] = implode('||', $this->dateFormats);
break;
}
return $fieldMapping;
}
}