Skip to content

Commit 509629b

Browse files
committed
MC-32378: Implement control over minimum_should_match for elasticsearch queries
1 parent 0def9f4 commit 509629b

File tree

12 files changed

+56
-17
lines changed

12 files changed

+56
-17
lines changed

app/code/Magento/Elasticsearch6/Model/Adapter/FieldMapper/AddDefaultSearchField.php renamed to app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/AddDefaultSearchField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Elasticsearch6\Model\Adapter\FieldMapper;
8+
namespace Magento\Elasticsearch\Model\Adapter\FieldMapper;
99

1010
use Magento\Elasticsearch\Model\Adapter\FieldsMappingPreprocessorInterface;
1111

app/code/Magento/Elasticsearch6/Model/Adapter/FieldMapper/CopySearchableFieldsToSearchField.php renamed to app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/CopySearchableFieldsToSearchField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Elasticsearch6\Model\Adapter\FieldMapper;
8+
namespace Magento\Elasticsearch\Model\Adapter\FieldMapper;
99

1010
use Magento\Elasticsearch\Model\Adapter\FieldsMappingPreprocessorInterface;
1111

app/code/Magento/Elasticsearch6/Test/Unit/Model/Adapter/FieldMapper/AddDefaultSearchFieldTest.php renamed to app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/FieldMapper/AddDefaultSearchFieldTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Elasticsearch6\Test\Unit\Model\Adapter\FieldMapper;
8+
namespace Magento\Elasticsearch\Test\Unit\Model\Adapter\FieldMapper;
99

10-
use Magento\Elasticsearch6\Model\Adapter\FieldMapper\AddDefaultSearchField;
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\AddDefaultSearchField;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212
use PHPUnit\Framework\TestCase;
1313

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Elasticsearch6\Test\Unit\Model\Adapter\FieldMapper;
8+
namespace Magento\Elasticsearch\Test\Unit\Model\Adapter\FieldMapper;
99

10-
use Magento\Elasticsearch6\Model\Adapter\FieldMapper\CopySearchableFieldsToSearchField;
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\CopySearchableFieldsToSearchField;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212
use PHPUnit\Framework\TestCase;
1313

app/code/Magento/Elasticsearch/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
"Elasticsearch HTTP Password","Elasticsearch HTTP Password"
1111
"Elasticsearch Server Timeout","Elasticsearch Server Timeout"
1212
"Test Connection","Test Connection"
13+
"Minimum terms to match","Minimum terms to match"

app/code/Magento/Elasticsearch6/Test/Unit/Model/Client/ElasticsearchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Magento\Elasticsearch6\Test\Unit\Model\Client;
88

99
use Magento\AdvancedSearch\Model\Client\ClientInterface as ElasticsearchClient;
10-
use Magento\Elasticsearch6\Model\Adapter\FieldMapper\AddDefaultSearchField;
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\AddDefaultSearchField;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1212
use Magento\Elasticsearch6\Model\Client\Elasticsearch;
1313

app/code/Magento/Elasticsearch6/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797
<type name="Magento\Elasticsearch6\Model\Client\Elasticsearch">
9898
<arguments>
9999
<argument name="fieldsMappingPreprocessors" xsi:type="array">
100-
<item name="elasticsearch6_copy_searchable_fields_to_search_field" xsi:type="object">Magento\Elasticsearch6\Model\Adapter\FieldMapper\CopySearchableFieldsToSearchField</item>
101-
<item name="elasticsearch6_add_default_search_field" xsi:type="object">Magento\Elasticsearch6\Model\Adapter\FieldMapper\AddDefaultSearchField</item>
100+
<item name="elasticsearch6_copy_searchable_fields_to_search_field" xsi:type="object">Magento\Elasticsearch\Model\Adapter\FieldMapper\CopySearchableFieldsToSearchField</item>
101+
<item name="elasticsearch6_add_default_search_field" xsi:type="object">Magento\Elasticsearch\Model\Adapter\FieldMapper\AddDefaultSearchField</item>
102102
</argument>
103103
</arguments>
104104
</type>

app/code/Magento/Elasticsearch7/Model/Client/Elasticsearch.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Elasticsearch7\Model\Client;
99

10+
use Magento\Elasticsearch\Model\Adapter\FieldsMappingPreprocessorInterface;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\AdvancedSearch\Model\Client\ClientInterface;
1213

@@ -32,16 +33,23 @@ class Elasticsearch implements ClientInterface
3233
*/
3334
private $pingResult;
3435

36+
/**
37+
* @var FieldsMappingPreprocessorInterface[]
38+
*/
39+
private $fieldsMappingPreprocessors;
40+
3541
/**
3642
* Initialize Elasticsearch 7 Client
3743
*
3844
* @param array $options
3945
* @param \Elasticsearch\Client|null $elasticsearchClient
46+
* @param array $fieldsMappingPreprocessors
4047
* @throws LocalizedException
4148
*/
4249
public function __construct(
4350
$options = [],
44-
$elasticsearchClient = null
51+
$elasticsearchClient = null,
52+
$fieldsMappingPreprocessors = []
4553
) {
4654
if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
4755
($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
@@ -54,6 +62,7 @@ public function __construct(
5462
$this->client[getmypid()] = $elasticsearchClient;
5563
}
5664
$this->clientOptions = $options;
65+
$this->fieldsMappingPreprocessors = $fieldsMappingPreprocessors;
5766
}
5867

5968
/**
@@ -273,11 +282,7 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
273282
'include_type_name' => true,
274283
'body' => [
275284
$entityType => [
276-
'properties' => [
277-
'_search' => [
278-
'type' => 'text'
279-
],
280-
],
285+
'properties' => [],
281286
'dynamic_templates' => [
282287
[
283288
'price_mapping' => [
@@ -315,7 +320,7 @@ public function addFieldsMapping(array $fields, string $index, string $entityTyp
315320
],
316321
];
317322

318-
foreach ($fields as $field => $fieldInfo) {
323+
foreach ($this->applyFieldsMappingPreprocessors($fields) as $field => $fieldInfo) {
319324
$params['body'][$entityType]['properties'][$field] = $fieldInfo;
320325
}
321326

@@ -349,4 +354,18 @@ public function deleteMapping(string $index, string $entityType)
349354
]
350355
);
351356
}
357+
358+
/**
359+
* Apply fields mapping preprocessors
360+
*
361+
* @param array $properties
362+
* @return array
363+
*/
364+
private function applyFieldsMappingPreprocessors(array $properties): array
365+
{
366+
foreach ($this->fieldsMappingPreprocessors as $preprocessor) {
367+
$properties = $preprocessor->process($properties);
368+
}
369+
return $properties;
370+
}
352371
}

app/code/Magento/Elasticsearch7/Test/Unit/Model/Client/ElasticsearchTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\AdvancedSearch\Model\Client\ClientInterface as ElasticsearchClient;
1212
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1313
use Magento\Elasticsearch7\Model\Client\Elasticsearch;
14+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\AddDefaultSearchField;
1415

1516
/**
1617
* Class ElasticsearchTest to test Elasticsearch 7
@@ -90,7 +91,8 @@ protected function setUp()
9091
Elasticsearch::class,
9192
[
9293
'options' => $this->getOptions(),
93-
'elasticsearchClient' => $this->elasticsearchClientMock
94+
'elasticsearchClient' => $this->elasticsearchClientMock,
95+
'fieldsMappingPreprocessors' => [new AddDefaultSearchField()]
9496
]
9597
);
9698
}

app/code/Magento/Elasticsearch7/etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@
7979
<field id="engine">elasticsearch7</field>
8080
</depends>
8181
</field>
82+
<field id="elasticsearch7_minimum_should_match" translate="label" type="text" sortOrder="93" showInDefault="1">
83+
<label>Minimum Terms to Match</label>
84+
<depends>
85+
<field id="engine">elasticsearch7</field>
86+
</depends>
87+
<comment><![CDATA[<a href="https://docs.magento.com/m2/ce/user_guide/catalog/search-elasticsearch.html">Learn more</a> about valid syntax.]]></comment>
88+
<backend_model>Magento\Elasticsearch\Model\Config\Backend\MinimumShouldMatch</backend_model>
89+
</field>
8290
</group>
8391
</section>
8492
</system>

0 commit comments

Comments
 (0)