Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Dec 18, 2023
2 parents e7e48ef + 196f0ef commit a6378b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ public function __construct(QueryFactory $queryFactory, array $fieldFilters = []
* @param string $queryText The text query.
* @param string $spellingType The type of spellchecked applied.
* @param float $boost Boost of the created query.
* @param int $depth Call depth of the create method. Can be used to avoid/prevent cycles.
*
* @return QueryInterface
*/
public function create(ContainerConfigurationInterface $containerConfig, $queryText, $spellingType, $boost = 1)
public function create(ContainerConfigurationInterface $containerConfig, $queryText, $spellingType, $boost = 1, $depth = 0)
{
$query = null;

Expand All @@ -75,7 +76,7 @@ public function create(ContainerConfigurationInterface $containerConfig, $queryT
if (is_array($queryText)) {
$queries = [];
foreach ($queryText as $currentQueryText) {
$queries[] = $this->create($containerConfig, $currentQueryText, $spellingType);
$queries[] = $this->create($containerConfig, $currentQueryText, $spellingType, $boost, $depth + 1);
}
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['should' => $queries, 'boost' => $boost]);
} elseif ($spellingType == SpellcheckerInterface::SPELLING_TYPE_PURE_STOPWORDS) {
Expand Down
23 changes: 17 additions & 6 deletions src/module-elasticsuite-thesaurus/Plugin/QueryRewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct(QueryFactory $queryFactory, Index $index)
* @param string $queryText Current query text.
* @param string $spellingType Spelling type of the query.
* @param float $boost Original query boost.
* @param int $depth Call depth of the create method. Can be used to avoid/prevent cycles.
*
* @return QueryInterface
*/
Expand All @@ -77,25 +78,35 @@ public function aroundCreate(
ContainerConfigurationInterface $containerConfig,
$queryText,
$spellingType,
$boost = 1
$boost = 1,
$depth = 0
) {

$storeId = $containerConfig->getStoreId();
$requestName = $containerConfig->getName();
$rewriteCacheKey = $requestName . '|' . $storeId . '|' . md5(json_encode($queryText));
$rewriteCacheKey = $requestName . '|' . $storeId . '|' . $depth . '|' . md5(json_encode($queryText));

if (!isset($this->rewritesCache[$rewriteCacheKey])) {
$rewrites = $this->getWeightedRewrites($queryText, $containerConfig, $boost);
$rewrites = [];
/*
* Prevents multiple and excessive rewriting when calling the fulltext query builder 'create' method
* with an array of query text
* - ALL queries will be rewritten here on the first pass in this plugin
* - but no longer on the consecutive "atomic" calls from the 'create' method to itself
* Also prevents rewriting a query text that has been provided by the rewriting process.
*/
if ($depth === 0) {
$rewrites = $this->getWeightedRewrites($queryText, $containerConfig, $boost);
}
// Set base query as SPELLING_TYPE_EXACT if synonyms/expansions are found.
$spellingType = empty($rewrites) ? $spellingType : SpellcheckerInterface::SPELLING_TYPE_EXACT;
$query = $proceed($containerConfig, $queryText, $spellingType, $boost);
$query = $proceed($containerConfig, $queryText, $spellingType, $boost, $depth);

if (!empty($rewrites)) {
$synonymQueries = [$query];
$synonymQueriesSpellcheck = SpellcheckerInterface::SPELLING_TYPE_EXACT;

foreach ($rewrites as $rewrittenQuery => $weight) {
$synonymQueries[] = $proceed($containerConfig, $rewrittenQuery, $synonymQueriesSpellcheck, $weight);
$synonymQueries[] = $proceed($containerConfig, $rewrittenQuery, $synonymQueriesSpellcheck, $weight, $depth + 1);
}

$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['should' => $synonymQueries]);
Expand Down

0 comments on commit a6378b5

Please sign in to comment.