-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTranslatedFieldQueryBuilder.php
More file actions
133 lines (108 loc) · 3.81 KB
/
Copy pathTranslatedFieldQueryBuilder.php
File metadata and controls
133 lines (108 loc) · 3.81 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
<?php declare(strict_types=1);
namespace Shopware\Elasticsearch;
use OpenSearchDSL\BuilderInterface;
use OpenSearchDSL\Query\Compound\DisMaxQuery;
use Shopware\Core\Framework\Adapter\Storage\AbstractKeyValueStorage;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
use Shopware\Elasticsearch\Product\ElasticsearchOptimizeSwitch;
use Shopware\Elasticsearch\Product\SearchFieldConfig;
/**
* @internal
*/
#[Package('inventory')]
class TranslatedFieldQueryBuilder extends AbstractFieldQueryBuilder
{
/**
* @internal
*/
public function __construct(
private readonly AbstractFieldQueryBuilder $fieldQueryBuilder,
private readonly AbstractKeyValueStorage $storage,
private readonly float $dismaxTieBreaker = 0.2,
) {
}
public function getDecorated(): AbstractFieldQueryBuilder
{
return $this->fieldQueryBuilder;
}
public function build(
ResolvedField $field,
string $token,
SearchFieldConfig $config,
Context $context,
): ?BuilderInterface {
if (!$field instanceof TranslatedResolvedField) {
return $this->getDecorated()->build($field, $token, $config, $context);
}
$languageIdChain = $this->getLanguageIdChain($field->getTranslatedField(), $context);
$languageQueries = [];
$ranking = $config->getRanking();
foreach ($languageIdChain as $languageId) {
$searchField = $this->buildTranslatedFieldName($config, $languageId);
$languageConfig = new SearchFieldConfig(
$searchField,
$ranking,
$config->tokenize(),
$config->isAndLogic(),
$config->usePrefixMatch(),
$config->useExactSubfield(),
$config->isPhrase(),
);
$languageQuery = $this->getDecorated()->build(
new ResolvedField($field->getResolvedField()),
$token,
$languageConfig,
$context,
);
$ranking *= 0.8;
if (!$languageQuery) {
continue;
}
$languageQueries[] = $languageQuery;
}
if ($languageQueries === []) {
return null;
}
$fieldQuery = \count($languageQueries) === 1
? $languageQueries[0]
: $this->buildDisMaxQuery($languageQueries);
return $fieldQuery;
}
/**
* @return list<string>
*/
private function getLanguageIdChain(TranslatedField $field, Context $context): array
{
if ($this->isSortableTranslatedField($field)) {
return [$context->getLanguageId()];
}
return $context->getLanguageIdChain();
}
private function isSortableTranslatedField(TranslatedField $field): bool
{
return $field->useForSorting() && (Feature::isActive('v6.8.0.0') || $this->storage->has(ElasticsearchOptimizeSwitch::FLAG));
}
private function buildTranslatedFieldName(SearchFieldConfig $fieldConfig, string $languageId): string
{
if ($fieldConfig->isCustomField()) {
$parts = explode('.', $fieldConfig->getField());
return \sprintf('%s.%s.%s', $parts[0], $languageId, $parts[1]);
}
return \sprintf('%s.%s', $fieldConfig->getField(), $languageId);
}
/**
* @param list<BuilderInterface> $queries
*/
private function buildDisMaxQuery(array $queries): DisMaxQuery
{
$dismax = new DisMaxQuery();
foreach ($queries as $query) {
$dismax->addQuery($query);
}
$dismax->addParameter('tie_breaker', $this->dismaxTieBreaker);
return $dismax;
}
}