-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathMapping.php
349 lines (296 loc) · 10.5 KB
/
Mapping.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
<?php
/**
* DISCLAIMER :
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile_Elasticsuite
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCore\Index;
use Smile\ElasticsuiteCore\Api\Index\MappingInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\DynamicFieldProviderInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldFilterInterface;
/**
* Default implementation for ES mappings (Smile\ElasticsuiteCore\Api\Index\MappingInterface).
*
* @category Smile_Elasticsuite
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
*/
class Mapping implements MappingInterface
{
/**
* @var string
*/
private $idFieldName;
/**
* List of fields for the current mapping.
*
* @var \Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface[]
*/
private $fields;
/**
* List of default fields and associated analyzers.
*
* @var array
*/
private $defaultMappingFields = [
self::DEFAULT_SEARCH_FIELD => [
FieldInterface::ANALYZER_STANDARD,
FieldInterface::ANALYZER_WHITESPACE,
FieldInterface::ANALYZER_SHINGLE,
],
self::DEFAULT_SPELLING_FIELD => [
FieldInterface::ANALYZER_STANDARD,
FieldInterface::ANALYZER_WHITESPACE,
FieldInterface::ANALYZER_SHINGLE,
FieldInterface::ANALYZER_PHONETIC,
],
self::DEFAULT_AUTOCOMPLETE_FIELD => [
FieldInterface::ANALYZER_STANDARD,
FieldInterface::ANALYZER_WHITESPACE,
FieldInterface::ANALYZER_SHINGLE,
],
];
/**
* List of target field for copy to by field configuration.
*
* @var array
*/
private $copyFieldMap = [
'isSearchable' => self::DEFAULT_SEARCH_FIELD,
'isUsedInSpellcheck' => self::DEFAULT_SPELLING_FIELD,
];
/**
* Instanciate a new mapping.
*
* @param string $idFieldName Field use as unique id for the documents.
* @param FieldInterface[] $staticFields List of static fields.
* @param DynamicFieldProviderInterface[] $dynamicFieldProviders Dynamic fields providers.
*/
public function __construct($idFieldName, array $staticFields = [], array $dynamicFieldProviders = [])
{
$this->fields = $this->prepareFields($staticFields) + $this->getDynamicFields($dynamicFieldProviders);
$this->idFieldName = $idFieldName;
if (!isset($this->fields[$this->idFieldName])) {
throw new \InvalidArgumentException("Invalid id field $this->idFieldName : field is not declared.");
}
}
/**
* {@inheritDoc}
*/
public function asArray()
{
return ['_all' => ['enabled' => false], 'properties' => $this->getProperties()];
}
/**
* {@inheritDoc}
*/
public function getProperties()
{
$properties = [];
foreach ($this->defaultMappingFields as $fieldName => $analyzers) {
$properties = $this->addProperty($properties, $fieldName, FieldInterface::FIELD_TYPE_TEXT, $analyzers);
}
foreach ($this->getFields() as $currentField) {
$properties = $this->addField($properties, $currentField);
}
return $properties;
}
/**
* {@inheritDoc}
*/
public function getFields()
{
return $this->fields;
}
/**
* {@inheritDoc}
*/
public function getField($name)
{
if (!isset($this->fields[$name])) {
throw new \LogicException("Field {$name} does not exists in mapping");
}
return $this->fields[$name];
}
/**
* {@inheritDoc}
*/
public function getIdField()
{
return $this->getField($this->idFieldName);
}
/**
* {@inheritDoc}
*/
public function getWeightedSearchProperties(
$analyzer = null,
$defaultField = null,
$boost = 1,
FieldFilterInterface $fieldFilter = null
) {
$weightedFields = [];
$fields = $this->getFields();
if ($defaultField) {
$defaultSearchProperty = $this->getDefaultSearchProperty($defaultField, $analyzer);
$weightedFields[$defaultSearchProperty] = $boost;
}
if ($fieldFilter !== null) {
$fields = array_filter($fields, [$fieldFilter, 'filterField']);
}
foreach ($fields as $field) {
$currentAnalyzer = $analyzer;
$canAddField = $defaultField === null || $field->getSearchWeight() !== 1;
if ($analyzer === null) {
$currentAnalyzer = $field->getDefaultSearchAnalyzer();
$canAddField = $canAddField || ($currentAnalyzer !== FieldInterface::ANALYZER_STANDARD);
}
$property = $field->getMappingProperty($currentAnalyzer);
if ($property && $canAddField) {
$weightedFields[$property] = $boost * $field->getSearchWeight();
}
}
return $weightedFields;
}
/**
* Return the search property for a field present in defaultMappingFields.
*
* @throws \InvalidArgument If the field / analyzer does not exists.
*
* @param string $field Field.
* @param string $analyzer Required analyzer.
*
* @return string
*/
private function getDefaultSearchProperty($field = self::DEFAULT_SEARCH_FIELD, $analyzer = null)
{
if (!isset($this->defaultMappingFields[$field])) {
throw new \InvalidArgumentException("Unable to find field {$field}.");
}
$property = $field;
if ($analyzer !== null) {
if (!in_array($analyzer, $this->defaultMappingFields[$field])) {
throw new \InvalidArgumentException("Unable to find analyzer {$analyzer} for field {$field}.");
}
$property = sprintf("%s.%s", $field, $analyzer);
}
return $property;
}
/**
* Prepare the array of fields to be added to the mapping. Mostly rekey the array.
*
* @param array $fields Fields to be prepared.
*
* @return FieldInterface[]
*/
private function prepareFields(array $fields)
{
$preparedFields = [];
foreach ($fields as $field) {
$preparedFields[$field->getName()] = $field;
}
return $preparedFields;
}
/**
* Retrieve the fields provided by differents providers.
*
* @param DynamicFieldProviderInterface[] $dynamicFieldProviders List of dynamic fields providers
*
* @return FieldInterface[]
*/
private function getDynamicFields(array $dynamicFieldProviders)
{
$fields = [];
foreach ($dynamicFieldProviders as $dynamicFieldProvider) {
$fields += $this->prepareFields($dynamicFieldProvider->getFields());
}
return $fields;
}
/**
* Append a new properties into a properties list and returned the updated map.
*
* @param array $properties Initial properties list.
* @param string $propertyName New property name.
* @param string $propertyType New property type.
* @param array $analyzers Property analyzers.
*
* @return array
*/
private function addProperty(array $properties, $propertyName, $propertyType, $analyzers = [])
{
$property = ['type' => $propertyType, 'analyzer' => FieldInterface::ANALYZER_STANDARD];
foreach ($analyzers as $analyzer) {
if ($analyzer !== FieldInterface::ANALYZER_STANDARD) {
$property['fields'][$analyzer] = ['type' => $propertyType, 'analyzer' => $analyzer];
}
}
$properties[$propertyName] = $property;
return $properties;
}
/**
* Append a field to a mapping properties list.
* The field is append and the new properties list is returned.
*
* @param array $properties Initial properties map.
* @param FieldInterface $field Field to be added.
*
* @return array
*/
private function addField(array $properties, FieldInterface $field)
{
$fieldName = $field->getName();
$fieldRoot = &$properties;
// Read property config from the field.
$property = $field->getMappingPropertyConfig();
$fieldPathArray = explode('.', $fieldName);
$currentPathArray = [];
$fieldPathSize = count($fieldPathArray);
for ($i = 0; $i < $fieldPathSize - 1; $i++) {
$currentPathArray[] = $fieldPathArray[$i];
$currentPath = implode('.', $currentPathArray);
if ($field->isNested() && $field->getNestedPath() == $currentPath && !isset($fieldRoot[$fieldPathArray[$i]])) {
$fieldRoot[$fieldPathArray[$i]] = ['type' => FieldInterface::FIELD_TYPE_NESTED, 'properties' => []];
} elseif (!isset($fieldRoot[$fieldPathArray[$i]])) {
$fieldRoot[$fieldPathArray[$i]] = ['type' => FieldInterface::FIELD_TYPE_OBJECT, 'properties' => []];
}
$fieldRoot = &$fieldRoot[$fieldPathArray[$i]]['properties'];
}
/*
* Retrieving location where the property has to be copied to.
* Ex : searchable fields are copied to default "search" field.
*/
$copyToProperties = $this->getFieldCopyToProperties($field);
if (!empty($copyToProperties)) {
// For normal fields, copy_to is append at the property root.
$copyToRoot = &$property;
$copyToRoot['copy_to'] = $copyToProperties;
}
$fieldRoot[end($fieldPathArray)] = $property;
return $properties;
}
/**
* Get the list of default fields where the current field must be copied.
* Example : searchable fields are copied into the default "search" field.
*
* @param FieldInterface $field Field to be checked.
*
* @return array
*/
private function getFieldCopyToProperties(FieldInterface $field)
{
$copyTo = [];
foreach ($this->copyFieldMap as $method => $targetField) {
if ($field->$method()) {
$copyTo[] = $targetField;
}
}
return $copyTo;
}
}