Skip to content

Commit 80cd335

Browse files
committed
MAGETWO-98258: Fix elacticsearch search perfomance
- reverting back fix
1 parent 5276b27 commit 80cd335

File tree

19 files changed

+81
-413
lines changed

19 files changed

+81
-413
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/DynamicField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getFields(array $context = []): array
126126
foreach ($groups as $group) {
127127
$groupPriceKey = $this->fieldNameResolver->getFieldName(
128128
$priceAttribute,
129-
array_merge($context, ['customerGroupId' => $group->getId()])
129+
['customerGroupId' => $group->getId(), 'websiteId' => $context['websiteId']]
130130
);
131131
$allAttributes[$groupPriceKey] = [
132132
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_FLOAT),

app/code/Magento/Elasticsearch/SearchAdapter/Aggregation/Builder/Term.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,24 @@
88
use Magento\Framework\Search\Request\BucketInterface as RequestBucketInterface;
99
use Magento\Framework\Search\Dynamic\DataProviderInterface;
1010

11-
/**
12-
* Builder for term buckets.
13-
*/
1411
class Term implements BucketBuilderInterface
1512
{
1613
/**
17-
* @inheritdoc
14+
* {@inheritdoc}
1815
*/
1916
public function build(
2017
RequestBucketInterface $bucket,
2118
array $dimensions,
2219
array $queryResult,
2320
DataProviderInterface $dataProvider
2421
) {
25-
$buckets = $queryResult['aggregations'][$bucket->getName()]['buckets'] ?? [];
2622
$values = [];
27-
foreach ($buckets as $resultBucket) {
23+
foreach ($queryResult['aggregations'][$bucket->getName()]['buckets'] as $resultBucket) {
2824
$values[$resultBucket['key']] = [
2925
'value' => $resultBucket['key'],
3026
'count' => $resultBucket['doc_count'],
3127
];
3228
}
33-
3429
return $values;
3530
}
3631
}

app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/Match.php

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
*/
66
namespace Magento\Elasticsearch\SearchAdapter\Query\Builder;
77

8-
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool;
9-
use Magento\Framework\App\ObjectManager;
108
use Magento\Framework\Search\Request\Query\BoolExpression;
119
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
1210
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
1311
use Magento\Framework\Search\Adapter\Preprocessor\PreprocessorInterface;
1412

15-
/**
16-
* Builder for match query.
17-
*/
1813
class Match implements QueryInterface
1914
{
2015
/**
@@ -28,35 +23,24 @@ class Match implements QueryInterface
2823
private $fieldMapper;
2924

3025
/**
31-
* @deprecated
32-
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
3326
* @var PreprocessorInterface[]
3427
*/
3528
protected $preprocessorContainer;
3629

37-
/**
38-
* @var ValueTransformerPool
39-
*/
40-
private $valueTransformerPool;
41-
4230
/**
4331
* @param FieldMapperInterface $fieldMapper
4432
* @param PreprocessorInterface[] $preprocessorContainer
45-
* @param ValueTransformerPool|null $valueTransformerPool
4633
*/
4734
public function __construct(
4835
FieldMapperInterface $fieldMapper,
49-
array $preprocessorContainer,
50-
ValueTransformerPool $valueTransformerPool = null
36+
array $preprocessorContainer
5137
) {
5238
$this->fieldMapper = $fieldMapper;
5339
$this->preprocessorContainer = $preprocessorContainer;
54-
$this->valueTransformerPool = $valueTransformerPool ?? ObjectManager::getInstance()
55-
->get(ValueTransformerPool::class);
5640
}
5741

5842
/**
59-
* @inheritdoc
43+
* {@inheritdoc}
6044
*/
6145
public function build(array $selectQuery, RequestQueryInterface $requestQuery, $conditionType)
6246
{
@@ -77,14 +61,16 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
7761
}
7862

7963
/**
80-
* Prepare query.
81-
*
8264
* @param string $queryValue
8365
* @param string $conditionType
8466
* @return array
8567
*/
8668
protected function prepareQuery($queryValue, $conditionType)
8769
{
70+
$queryValue = $this->escape($queryValue);
71+
foreach ($this->preprocessorContainer as $preprocessor) {
72+
$queryValue = $preprocessor->process($queryValue);
73+
}
8874
$condition = $conditionType === BoolExpression::QUERY_CONDITION_NOT ?
8975
self::QUERY_CONDITION_MUST_NOT : $conditionType;
9076
return [
@@ -113,34 +99,21 @@ protected function buildQueries(array $matches, array $queryValue)
11399

114100
// Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
115101
$count = 0;
116-
$value = preg_replace('#^"(.*)"$#m', '$1', $queryValue['value'], -1, $count);
102+
$value = preg_replace('#^\\\\"(.*)\\\\"$#m', '$1', $queryValue['value'], -1, $count);
117103
$condition = ($count) ? 'match_phrase' : 'match';
118104

119-
$attributesTypes = $this->fieldMapper->getAllAttributesTypes();
120-
$transformedTypes = [];
121105
foreach ($matches as $match) {
122106
$resolvedField = $this->fieldMapper->getFieldName(
123107
$match['field'],
124108
['type' => FieldMapperInterface::TYPE_QUERY]
125109
);
126-
$valueTransformer = $this->valueTransformerPool->get($attributesTypes[$resolvedField]['type'] ?? 'text');
127-
$valueTransformerHash = \spl_object_hash($valueTransformer);
128-
if (!isset($transformedTypes[$valueTransformerHash])) {
129-
$transformedTypes[$valueTransformerHash] = $valueTransformer->transform($value);
130-
}
131-
$transformedValue = $transformedTypes[$valueTransformerHash];
132-
if (null === $transformedValue) {
133-
//Value is incompatible with this field type.
134-
continue;
135-
}
136-
137110
$conditions[] = [
138111
'condition' => $queryValue['condition'],
139112
'body' => [
140113
$condition => [
141114
$resolvedField => [
142-
'query' => $transformedValue,
143-
'boost' => $match['boost'] ?? 1,
115+
'query' => $value,
116+
'boost' => isset($match['boost']) ? $match['boost'] : 1,
144117
],
145118
],
146119
],
@@ -151,15 +124,18 @@ protected function buildQueries(array $matches, array $queryValue)
151124
}
152125

153126
/**
127+
* Cut trailing plus or minus sign, and @ symbol, using of which causes InnoDB to report a syntax error.
128+
* @link https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
129+
*
154130
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
155131
*
156-
* @deprecated
157-
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
158132
* @param string $value
159133
* @return string
160134
*/
161135
protected function escape($value)
162136
{
137+
$value = preg_replace('/@+|[@+-]+$/', '', $value);
138+
163139
$pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
164140
$replace = '\\\$1';
165141

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformer/DateTransformer.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformer/FloatTransformer.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformer/IntegerTransformer.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformer/TextTransformer.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

app/code/Magento/Elasticsearch/SearchAdapter/Query/ValueTransformerInterface.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)