Skip to content

Commit

Permalink
Merge pull request #986 from afoucret/fix-thesaurus-max-rewrites
Browse files Browse the repository at this point in the history
Fix synonym expansion to avoid huge queries.
  • Loading branch information
afoucret authored Jun 27, 2018
2 parents ed42a14 + ac14dcb commit 49bcbe5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
19 changes: 18 additions & 1 deletion src/module-elasticsuite-thesaurus/Config/ThesaurusConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,35 @@ class ThesaurusConfig
*/
private $expansionsConfig;

/**
* @var array
*/
private $general;

/**
* Constructor.
*
* @param array $general General configuration.
* @param array $synonyms Synonyms configuration.
* @param array $expansions Expansions configuration.
*/
public function __construct($synonyms = [], $expansions = [])
public function __construct($general = [], $synonyms = [], $expansions = [])
{
$this->general = $general;
$this->synonymsConfig = $synonyms;
$this->expansionsConfig = $expansions;
}

/**
* Max allowed rewrites for the synonym engine.
*
* @return int
*/
public function getMaxRewrites()
{
return (int) $this->general['max_rewrites'];
}

/**
* Is the synonyms search enabled ?
*
Expand Down
37 changes: 21 additions & 16 deletions src/module-elasticsuite-thesaurus/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,22 @@ private function computeQueryRewrites(ContainerConfigurationInterface $container
$config = $this->getConfig($containerConfig);
$storeId = $containerConfig->getStoreId();
$rewrites = [];
$maxRewrites = $config->getMaxRewrites();

if ($config->isSynonymSearchEnabled()) {
$synonymRewrites = $this->getSynonymRewrites($storeId, $queryText, ThesaurusInterface::TYPE_SYNONYM);
$thesaurusType = ThesaurusInterface::TYPE_SYNONYM;
$synonymRewrites = $this->getSynonymRewrites($storeId, $queryText, $thesaurusType, $maxRewrites);
$rewrites = $this->getWeightedRewrites($synonymRewrites, $config->getSynonymWeightDivider());
}

if ($config->isExpansionSearchEnabled()) {
$synonymRewrites = array_merge([$queryText => 1], $rewrites);

foreach ($synonymRewrites as $currentQueryText => $currentWeight) {
$expansions = $this->getSynonymRewrites($storeId, $currentQueryText, ThesaurusInterface::TYPE_EXPANSION);
$thesaurusType = ThesaurusInterface::TYPE_EXPANSION;
$expansions = $this->getSynonymRewrites($storeId, $currentQueryText, $thesaurusType, $maxRewrites);
$expansionRewrites = $this->getWeightedRewrites($expansions, $config->getExpansionWeightDivider(), $currentWeight);
$rewrites = array_merge($rewrites, $expansionRewrites);
$rewrites = array_merge($rewrites, $expansionRewrites);
}
}

Expand Down Expand Up @@ -200,13 +203,14 @@ private function getConfig(ContainerConfigurationInterface $containerConfig)
/**
* Generates all possible synonym rewrites for a store and text query.
*
* @param integer $storeId Store id.
* @param string $queryText Text query.
* @param string $type Substitution type (synonym or expansion).
* @param integer $storeId Store id.
* @param string $queryText Text query.
* @param string $type Substitution type (synonym or expansion).
* @param integer $maxRewrites Max number of allowed rewrites.
*
* @return array
*/
private function getSynonymRewrites($storeId, $queryText, $type)
private function getSynonymRewrites($storeId, $queryText, $type, $maxRewrites)
{
$indexName = $this->getIndexAlias($storeId);

Expand All @@ -228,24 +232,25 @@ private function getSynonymRewrites($storeId, $queryText, $type)
}
}

return $this->combineSynonyms($queryText, $synonymByPositions);
return $this->combineSynonyms($queryText, $synonymByPositions, $maxRewrites);
}

/**
* Combine analysis result to provides all possible synonyms substitution comination.
*
* @param string $queryText Original query text
* @param array $synonymByPositions Synonyms array by positions.
* @param int $substitutions Number of substitutions in the current query.
* @param int $offset Offset of previous substitutions.
* @param string $queryText Original query text
* @param array $synonymByPositions Synonyms array by positions.
* @param integer $maxRewrites Max number of allowed rewrites.
* @param int $substitutions Number of substitutions in the current query.
* @param int $offset Offset of previous substitutions.
*
* @return array
*/
private function combineSynonyms($queryText, $synonymByPositions, $substitutions = 0, $offset = 0)
private function combineSynonyms($queryText, $synonymByPositions, $maxRewrites, $substitutions = 0, $offset = 0)
{
$combinations = [];

if (!empty($synonymByPositions)) {
if (!empty($synonymByPositions) && $substitutions < $maxRewrites) {
$currentPositionSynonyms = current($synonymByPositions);
$remainingSynonyms = array_slice($synonymByPositions, 1);

Expand All @@ -259,15 +264,15 @@ private function combineSynonyms($queryText, $synonymByPositions, $substitutions
if (!empty($remainingSynonyms)) {
$combinations = array_merge(
$combinations,
$this->combineSynonyms($rewrittenQueryText, $remainingSynonyms, $substitutions + 1, $newOffset)
$this->combineSynonyms($rewrittenQueryText, $remainingSynonyms, $maxRewrites, $substitutions + 1, $newOffset)
);
}
}

if (!empty($remainingSynonyms)) {
$combinations = array_merge(
$combinations,
$this->combineSynonyms($queryText, $remainingSynonyms, $substitutions, $offset)
$this->combineSynonyms($queryText, $remainingSynonyms, $maxRewrites, $substitutions, $offset)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
<label>Thesaurus Configuration</label>
<resource>Smile_ElasticsuiteCore::manage_relevance</resource>

<group id="general" translate="label" type="text" sortOrder="20" showInDefault="1" showInContainer="1" showInStore="1">
<label>General Configuration</label>
<field id="max_rewrites" translate="label" type="text" sortOrder="10" showInDefault="1" showInContainer="1" showInStore="1">
<label>Max Allowed Rewrites</label>
</field>
</group>

<group id="synonyms" translate="label" type="text" sortOrder="40" showInDefault="1" showInContainer="1" showInStore="1">
<label>Synonyms Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInContainer="1" showInStore="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
xsi:noNamespaceSchemaLocation="urn:magento:module:Smile_ElasticsuiteCore:etc/elasticsuite_relevance_initial_config.xsd">
<default>
<thesaurus>
<general>
<max_rewrites>2</max_rewrites>
</general>
<synonyms>
<enable>1</enable>
<weight_divider>10</weight_divider>
Expand Down

0 comments on commit 49bcbe5

Please sign in to comment.