-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFieldMapping.php
More file actions
185 lines (153 loc) · 5.89 KB
/
FieldMapping.php
File metadata and controls
185 lines (153 loc) · 5.89 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
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Elasticsearch;
use Rollerworks\Component\Search\Field\FieldConfig;
use Rollerworks\Component\Search\Field\OrderField;
/**
* @internal
*/
final class FieldMapping
{
public ?string $indexName;
public ?string $typeName;
public ?string $propertyName;
public mixed $propertyValue;
/** @var array<string, mixed> */
public array $propertyQuery = [];
public mixed $nested = false; // Unknown type
public mixed $join = false; // Unknown type
public float $boost; // Currently unused
public ?ValueConversion $valueConversion = null;
public ?QueryConversion $queryConversion = null;
public ?ChildOrderConversion $childOrderConversion = null;
/** @var self[] */
public array $conditions;
/**
* @param self[] $conditions
* @param array<string, mixed> $options
*/
public function __construct(
public string $fieldName,
string $property,
FieldConfig $fieldConfig,
array $conditions = [],
public array $options = [],
) {
$mapping = $this->parseProperty($property);
$this->indexName = $mapping['indexName'];
$this->typeName = $mapping['typeName'];
$this->propertyName = $mapping['propertyName'];
$this->nested = $mapping['nested'];
$this->join = $mapping['join'];
$converter = $fieldConfig->getOption('elasticsearch_conversion');
$childOrderConverter = $fieldConfig->getOption('elasticsearch_child_order_conversion');
if ($converter instanceof ValueConversion) {
$this->valueConversion = $converter;
}
if ($childOrderConverter instanceof ChildOrderConversion) {
$this->childOrderConversion = $childOrderConverter;
}
if ($converter instanceof QueryConversion) {
$this->queryConversion = $converter;
}
$this->conditions = $this->expandConditions($conditions, $fieldConfig);
}
/**
* Supported formats:
* - <property>
* - <sub.property>
* - <nested[].property>
* - <sub.nested[].property>
* - <index>#<property>
* - <index>#<nested[].property>
* - <index>#<sub.nested[].property>
* - <index>/<type>#<property>
* - <index>/<type>#<sub.nested[].property>
* - <index>/<type>#child><sub.nested[].property>.
*
* Returns: array{indexName: ?string, typeName: ?string, propertyName: string, nested: array{path: string}|bool, join: array{type: string}|bool}
*
* @return array<string, mixed>
*/
private function parseProperty(string $property): array
{
$indexName = null;
$typeName = null;
$propertyName = $property;
$nested = false;
$join = false;
if (str_contains($property, '#')) {
[$path, $propertyName] = explode('#', $property);
$path = mb_trim($path, '/');
$indexName = $path;
if (mb_strpos($path, '/') !== false) {
[$indexName, $typeName] = explode('/', $path);
}
}
if (str_contains($property, '>')) {
$tokens = explode('>', $propertyName);
// last token is the property name
$propertyName = mb_trim(array_pop($tokens), '.');
foreach ($tokens as $type) {
$type = mb_trim($type, '.');
$join = compact('type', 'join');
}
}
if (str_contains($propertyName, '[]')) {
$tokens = explode('[]', $propertyName);
// last token is the property name
$propertyName = mb_trim(array_pop($tokens), '.');
$propertyName = mb_trim(end($tokens), '.') . '.' . $propertyName;
foreach ($tokens as $path) {
$path = mb_trim($path, '.');
$nested = compact('path', 'nested');
}
}
return compact('indexName', 'typeName', 'propertyName', 'nested', 'join');
}
private function expandConditions(array $conditions, FieldConfig $fieldConfig): array
{
if ($this->join && OrderField::isOrder($this->fieldName)) {
// sorting by has_child query is special
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html#_sorting
$property = $this->indexName . ($this->typeName ? '/' . $this->typeName : '') . '#' . $this->join['type'] . '>';
$queryScript = \sprintf('doc["%s"].value', $this->propertyName);
if ($this->childOrderConversion !== null) {
$queryScript = $this->childOrderConversion->convert($property, $queryScript);
}
$queryScript = \sprintf('%1$s * %2$s', QueryConditionGenerator::SORT_SCORE, $queryScript);
$scoreQuery = new self('_', $property, $fieldConfig, [], ['score_mode' => 'max']);
$scoreQuery->propertyQuery = [
QueryConditionGenerator::QUERY_FUNCTION_SCORE => [
QueryConditionGenerator::QUERY_SCRIPT_SCORE => [
QueryConditionGenerator::QUERY_SCRIPT => $queryScript,
],
],
];
$conditions[] = $scoreQuery;
}
return $conditions;
}
public function __serialize()
{
return [
'field_name' => $this->fieldName,
'index_name' => $this->indexName,
'type_name' => $this->typeName,
'property_name' => $this->propertyName,
'nested' => $this->nested,
];
}
public function __unserialize($serialized): void
{
// no-op
}
}