Skip to content

Commit

Permalink
Adding named queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Jul 12, 2024
1 parent fa61b9b commit e3d1036
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ public function getFunction(ContainerConfigurationInterface $containerConfigurat
{
$field = $this->getField($containerConfiguration, $optimizer);
$scaleFactor = (float) $optimizer->getConfig('scale_factor');

$queryName = sprintf('Optimizer [%s]:%d', $optimizer->getName(), $optimizer->getId());
$query = $optimizer->getRuleCondition()->getSearchQuery();
$query->setName(($query->getName() !== '') ? $queryName . " => " . $query->getName() : $queryName);

$function = [
'field_value_factor' => [
'field' => $field,
'factor' => $scaleFactor,
'modifier' => $optimizer->getConfig('scale_function'),
'missing' => 1 / $scaleFactor,
],
'filter' => $optimizer->getRuleCondition()->getSearchQuery(),
'filter' => $query,
];

return $function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ class ConstantScore implements ApplierInterface
*/
public function getFunction(ContainerConfigurationInterface $containerConfiguration, OptimizerInterface $optimizer)
{
$queryName = sprintf('Optimizer [%s]:%d', $optimizer->getName(), $optimizer->getId());
$query = $optimizer->getRuleCondition()->getSearchQuery();
$query->setName(($query->getName() !== '') ? $queryName . " => " . $query->getName() : $queryName);

$function = [
'weight' => 1 + ((float) $optimizer->getConfig('constant_score_value') / 100),
'filter' => $optimizer->getRuleCondition()->getSearchQuery(),
'filter' => $query,
];

return $function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function getSearchQuery()
}
}

$queryParams['name'] = $this->asStringRecursive();
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);

return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getSearchQuery(ProductCondition $productCondition)
$queryParams = ['field' => $this->getSearchFieldName($productCondition)];
}

$query = $this->prepareQuery($queryType, $queryParams);
$query = $this->prepareQuery($queryType, $queryParams)->setName($productCondition->asString());

if (substr($productCondition->getOperator(), 0, 1) === '!') {
$query = $this->applyNegation($query);
Expand All @@ -122,6 +122,7 @@ public function getSearchQuery(ProductCondition $productCondition)
}

$query = $this->queryFactory->create(QueryInterface::TYPE_NESTED, $nestedQueryParams);
$query->setName($productCondition->asString());
}
}

Expand Down Expand Up @@ -179,7 +180,11 @@ private function getMatchQueryParams(ProductCondition $productCondition)
$queryText = $productCondition->getValue();
$minimumShouldMatch = "100%";

return ['field' => $fieldName, 'queryText' => $queryText, 'minimumShouldMatch' => $minimumShouldMatch];
return [
'field' => $fieldName,
'queryText' => $queryText,
'minimumShouldMatch' => $minimumShouldMatch
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function buildQuery(QueryInterface $query)
'boost' => $query->getBoost(),
];

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

if ($query->getName()) {
$searchQuery['match']['_name'] = $query->getName();
$searchQueryParams['_name'] = $query->getName();
}

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

return $searchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function buildQuery(QueryInterface $query)
$searchQuery = ['range' => [$query->getField() => $queryParams]];

if ($query->getName()) {
$searchQuery['range']['_name'] = $query->getName();
// Intentional omission of the '_name' management because range query does not support it.
}

return $searchQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ public function buildQuery(QueryInterface $query)
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
$searchQuery = [
'span_first' => [
'boost' => $query->getBoost(),
'match' => $this->parentBuilder->buildQuery($query->getMatch()),
'end' => $query->getEnd(),
],
];

if ($query->getName()) {
$searchQuery['span_first']['_name'] = $query->getName();
}

return $searchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public function create(ContainerConfigurationInterface $containerConfig, $queryT
}
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['should' => $queries, 'boost' => $boost]);
} elseif ($spellingType == SpellcheckerInterface::SPELLING_TYPE_PURE_STOPWORDS) {
$query = $this->getPureStopwordsQuery($containerConfig, $queryText, $boost);
$query = $this->getPureStopwordsQuery($containerConfig, $queryText, $boost)->setName('PURE_STOPWORDS');
} elseif (in_array($spellingType, $fuzzySpellingTypes)) {
$query = $this->getSpellcheckedQuery($containerConfig, $queryText, $spellingType, $boost);
$query = $this->getSpellcheckedQuery($containerConfig, $queryText, $spellingType, $boost)->setName('SPELLCHECK');
}

if ($query === null) {
$queryParams = [
'query' => $this->getWeightedSearchQuery($containerConfig, $queryText),
'filter' => $this->getCutoffFrequencyQuery($containerConfig, $queryText),
'query' => $this->getWeightedSearchQuery($containerConfig, $queryText)->setName('EXACT'),
'filter' => $this->getCutoffFrequencyQuery($containerConfig, $queryText)->setName('CUTOFF_FREQUENCY'),
'boost' => $boost,
];
$query = $this->queryFactory->create(QueryInterface::TYPE_FILTER, $queryParams);

$relevanceConfig = $containerConfig->getRelevanceConfig();
if ($relevanceConfig->getSpanMatchBoost()) {
$spanQuery = $this->getSpanQuery($containerConfig, $queryText, $relevanceConfig->getSpanMatchBoost());
$spanQuery = $this->getSpanQuery($containerConfig, $queryText, $relevanceConfig->getSpanMatchBoost())->setName('SPAN');
if ($spanQuery !== null) {
$queryParams = [
'must' => [$query],
Expand Down Expand Up @@ -246,11 +246,11 @@ private function getSpellcheckedQuery(ContainerConfigurationInterface $container
$queryClauses = [];

if ($relevanceConfig->isFuzzinessEnabled()) {
$queryClauses[] = $this->getFuzzyQuery($containerConfig, $queryText);
$queryClauses[] = $this->getFuzzyQuery($containerConfig, $queryText)->setName('FUZZY');
}

if ($relevanceConfig->isPhoneticSearchEnabled()) {
$queryClauses[] = $this->getPhoneticQuery($containerConfig, $queryText);
$queryClauses[] = $this->getPhoneticQuery($containerConfig, $queryText)->setName('PHONETIC');
}

if (!empty($queryClauses)) {
Expand Down
3 changes: 2 additions & 1 deletion src/module-elasticsuite-virtual-category/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ private function getVirtualCategoryQuery(

$query = $category->getVirtualRule()->getConditions()->getSearchQuery($excludedCategories);
if ($query instanceof QueryInterface) {
$query->setName(sprintf('(%s) virtual category [%s]:%d', $category->getPath(), $category->getName(), $category->getId()));
$queryName = sprintf('(%s) virtual category [%s]:%d', $category->getPath(), $category->getName(), $category->getId());
$query->setName(($query->getName() !== '') ? $queryName . " => " . $query->getName() : $queryName);
}
if ($rootCategory && $rootCategory->getId()) {
$rootQuery = $this->getCategorySearchQuery($rootCategory, $excludedCategories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getSearchQuery($excludedCategories = []): QueryInterface
}

$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);
$query->setName($this->asStringRecursive());

return $query;
}
Expand Down

0 comments on commit e3d1036

Please sign in to comment.