Skip to content

Commit 473d730

Browse files
committed
Merge branch '2.10.x' into 2.11.x
2 parents 81a8b26 + 7a9e490 commit 473d730

3 files changed

Lines changed: 57 additions & 11 deletions

File tree

src/module-elasticsuite-core/Search/Request/Query/Fulltext/QueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(
7171
* Create the fulltext search query.
7272
*
7373
* @param ContainerConfigurationInterface $containerConfig Search request container configuration.
74-
* @param string $queryText The text query.
74+
* @param string|array $queryText The text query.
7575
* @param string $spellingType The type of spellchecked applied.
7676
* @param float $boost Boost of the created query.
7777
* @param int $depth Call depth of the create method. Can be used to avoid/prevent cycles.

src/module-elasticsuite-thesaurus/Plugin/QueryRewrite.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Smile\ElasticsuiteThesaurus\Model\Index;
2323
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
2424
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
25+
use Smile\ElasticsuiteCore\Helper\Text as TextHelper;
2526

2627
/**
2728
* Plugin that handle query rewriting (synonym substitution) during fulltext query building phase.
@@ -47,6 +48,11 @@ class QueryRewrite
4748
*/
4849
private $index;
4950

51+
/**
52+
* @var Text
53+
*/
54+
private $textHelper;
55+
5056
/**
5157
* @var array
5258
*/
@@ -58,15 +64,18 @@ class QueryRewrite
5864
* @param QueryFactory $queryFactory Search request query factory.
5965
* @param ThesaurusConfigFactory $thesaurusConfigFactory Thesaurus configuration factory.
6066
* @param Index $index Synonym index.
67+
* @param TextHelper $textHelper Text helper.
6168
*/
6269
public function __construct(
6370
QueryFactory $queryFactory,
6471
ThesaurusConfigFactory $thesaurusConfigFactory,
65-
Index $index
72+
Index $index,
73+
TextHelper $textHelper
6674
) {
6775
$this->queryFactory = $queryFactory;
6876
$this->thesaurusConfigFactory = $thesaurusConfigFactory;
6977
$this->index = $index;
78+
$this->textHelper = $textHelper;
7079
}
7180

7281
/**
@@ -77,7 +86,7 @@ public function __construct(
7786
* @param QueryBuilder $subject Original query builder.
7887
* @param \Closure $proceed Original create func.
7988
* @param ContainerConfigurationInterface $containerConfig Search request container config.
80-
* @param string $queryText Current query text.
89+
* @param string|array $queryText Current query text.
8190
* @param string $spellingType Spelling type of the query.
8291
* @param float $boost Original query boost.
8392
* @param int $depth Call depth of the create method. Can be used to avoid/prevent cycles.
@@ -109,13 +118,16 @@ public function aroundCreate(
109118
if ($depth === 0) {
110119
$rewrites = $this->getWeightedRewrites($queryText, $containerConfig, $boost);
111120
}
121+
$originalSpellingType = $spellingType;
112122
// Set base query as SPELLING_TYPE_EXACT if synonyms/expansions are found.
123+
// This is to prevent possible fuzzy matches on that original query's terms and doing so, prioritize the rewritten queries.
113124
$spellingType = empty($rewrites) ? $spellingType : SpellcheckerInterface::SPELLING_TYPE_EXACT;
114125
$query = $proceed($containerConfig, $queryText, $spellingType, $boost, $depth);
115126

116127
if (!empty($rewrites)) {
117128
$synonymQueries = [$query];
118-
$synonymQueriesSpellcheck = SpellcheckerInterface::SPELLING_TYPE_EXACT;
129+
// Do not enforce SPELLING_TYPE_EXACT systematically for alternative queries.
130+
$synonymQueriesSpellcheck = $this->getRewritesSpellingType($queryText, $originalSpellingType);
119131

120132
foreach ($rewrites as $rewrittenQuery => $weight) {
121133
$synonymQueries[] = $proceed($containerConfig, $rewrittenQuery, $synonymQueriesSpellcheck, $weight, $depth + 1);
@@ -161,6 +173,40 @@ private function getWeightedRewrites($queryText, $containerConfig, $originalBoos
161173
return $rewrites;
162174
}
163175

176+
/**
177+
* Returns the spelling type to use for rewritten queries.
178+
* For multi terms queries, considering that at least one term has been replaced,
179+
* but there could be at least one mistyped term that might not have been replaced.
180+
* So "elevate" the spelling type a bit.
181+
* Ideally a new spellcheck query should be run.
182+
*
183+
* @param string|array $originalQueryText The original query text.
184+
* @param int $originalSpellingType The original spelling type.
185+
*
186+
* @return int
187+
*/
188+
private function getRewritesSpellingType($originalQueryText, $originalSpellingType)
189+
{
190+
if (is_array($originalQueryText)) {
191+
// Expected to be SPELLING_TYPE_EXACT as enforced by the request builder.
192+
return $originalSpellingType;
193+
}
194+
195+
if ($this->textHelper->mbWordCount($originalQueryText) === 1) {
196+
return SpellcheckerInterface::SPELLING_TYPE_EXACT;
197+
}
198+
199+
$spellingType = $originalSpellingType;
200+
201+
if (SpellcheckerInterface::SPELLING_TYPE_FUZZY === $originalSpellingType) {
202+
$spellingType = SpellcheckerInterface::SPELLING_TYPE_MOST_FUZZY;
203+
} elseif (SpellcheckerInterface::SPELLING_TYPE_PURE_STOPWORDS === $originalSpellingType) {
204+
$spellingType = SpellcheckerInterface::SPELLING_TYPE_MOST_EXACT;
205+
}
206+
207+
return $spellingType;
208+
}
209+
164210
/**
165211
* Return thesaurus/relevance configuration.
166212
*

src/module-elasticsuite-thesaurus/Test/Unit/Plugin/QueryRewriteTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public function testMultipleSearchQueryDepthBuilder()
9595
$maxRewrittenQueries = 0;
9696

9797
$thesaurusConfigFactory = $this->getThesaurusConfigFactoryMock($maxRewrittenQueries);
98-
9998
$thesaurusIndex = $this->getMockBuilder(ThesaurusIndex::class)
10099
->disableOriginalConstructor()
101100
->getMock();
101+
$textHelper = $this->getRealTextHelper();
102102

103-
$queryRewritePlugin = new QueryRewrite($queryFactory, $thesaurusConfigFactory, $thesaurusIndex);
103+
$queryRewritePlugin = new QueryRewrite($queryFactory, $thesaurusConfigFactory, $thesaurusIndex, $textHelper);
104104
$queryBuilderInterceptor = $this->getQueryBuilderWithPlugin($queryFactory, $queryRewritePlugin);
105105

106106
/*
@@ -162,12 +162,12 @@ public function testMultipleSearchQueryDepthBuilderWithRewrites()
162162
$maxRewrittenQueries = 0;
163163

164164
$thesaurusConfigFactory = $this->getThesaurusConfigFactoryMock($maxRewrittenQueries);
165-
166165
$thesaurusIndex = $this->getMockBuilder(ThesaurusIndex::class)
167166
->disableOriginalConstructor()
168167
->getMock();
168+
$textHelper = $this->getRealTextHelper();
169169

170-
$queryRewritePlugin = new QueryRewrite($queryFactory, $thesaurusConfigFactory, $thesaurusIndex);
170+
$queryRewritePlugin = new QueryRewrite($queryFactory, $thesaurusConfigFactory, $thesaurusIndex, $textHelper);
171171
$queryBuilderInterceptor = $this->getQueryBuilderWithPlugin($queryFactory, $queryRewritePlugin);
172172

173173
/*
@@ -224,13 +224,13 @@ public function testSingleSearchQueryLimitedRewrites()
224224
$maxRewrittenQueries = 1;
225225

226226
$thesaurusConfigFactory = $this->getThesaurusConfigFactoryMock($maxRewrittenQueries);
227-
228227
$thesaurusIndex = $this->getMockBuilder(ThesaurusIndex::class)
229228
->disableOriginalConstructor()
230229
->getMock();
230+
$textHelper = $this->getRealTextHelper();
231231

232-
// Passing the mock Query Factory to the plugin to count the occurrence of calls to 'create'.
233-
$queryRewritePlugin = new QueryRewrite($queryFactoryFullMock, $thesaurusConfigFactory, $thesaurusIndex);
232+
// Passing the mock Query Factory to the plugin to count the occurence of calls to 'create'.
233+
$queryRewritePlugin = new QueryRewrite($queryFactoryFullMock, $thesaurusConfigFactory, $thesaurusIndex, $textHelper);
234234
// But passing the real Query Factory (with mocked factories) to the query builder itself.
235235
$queryBuilderInterceptor = $this->getQueryBuilderWithPlugin($queryFactory, $queryRewritePlugin);
236236

0 commit comments

Comments
 (0)