Skip to content

Commit 5e75fb7

Browse files
committed
Merge branch 'MC-17629' of https://github.com/magento-mpi/magento2ce into PR-2019-12-06
2 parents b736fd0 + b9af778 commit 5e75fb7

File tree

16 files changed

+649
-114
lines changed

16 files changed

+649
-114
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Model\Adapter;
9+
10+
/**
11+
* Modifies fields mapping before save
12+
*/
13+
interface FieldsMappingPreprocessorInterface
14+
{
15+
/**
16+
* Modifies fields mapping before save
17+
*
18+
* @param array $mapping
19+
* @return array
20+
*/
21+
public function process(array $mapping): array;
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Model\Config\Backend;
9+
10+
use Magento\Framework\App\Config\Value;
11+
use Magento\Framework\Exception\LocalizedException;
12+
13+
/**
14+
* Elasticsearch minimum should match data model
15+
*/
16+
class MinimumShouldMatch extends Value
17+
{
18+
/**
19+
* @inheritDoc
20+
*/
21+
public function beforeSave()
22+
{
23+
$result = parent::beforeSave();
24+
$this->validateValue();
25+
return $result;
26+
}
27+
28+
/**
29+
* Validates config value
30+
*
31+
* @throws LocalizedException
32+
*/
33+
public function validateValue(): void
34+
{
35+
if (strlen($this->getValue()) && !preg_match('/^((\d+<)?-?\d+%?\s?)+$/', $this->getValue())) {
36+
throw new LocalizedException(
37+
__('Value for the field "%1" was not saved because of the incorrect format.', __('Minimum Terms to Match'))
38+
);
39+
}
40+
}
41+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
99
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ResolverInterface as TypeResolver;
10+
use Magento\Elasticsearch\Model\Config;
1011
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Search\Request\Query\BoolExpression;
@@ -50,20 +51,26 @@ class Match implements QueryInterface
5051
* @var ValueTransformerPool
5152
*/
5253
private $valueTransformerPool;
54+
/**
55+
* @var Config
56+
*/
57+
private $config;
5358

5459
/**
5560
* @param FieldMapperInterface $fieldMapper
5661
* @param PreprocessorInterface[] $preprocessorContainer
5762
* @param AttributeProvider|null $attributeProvider
5863
* @param TypeResolver|null $fieldTypeResolver
5964
* @param ValueTransformerPool|null $valueTransformerPool
65+
* @param Config|null $config
6066
*/
6167
public function __construct(
6268
FieldMapperInterface $fieldMapper,
6369
array $preprocessorContainer,
6470
AttributeProvider $attributeProvider = null,
6571
TypeResolver $fieldTypeResolver = null,
66-
ValueTransformerPool $valueTransformerPool = null
72+
ValueTransformerPool $valueTransformerPool = null,
73+
Config $config = null
6774
) {
6875
$this->fieldMapper = $fieldMapper;
6976
$this->preprocessorContainer = $preprocessorContainer;
@@ -73,6 +80,7 @@ public function __construct(
7380
->get(TypeResolver::class);
7481
$this->valueTransformerPool = $valueTransformerPool ?? ObjectManager::getInstance()
7582
->get(ValueTransformerPool::class);
83+
$this->config = $config ?? ObjectManager::getInstance()->get(Config::class);
7684
}
7785

7886
/**
@@ -83,11 +91,15 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
8391
$queryValue = $this->prepareQuery($requestQuery->getValue(), $conditionType);
8492
$queries = $this->buildQueries($requestQuery->getMatches(), $queryValue);
8593
$requestQueryBoost = $requestQuery->getBoost() ?: 1;
94+
$minimumShouldMatch = $this->config->getElasticsearchConfigData('minimum_should_match');
8695
foreach ($queries as $query) {
8796
$queryBody = $query['body'];
8897
$matchKey = isset($queryBody['match_phrase']) ? 'match_phrase' : 'match';
8998
foreach ($queryBody[$matchKey] as $field => $matchQuery) {
9099
$matchQuery['boost'] = $requestQueryBoost + $matchQuery['boost'];
100+
if ($minimumShouldMatch) {
101+
$matchQuery['minimum_should_match'] = $minimumShouldMatch;
102+
}
91103
$queryBody[$matchKey][$field] = $matchQuery;
92104
}
93105
$selectQuery['bool'][$query['condition']][] = $queryBody;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Test\Unit\Model\Config\Backend;
9+
10+
use Magento\Elasticsearch\Model\Config\Backend\MinimumShouldMatch;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit\Framework\TestCase;
14+
use Throwable;
15+
16+
/**
17+
* Test elasticsearch minimum should match data model
18+
*/
19+
class MinimumShouldMatchTest extends TestCase
20+
{
21+
/**
22+
* @var MinimumShouldMatch
23+
*/
24+
private $model;
25+
26+
/**
27+
* @inheritDoc
28+
*/
29+
protected function setUp()
30+
{
31+
$objectManager = new ObjectManager($this);
32+
$this->model = $objectManager->getObject(MinimumShouldMatch::class);
33+
parent::setUp();
34+
}
35+
36+
/**
37+
* @param string $value
38+
* @param bool $valid
39+
* @dataProvider validateValueDataProvider
40+
* @throws LocalizedException
41+
*/
42+
public function testValidateValue(string $value, bool $valid)
43+
{
44+
$this->model->setValue($value);
45+
try {
46+
$this->model->validateValue();
47+
} catch (Throwable $exception) {
48+
$this->assertFalse($valid);
49+
return;
50+
}
51+
$this->assertTrue($valid);
52+
}
53+
54+
/**
55+
* @return array
56+
*/
57+
public function validateValueDataProvider(): array
58+
{
59+
return [
60+
['3', true],
61+
['-2', true],
62+
['75%', true],
63+
['-25%', true],
64+
['3<90%', true],
65+
['2<-25% 9<-3', true],
66+
['90%<3', false],
67+
['<90%', false],
68+
['90%<', false],
69+
['-3<2', false],
70+
['two', false],
71+
['2<', false],
72+
['<2', false],
73+
];
74+
}
75+
}

0 commit comments

Comments
 (0)