Skip to content

Commit cce2dd9

Browse files
committed
ACP2E-99: "Search Synonyms" no longer works when value is added in "Minimum Terms to Match"
1 parent e22173f commit cce2dd9

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/Index/Builder.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Elasticsearch\Model\Adapter\Index;
77

8+
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Framework\Locale\Resolver as LocaleResolver;
910
use Magento\Elasticsearch\Model\Adapter\Index\Config\EsConfigInterface;
1011
use Magento\Search\Model\ResourceModel\SynonymReader;
@@ -59,7 +60,7 @@ public function build()
5960
$tokenizer = $this->getTokenizer();
6061
$filter = $this->getFilter();
6162
$charFilter = $this->getCharFilter();
62-
$synonymFilter = $this->synonymReader->getSynonymFilter();
63+
$synonymFilter = $this->getSynonymFilter();
6364

6465
$settings = [
6566
'analysis' => [
@@ -186,4 +187,26 @@ protected function getStemmerConfig()
186187
'language' => $stemmerInfo['default'],
187188
];
188189
}
190+
191+
/**
192+
* Get filter based on defined synonyms
193+
*
194+
* @throws LocalizedException
195+
*/
196+
private function getSynonymFilter(): array
197+
{
198+
$synonyms = $this->synonymReader->getAllSynonyms();
199+
$synonymFilter = [];
200+
201+
if ($synonyms) {
202+
$synonymFilter = [
203+
'synonyms' => [
204+
'type' => 'synonym_graph',
205+
'synonyms' => $synonyms
206+
]
207+
];
208+
}
209+
210+
return $synonymFilter;
211+
}
189212
}

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/Index/BuilderTest.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testBuildWithoutSynonymsProvided(string $locale)
9999
->method('getLocale')
100100
->willReturn($locale);
101101
$this->synonymReaderMock->expects($this->once())
102-
->method('getSynonymFilter')
102+
->method('getAllSynonyms')
103103
->willReturn([]);
104104

105105
$result = $this->model->build();
@@ -125,6 +125,65 @@ public function testBuildWithoutSynonymsProvided(string $locale)
125125
);
126126
}
127127

128+
/**
129+
* Test build() method with synonyms provided.
130+
*
131+
* In this case synonyms filter should be created, populated with the list of available synonyms
132+
* and referenced in the prefix_search and sku_prefix_search analyzers.
133+
*
134+
* @param string $locale
135+
* @dataProvider buildDataProvider
136+
*/
137+
public function testBuildWithProvidedSynonyms(string $locale)
138+
{
139+
$synonymsFilterName = 'synonyms';
140+
141+
$synonyms = [
142+
'mp3,player,sound,audio',
143+
'tv,video,television,screen'
144+
];
145+
146+
$expectedFilter = [
147+
'type' => 'synonym_graph',
148+
'synonyms' => $synonyms
149+
];
150+
151+
$this->localeResolver->expects($this->once())
152+
->method('getLocale')
153+
->willReturn($locale);
154+
155+
$this->synonymReaderMock->expects($this->once())
156+
->method('getAllSynonyms')
157+
->willReturn($synonyms);
158+
159+
$result = $this->model->build();
160+
161+
$analysisFilters = $result["analysis"]["filter"];
162+
$prefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["prefix_search"]["filter"];
163+
$skuPrefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["sku_prefix_search"]["filter"];
164+
165+
$this->assertArrayHasKey(
166+
$synonymsFilterName,
167+
$analysisFilters,
168+
'Analysis filters must contain synonyms when defined'
169+
);
170+
$this->assertContains(
171+
$expectedFilter,
172+
$analysisFilters,
173+
'Analysis synonyms filter must match the expected result'
174+
);
175+
$this->assertContains(
176+
$synonymsFilterName,
177+
$prefixSearchAnalyzerFilters,
178+
'The prefix_search analyzer must include synonyms filter'
179+
);
180+
$this->assertContains(
181+
$synonymsFilterName,
182+
$skuPrefixSearchAnalyzerFilters,
183+
'The sku_prefix_search analyzer must include synonyms filter'
184+
);
185+
}
186+
128187
/**
129188
* @return array
130189
*/

app/code/Magento/Search/Model/ResourceModel/SynonymReader.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,16 @@ public function loadByPhrase(\Magento\Search\Model\SynonymReader $object, $phras
6767
}
6868

6969
/**
70-
* Get all synonyms as array
70+
* Get all synonyms as an array
7171
*
7272
* @throws LocalizedException
7373
*/
74-
public function getSynonymFilter(): array
74+
public function getAllSynonyms(): array
7575
{
7676
$connection = $this->getConnection();
7777
$select = $connection->select()->from($this->getMainTable(), 'synonyms');
78-
$synonyms = $connection->fetchCol($select);
7978

80-
$synonymFilter = [];
81-
82-
if ($synonyms) {
83-
$synonymFilter = [
84-
'synonyms' => [
85-
'type' => 'synonym_graph',
86-
'synonyms' => $synonyms
87-
]
88-
];
89-
}
90-
91-
return $synonymFilter;
79+
return $connection->fetchCol($select);;
9280
}
9381

9482
/**

0 commit comments

Comments
 (0)