-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFieldQueryBuilder.php
More file actions
298 lines (249 loc) · 11.1 KB
/
Copy pathFieldQueryBuilder.php
File metadata and controls
298 lines (249 loc) · 11.1 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
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
<?php declare(strict_types=1);
namespace Shopware\Elasticsearch;
use OpenSearchDSL\BuilderInterface;
use OpenSearchDSL\Query\Compound\ConstantScoreQuery;
use OpenSearchDSL\Query\Compound\DisMaxQuery;
use OpenSearchDSL\Query\FullText\MatchPhrasePrefixQuery;
use OpenSearchDSL\Query\FullText\MatchQuery;
use OpenSearchDSL\Query\TermLevel\TermQuery;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FloatField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ListField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Elasticsearch\Framework\ElasticsearchFieldBuilder;
use Shopware\Elasticsearch\Product\SearchFieldConfig;
use Shopware\Elasticsearch\Query\MatchBoolPrefixQuery;
/**
* @internal
*
* Builds the per-field query for a SINGLE search token. Splitting the user's input into
* tokens is the caller's job ({@see Product\ProductSearchQueryBuilder}); this builder never
* re-splits. Multi-word proximity is handled separately: when the config is a "phrase" config
* ({@see SearchFieldConfig::isPhrase()}) the whole term is matched as a match_phrase_prefix,
* which the caller adds as a SHOULD boost on top of the per-token queries.
*
* Per-match-type boosts are injected from `elasticsearch.search.boost.*` so relevance can be
* tuned via configuration. In explain mode ({@see Context::ELASTICSEARCH_EXPLAIN_MODE}) each
* clause is named with its match type so the live-search preview can report how a field matched.
*/
#[Package('inventory')]
class FieldQueryBuilder extends AbstractFieldQueryBuilder
{
/**
* @internal
*/
public function __construct(
private readonly int $minGram = 4,
private readonly bool $useLanguageAnalyzer = true,
private readonly float $dismaxTieBreaker = 0.2,
private readonly float $exactBoost = 2.0,
private readonly float $phraseBoost = 4.0,
private readonly float $fuzzyBoost = 0.4,
private readonly float $prefixBoost = 0.4,
private readonly float $partialBoost = 0.4,
) {
}
public function getDecorated(): AbstractFieldQueryBuilder
{
throw new DecorationPatternException(self::class);
}
public function build(
ResolvedField $field,
string $token,
SearchFieldConfig $config,
Context $context,
): ?BuilderInterface {
return $this->matchQuery($field->getResolvedField(), $token, $config, $context);
}
private function matchQuery(Field $field, string $token, SearchFieldConfig $config, Context $context): ?BuilderInterface
{
if ($this->isTextField($field)) {
return $config->isPhrase()
? $this->buildPhraseMatchQuery($token, $config, $context)
: $this->buildTextMatchQuery($token, $config, $context);
}
// A phrase boost is a multi-word proximity signal — it has no meaning for
// non-text (numeric/bool) fields, so those contribute nothing to it.
if ($config->isPhrase()) {
return null;
}
$normalizedToken = $this->normalizeToken($token, $field);
if ($normalizedToken === null) {
return null;
}
$term = new TermQuery($config->getField(), $normalizedToken, ['boost' => $config->getRanking()]);
$this->nameClause($term, $config, (string) $normalizedToken, 'exact', $context);
return $term;
}
private function normalizeToken(string $token, Field $field): bool|int|float|string|null
{
if ($field instanceof BoolField) {
return match ($token) {
'1', 'true' => true,
'0', 'false' => false,
default => null,
};
}
if ($field instanceof IntField || $field instanceof FloatField || $field instanceof PriceField) {
if (!\is_numeric($token)) {
return null;
}
return $field instanceof IntField ? (int) $token : (float) $token;
}
return $token;
}
private function isTextField(Field $field): bool
{
return $field instanceof StringField || $field instanceof LongTextField || $field instanceof ListField;
}
private function buildTextMatchQuery(string $token, SearchFieldConfig $config, Context $context): BuilderInterface
{
$searchField = $config->getField() . '.search';
$maxExpansions = $this->getMaxExpansions($token);
$clauses = [
'exact' => $this->buildExactMatchQuery($config, $token),
'fuzzy' => $this->buildFuzzyMatchQuery($searchField, $token, $config, $maxExpansions),
'prefix' => $this->buildPrefixMatchQuery($searchField, $token, $config),
'ngram' => $this->buildNgramQuery($token, $config),
];
foreach ($clauses as $type => $clause) {
if ($clause !== null) {
$this->nameClause($clause, $config, $token, $type, $context);
}
}
return $this->buildDisMaxQuery(array_values(array_filter($clauses)), $config->getRanking());
}
/**
* Explicit multi-word proximity boost. Runs on the analyzed `.search` subfield as a
* match_phrase_prefix so word order/adjacency is rewarded and the last word may still be
* a prefix (search-as-you-type). A phrase match requires every word to be present, so
* when the caller adds this as a SHOULD it can only re-rank AND matches, never admit new
* ones — AND semantics stay intact.
*/
private function buildPhraseMatchQuery(string $token, SearchFieldConfig $config, Context $context): ?MatchPhrasePrefixQuery
{
if (!$config->usePrefixMatch()) {
return null;
}
$lastWord = array_last(preg_split('/\s+/u', $token, -1, \PREG_SPLIT_NO_EMPTY) ?: [$token]);
// The phrase boost is folded with the field ranking, since the caller adds this as a
// standalone SHOULD clause rather than a member of the per-field DisMax.
$params = [
'boost' => $this->phraseBoost * $config->getRanking(),
'slop' => 3,
'max_expansions' => $this->getMaxExpansions((string) $lastWord),
];
if (!$this->useLanguageAnalyzer) {
$params['analyzer'] = ElasticsearchFieldBuilder::ANALYZER_WHITESPACE;
}
$phrase = new MatchPhrasePrefixQuery($config->getField() . '.search', $token, $params);
$this->nameClause($phrase, $config, $token, 'phrase', $context);
return $phrase;
}
private function buildExactMatchQuery(SearchFieldConfig $config, string $token): BuilderInterface
{
if ($config->useExactSubfield()) {
return new TermQuery($config->getField() . '.exact', $token, ['boost' => $this->exactBoost]);
}
$matchQueryParams = [
'boost' => $this->exactBoost,
'fuzziness' => 0,
'operator' => 'and',
];
if (!$this->useLanguageAnalyzer) {
$matchQueryParams['analyzer'] = ElasticsearchFieldBuilder::ANALYZER_WHITESPACE;
}
return new MatchQuery($config->getField() . '.search', $token, $matchQueryParams);
}
private function buildFuzzyMatchQuery(string $searchField, string $token, SearchFieldConfig $config, int $maxExpansions): MatchQuery
{
$matchQueryParams = [
'boost' => $this->fuzzyBoost,
'fuzziness' => $config->getFuzziness($token),
'operator' => $config->isAndLogic() ? 'and' : 'or',
'fuzzy_transpositions' => true,
'max_expansions' => $maxExpansions,
'prefix_length' => $config->getPrefixLength($token),
];
if (!$this->useLanguageAnalyzer) {
$matchQueryParams['analyzer'] = ElasticsearchFieldBuilder::ANALYZER_WHITESPACE;
}
return new MatchQuery($searchField, $token, $matchQueryParams);
}
private function buildPrefixMatchQuery(string $searchField, string $token, SearchFieldConfig $config): ?BuilderInterface
{
if (!$config->usePrefixMatch()) {
return null;
}
$matchBoolPrefixParams = ['boost' => $this->prefixBoost];
if (!$this->useLanguageAnalyzer) {
$matchBoolPrefixParams['analyzer'] = ElasticsearchFieldBuilder::ANALYZER_WHITESPACE;
}
return new MatchBoolPrefixQuery($searchField, $token, $matchBoolPrefixParams);
}
private function buildNgramQuery(string $token, SearchFieldConfig $config): ?BuilderInterface
{
if (!$config->tokenize() || mb_strlen($token) < $this->minGram) {
return null;
}
// n-gram is the weakest, fragment-level fallback: it matches on shared
// character n-grams. Because it is scored with BM25, a token whose only
// shared fragment is rare (high idf) can out-score a real word match — a
// misspelling like "mabrle" then ranks an unrelated product that merely
// shares the "abr" fragment above the actual fuzzy "marble" corrections.
// Wrap it in constant_score so every n-gram hit contributes the same
// fixed, low weight: enough to surface fragment matches for recall, never
// enough to outrank an exact / fuzzy / prefix word match.
return new ConstantScoreQuery(
new MatchQuery($config->getField() . '.ngram', $token),
['boost' => $this->partialBoost],
);
}
/**
* In explain mode, tag a clause with its match type so the live-search preview can report
* how the field matched. Gated on the state, so normal search is untouched.
*/
private function nameClause(BuilderInterface $clause, SearchFieldConfig $config, string $term, string $type, Context $context): void
{
if (!$context->hasState(Context::ELASTICSEARCH_EXPLAIN_MODE) || !method_exists($clause, 'addParameter')) {
return;
}
$clause->addParameter('_name', (string) json_encode([
'field' => $config->getField(),
'term' => $term,
'ranking' => $config->getRanking(),
'type' => $type,
]));
}
/**
* @param list<BuilderInterface> $queries
*/
private function buildDisMaxQuery(array $queries, float|int $boost): DisMaxQuery
{
$dismax = new DisMaxQuery();
foreach ($queries as $query) {
$dismax->addQuery($query);
}
$dismax->addParameter('boost', $boost);
$dismax->addParameter('tie_breaker', $this->dismaxTieBreaker);
return $dismax;
}
private function getMaxExpansions(string $lastWord): int
{
$len = mb_strlen($lastWord);
if ($len <= 3) {
return 5;
}
if ($len <= 6) {
return 10;
}
return 20;
}
}