Skip to content

Commit

Permalink
Use the configurable msm for fuzzy search.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Feb 8, 2024
1 parent 0ec1a08 commit ca473fd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function getPrefixLength();
* @return int
*/
public function getMaxExpansion();

/**
* @return string
*/
public function getMinimumShouldMatch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private function getFuzzinessConfiguration($scopeCode)
'value' => $this->getConfigValue($path . "/value", $scopeCode),
'prefixLength' => $this->getConfigValue($path . "/prefix_length", $scopeCode),
'maxExpansion' => $this->getConfigValue($path . "/max_expansion", $scopeCode),
'minimumShouldMatch' => $this->getFuzzinessMinimumShouldMatch($scopeCode),
];

$configuration = $this->createFuzzinessConfiguration($configurationParams);
Expand Down Expand Up @@ -489,4 +490,27 @@ private function getExactMatchSortableBoostConfiguration($scopeCode)

return (int) $this->getConfigValue($path . "/sortable_boost_value", $scopeCode);
}

/**
* Return the minimum should match configured for fuzzy search.
* Either the default one, or a particular if configured so.
*
* @param string $scopeCode The scope code
*
* @return string
*/
private function getFuzzinessMinimumShouldMatch($scopeCode)
{
$minimumShouldMatch = $this->getMinimumShouldMatch($scopeCode);
$useDefaultMsmPath = self::FUZZINESS_CONFIG_XML_PREFIX . '/use_default_minimum_should_match';
$useDefaultMsm = (bool) $this->getConfigValue($useDefaultMsmPath, $scopeCode);
$fuzzyMsmPath = self::FUZZINESS_CONFIG_XML_PREFIX . '/minimum_should_match';
$fuzzyMsm = (string) $this->getConfigValue($fuzzyMsmPath, $scopeCode);

if ($useDefaultMsm === false && $fuzzyMsm !== '') {
$minimumShouldMatch = $fuzzyMsm;
}

return $minimumShouldMatch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,29 @@ class FuzzinessConfig implements FuzzinessConfigurationInterface
*/
private $maxExpansion;

/**
* @var string
*/
private $minimumShouldMatch;

/**
* RelevanceConfiguration constructor.
*
* @param float $value The value
* @param int $prefixLength The prefix length
* @param int $maxExpansion The max expansion
* @param float $value The value
* @param int $prefixLength The prefix length
* @param int $maxExpansion The max expansion
* @param string $minimumShouldMatch Minimum should match clause of the fuzzy query.
*/
public function __construct(
$value,
$prefixLength,
$maxExpansion
$maxExpansion,
$minimumShouldMatch
) {
$this->value = $value;
$this->prefixLength = $prefixLength;
$this->maxExpansion = $maxExpansion;
$this->minimumShouldMatch = $minimumShouldMatch;
}

/**
Expand All @@ -79,4 +87,12 @@ public function getMaxExpansion()
{
return (int) $this->maxExpansion;
}

/**
* {@inheritDoc}
*/
public function getMinimumShouldMatch()
{
return (string) $this->minimumShouldMatch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private function getFuzzyQuery(ContainerConfigurationInterface $containerConfig,
$queryParams = [
'fields' => $searchFields,
'queryText' => $queryText,
'minimumShouldMatch' => "100%",
'minimumShouldMatch' => $relevanceConfig->getFuzzinessConfiguration()->getMinimumShouldMatch(),
'tieBreaker' => $relevanceConfig->getTieBreaker(),
'fuzzinessConfig' => $relevanceConfig->getFuzzinessConfiguration(),
'cutoffFrequency' => $relevanceConfig->getCutoffFrequency(),
Expand All @@ -311,7 +311,7 @@ private function getFuzzyQuery(ContainerConfigurationInterface $containerConfig,
}

/**
* Phonentic query part.
* Phonetic query part.
*
* @param ContainerConfigurationInterface $containerConfig Search request container configuration.
* @param string $queryText The text query.
Expand All @@ -330,7 +330,7 @@ private function getPhoneticQuery(ContainerConfigurationInterface $containerConf
$queryParams = [
'fields' => $searchFields,
'queryText' => $queryText,
'minimumShouldMatch' => "100%",
'minimumShouldMatch' => $relevanceConfig->getFuzzinessConfiguration()->getMinimumShouldMatch(),
'tieBreaker' => $relevanceConfig->getTieBreaker(),
'cutoffFrequency' => $relevanceConfig->getCutoffFrequency(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Request\Query\Fulltext;

use Smile\ElasticsuiteCore\Api\Search\Request\Container\RelevanceConfiguration\FuzzinessConfigurationInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\Fulltext\QueryBuilder;
use Smile\ElasticsuiteCore\Index\Mapping\Field;
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
Expand All @@ -27,6 +28,8 @@
/**
* Fulltext query builder test case.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
Expand Down Expand Up @@ -206,11 +209,12 @@ private function getContainerConfigMock($fields)
*/
private function getRelevanceConfig()
{
$relevanceConfig = $this->getMockBuilder(RelevanceConfigurationInterface::class)
->getMock();
$relevanceConfig = $this->getMockBuilder(RelevanceConfigurationInterface::class)->getMock();
$fuzzinessConfig = $this->getMockBuilder(FuzzinessConfigurationInterface::class)->getMock();

$relevanceConfig->method('isFuzzinessEnabled')->will($this->returnValue(true));
$relevanceConfig->method('isPhoneticSearchEnabled')->will($this->returnValue(true));
$relevanceConfig->method('getFuzzinessConfiguration')->will($this->returnValue($fuzzinessConfig));

return $relevanceConfig;
}
Expand Down

0 comments on commit ca473fd

Please sign in to comment.