Skip to content

Commit

Permalink
Make the exact match on default analyzer configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Jul 4, 2023
1 parent be83195 commit e427d77
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,11 @@ public function isUsingAllTokens();
* @return bool
*/
public function isUsingReferenceAnalyzer();

/**
* If we should use the default analyzer of each field when building the exact match filter query.
*
* @return bool
*/
public function isUsingDefaultAnalyzerInExactMatchFilter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class RelevanceConfig implements RelevanceConfigurationInterface
*/
private $useReferenceInExactMatchFilter;

/**
* @var bool
*/
private $useDefaultAnalyzerInExactMatchFilter;

/**
* @var boolean
*/
Expand All @@ -91,22 +96,24 @@ class RelevanceConfig implements RelevanceConfigurationInterface
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param string $minimumShouldMatch Minimum should match clause of the text query.
* @param float $tieBreaker Tie breaker for multimatch queries.
* @param int|null $phraseMatchBoost The Phrase match boost value, or null if not
* enabled
* @param float $cutOffFrequency The cutoff Frequency value
* @param FuzzinessConfigurationInterface|null $fuzziness The fuzziness Configuration, or null
* @param boolean $enablePhoneticSearch The phonetic Configuration, or null
* @param int|null $spanMatchBoost The Span match boost value, or null if not
* enabled
* @param int|null $spanSize The number of terms to match in span queries
* @param int|null $minScore The Min Score value, or null if not enabled
* @param boolean $useReferenceInExactMatchFilter Whether to use the reference collector field instead of
* the 'sku' field in the exact match filter query
* @param boolean $useAllTokens Whether to take into account all term vectors tokens
* @param boolean $useReferenceAnalyzer Whether to include the collector field associated
* with the reference analyzer in term vectors request
* @param string $minimumShouldMatch Minimum should match clause of the text query.
* @param float $tieBreaker Tie breaker for multimatch queries.
* @param int|null $phraseMatchBoost The Phrase match boost value, or null if not
* enabled
* @param float $cutOffFrequency The cutoff Frequency value
* @param FuzzinessConfigurationInterface|null $fuzziness The fuzziness Configuration, or null
* @param boolean $enablePhoneticSearch The phonetic Configuration, or null
* @param int|null $spanMatchBoost The Span match boost value, or null if not
* enabled
* @param int|null $spanSize The number of terms to match in span queries
* @param int|null $minScore The Min Score value, or null if not enabled
* @param boolean $useReferenceInExactMatchFilter Whether to use the reference collector field
* instead of 'sku' field in the exact match filter
* @param boolean $useDefaultAnalyzerInExactMatchFilter Whether to use 'field' or 'field.default_analyzer'
* in the exact match filter query
* @param boolean $useAllTokens Whether to take into account all term vectors tokens
* @param boolean $useReferenceAnalyzer Whether to include the collector field associated
* with the reference analyzer in term vectors request
*/
public function __construct(
$minimumShouldMatch,
Expand All @@ -119,6 +126,7 @@ public function __construct(
$spanSize = null,
$minScore = null,
$useReferenceInExactMatchFilter = false,
$useDefaultAnalyzerInExactMatchFilter = false,
$useAllTokens = false,
$useReferenceAnalyzer = false
) {
Expand All @@ -134,6 +142,7 @@ public function __construct(
$this->useReferenceInExactMatchFilter = $useReferenceInExactMatchFilter;
$this->useAllTokens = $useAllTokens;
$this->useReferenceAnalyzer = $useReferenceAnalyzer;
$this->useDefaultAnalyzerInExactMatchFilter = $useDefaultAnalyzerInExactMatchFilter;
}

/**
Expand Down Expand Up @@ -230,6 +239,14 @@ public function isUsingReferenceInExactMatchFilter()
return (bool) $this->useReferenceInExactMatchFilter;
}

/**
* {@inheritDoc}
*/
public function isUsingDefaultAnalyzerInExactMatchFilter()
{
return (bool) $this->useDefaultAnalyzerInExactMatchFilter;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Factory
*/
const MIN_SCORE_CONFIG_XML_PREFIX = 'min_score_configuration';

/**
* XML node for exact match configuration
*/
const EXACT_MATCH_CONFIG_XML_PREFIX = 'exact_match_configuration';

/**
* XML node for using reference in the exact match filter query
*/
Expand Down Expand Up @@ -165,6 +170,7 @@ protected function loadConfiguration($scopeCode)
'useReferenceInExactMatchFilter' => $this->isUsingReferenceInExactMatchFilter($scopeCode),
'useAllTokens' => $this->isUsingAllTokensConfiguration($scopeCode),
'useReferenceAnalyzer' => $this->isUsingReferenceAnalyzerConfiguration($scopeCode),
'useDefaultAnalyzerInExactMatchFilter' => $this->isUsingDefaultAnalyzerInExactMatchFilter($scopeCode),
];

return $configurationParams;
Expand Down Expand Up @@ -410,4 +416,18 @@ private function isUsingReferenceAnalyzerConfiguration($scopeCode)
{
return (bool) $this->getConfigValue(self::TERM_VECTORS_USE_REFERENCE_CONFIG_XML_PATH, $scopeCode);
}

/**
* Check if we should use the default analyzer of each field when building the exact match filter query.
*
* @param @param string $scopeCode The scope code
*
* @return bool
*/
private function isUsingDefaultAnalyzerInExactMatchFilter($scopeCode)
{
$path = self::BASE_RELEVANCE_CONFIG_XML_PREFIX . "/" . self::EXACT_MATCH_CONFIG_XML_PREFIX;

return (bool) $this->getConfigValue($path . "/use_default_analyzer", $scopeCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,25 @@ public function create(ContainerConfigurationInterface $containerConfig, $queryT
private function getCutoffFrequencyQuery(ContainerConfigurationInterface $containerConfig, $queryText)
{
$relevanceConfig = $containerConfig->getRelevanceConfig();
$fields = array_fill_keys([MappingInterface::DEFAULT_SEARCH_FIELD, 'sku'], 1);

$nonStandardSearchableFieldFilter = $this->fieldFilters['nonStandardSearchableFieldFilter'];
if ($containerConfig->getRelevanceConfig()->isUsingDefaultAnalyzerInExactMatchFilter()) {
$nonStandardSearchableFieldFilter = $this->fieldFilters['nonStandardSearchableFieldFilter'];

$defaultFields = array_fill_keys([MappingInterface::DEFAULT_SEARCH_FIELD, 'sku'], 1);
$fields = $defaultFields + $this->getWeightedFields(
$containerConfig,
null,
$nonStandardSearchableFieldFilter,
MappingInterface::DEFAULT_SEARCH_FIELD
);
$fields = $fields + $this->getWeightedFields(
$containerConfig,
null,
$nonStandardSearchableFieldFilter,
MappingInterface::DEFAULT_SEARCH_FIELD
);
}

if ($containerConfig->getRelevanceConfig()->isUsingReferenceInExactMatchFilter()) {
$fields += array_fill_keys(
[MappingInterface::DEFAULT_SEARCH_FIELD, MappingInterface::DEFAULT_REFERENCE_FIELD . ".reference"],
1
);
}

$queryParams = [
'fields' => array_fill_keys(array_keys($fields), 1),
Expand All @@ -134,13 +143,6 @@ private function getCutoffFrequencyQuery(ContainerConfigurationInterface $contai
'minimumShouldMatch' => $relevanceConfig->getMinimumShouldMatch(),
];

if ($containerConfig->getRelevanceConfig()->isUsingReferenceInExactMatchFilter()) {
$queryParams['fields'] = array_fill_keys(
[MappingInterface::DEFAULT_SEARCH_FIELD, MappingInterface::DEFAULT_REFERENCE_FIELD . ".reference"],
1
);
}

return $this->queryFactory->create(QueryInterface::TYPE_MULTIMATCH, $queryParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@

<group id="exact_match_configuration" translate="label" type="text" sortOrder="60" showInDefault="1" showInContainer="1" showInStore="1">
<label>Exact Match Configuration</label>
<field id="use_default_analyzer" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInContainer="1" showInStore="1">
<label>[Experimental] Use default analyzer in exact matching filter query</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Experimental: Use the default analyzer of each field instead of the 'standard' analyzer for exact matching. Eg : for sku, it will try to match on 'sku.reference' field. If a field is using 'standard_edge_ngram' analyzer, it will try to match on 'field.standard_edge_ngram'. Useful for partial matching, or matching on beginning of words.]]></comment>
</field>
<field id="use_reference_in_filter" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInContainer="1" showInStore="1">
<label>[Experimental] Use reference analyzer in exact matching filter query</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<min_score_value>0</min_score_value>
</min_score_configuration>
<exact_match_configuration>
<use_default_analyzer>0</use_default_analyzer>
<use_reference_in_filter>0</use_reference_in_filter>
</exact_match_configuration>
</relevance>
Expand Down
2 changes: 2 additions & 0 deletions src/module-elasticsuite-core/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ Autocomplete,Autocomplete
"Experimental: Take into account all tokens from the term vectors response, instead of one token per position. Useful for sku/reference matching.","Experimental: Take into account all tokens from the term vectors response, instead of one token per position. Useful for sku/reference matching."
"[Experimental] Use reference analyzer in term vectors","[Experimental] Use reference analyzer in term vectors"
"Experimental: Include the 'reference' collector field which contains all searchable fields using the 'reference' analyzer when performing the term vectors request. Useful for sku/reference matching.","Experimental: Include the 'reference' collector field which contains all searchable fields using the 'reference' analyzer when performing the term vectors request. Useful for sku/reference matching."
"[Experimental] Use default analyzer in exact matching filter query","[Experimental] Use default analyzer in exact matching filter query"
"Experimental: Use the default analyzer of each field instead of the 'standard' analyzer for exact matching. Eg : for sku, it will try to match on 'sku.reference' field. If a field is using 'standard_edge_ngram' analyzer, it will try to match on 'field.standard_edge_ngram'. Useful for partial matching, or matching on beginning of words.","Experimental: Use the default analyzer of each field instead of the 'standard' analyzer for exact matching. Eg : for sku, it will try to match on 'sku.reference' field. If a field is using 'standard_edge_ngram' analyzer, it will try to match on 'field.standard_edge_ngram'. Useful for partial matching, or matching on beginning of words."
2 changes: 2 additions & 0 deletions src/module-elasticsuite-core/i18n/fr_FR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ General,Général
"Experimental: Take into account all tokens from the term vectors response, instead of one token per position. Useful for sku/reference matching.","Expérimental: Prendre en considération tous les tokens de la réponse des term vectors, plutôt qu'un seul token par position. Utile pour matcher les sku/références."
"[Experimental] Use reference analyzer in term vectors","[Expérimental] Utiliser l'analyseur reference dans les term vectors"
"Experimental: Include the 'reference' collector field which contains all searchable fields using the 'reference' analyzer when performing the term vectors request. Useful for sku/reference matching.","Expérimental: Inclure le champ collecteur 'reference' qui contient tous les champs cherchables utilisant l'analyseur 'reference' lors de la requête des term vectors. Utile pour le matching de sku/références produit."
"[Experimental] Use default analyzer in exact matching filter query","[Expérimental] Utiliser l'analyseur par défaut des champs dans la recherche exacte"
"Experimental: Use the default analyzer of each field instead of the 'standard' analyzer for exact matching. Eg : for sku, it will try to match on 'sku.reference' field. If a field is using 'standard_edge_ngram' analyzer, it will try to match on 'field.standard_edge_ngram'. Useful for partial matching, or matching on beginning of words.","Expérimental: Utiliser l'analyseur par défaut de chaque champ au lieu de l'analyseur 'standard' pour la recherch exacte. Ex : pour le sku, le moteur essayera de trouver une correspondance exacte sur sku.reference. Si un champ a pour analyseur 'standard_edge_ngram', le moteur tentera de trouver une correspondance exacte sur 'champ.standard_edge_ngram'. Utile pour le matching partiel, ou le matching sur les débuts de mots."

0 comments on commit e427d77

Please sign in to comment.