Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce base query to be exact match when synonyms are found. #1156

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/module-elasticsuite-thesaurus/Plugin/QueryRewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,13 @@ public function aroundCreate(
$rewriteCacheKey = $requestName . '|' . $storeId . '|' . md5(json_encode($queryText));

if (!isset($this->rewritesCache[$rewriteCacheKey])) {
$query = $proceed($containerConfig, $queryText, $spellingType, $boost);

$rewrites = [];

if (!is_array($queryText)) {
$queryText = [$queryText];
}

foreach ($queryText as $currentQueryText) {
$rewrites = array_merge($rewrites, $this->index->getQueryRewrites($containerConfig, $currentQueryText));
}
$rewrites = $this->getWeightedRewrites($queryText, $containerConfig);
// 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);

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

foreach ($rewrites as $rewrittenQuery => $weight) {
Expand All @@ -107,9 +100,34 @@ public function aroundCreate(

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

$this->rewritesCache[$rewriteCacheKey] = $query;
}

return $this->rewritesCache[$rewriteCacheKey];
}

/**
* Get weighted rewrites for a given query text.
* Returns an associative array of ['rewritten query' => weight] if any matches are found.
*
* @param string|array $queryText The query text
* @param ContainerConfigurationInterface $containerConfig Container Configuration
*
* @return array
*/
private function getWeightedRewrites($queryText, $containerConfig)
{
$rewrites = [];

if (!is_array($queryText)) {
$queryText = [$queryText];
}

foreach ($queryText as $currentQueryText) {
$rewrites = array_merge($rewrites, $this->index->getQueryRewrites($containerConfig, $currentQueryText));
}

return $rewrites;
}
}