Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Jul 12, 2024
2 parents 1117be3 + eb78f05 commit b1bc7f4
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ 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' => [
Expand All @@ -65,7 +68,7 @@ public function getFunction(ContainerConfigurationInterface $containerConfigurat
'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 @@ -82,8 +82,10 @@ 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->setName('PURE_STOPWORDS');
} elseif (in_array($spellingType, $fuzzySpellingTypes)) {
$query = $this->getSpellcheckedQuery($containerConfig, $queryText, $spellingType, $boost);
$query->setName('SPELLCHECK');
}

if ($query === null) {
Expand All @@ -93,10 +95,12 @@ public function create(ContainerConfigurationInterface $containerConfig, $queryT
'boost' => $boost,
];
$query = $this->queryFactory->create(QueryInterface::TYPE_FILTER, $queryParams);
$query->setName('EXACT');

$relevanceConfig = $containerConfig->getRelevanceConfig();
if ($relevanceConfig->getSpanMatchBoost()) {
$spanQuery = $this->getSpanQuery($containerConfig, $queryText, $relevanceConfig->getSpanMatchBoost());
$spanQuery->setName('SPAN');
if ($spanQuery !== null) {
$queryParams = [
'must' => [$query],
Expand Down Expand Up @@ -246,11 +250,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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testAnonymousMatchQueryBuilder()
$this->assertEquals(MatchQuery::DEFAULT_MINIMUM_SHOULD_MATCH, $query['match']['searchField']['minimum_should_match']);
$this->assertEquals(MatchQuery::DEFAULT_BOOST_VALUE, $query['match']['searchField']['boost']);

$this->assertArrayNotHasKey('_name', $query['match']);
$this->assertArrayNotHasKey('_name', $query['match']['searchField']);
}

/**
Expand All @@ -58,8 +58,8 @@ public function testNamedMatchQueryBuilder()
$matchQuery = new MatchQuery('search text', 'searchField', MatchQuery::DEFAULT_BOOST_VALUE, 'queryName');
$query = $builder->buildQuery($matchQuery);

$this->assertArrayHasKey('_name', $query['match']);
$this->assertEquals('queryName', $query['match']['_name']);
$this->assertArrayHasKey('_name', $query['match']['searchField']);
$this->assertEquals('queryName', $query['match']['searchField']['_name']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ public function testAnonymousRangeQueryBuilder()
$this->assertArrayNotHasKey('_name', $query['range']);
}

/**
* Test the builder with mandatory + name params.
*
* @return void
*/
public function testNamedRangeQueryBuilder()
{
$builder = $this->getQueryBuilder();

$rangeQuery = new RangeQuery('field', ['bounds'], 'queryName');
$query = $builder->buildQuery($rangeQuery);

$this->assertArrayHasKey('_name', $query['range']);
$this->assertEquals('queryName', $query['range']['_name']);
}

/**
* {@inheritDoc}
*/
Expand Down
47 changes: 44 additions & 3 deletions src/module-elasticsuite-virtual-category/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ private function getRootCategory(int $rootCategoryId, int $storeId = null)
*/
private function getStandardCategoryQuery(CategoryInterface $category, $excludedCategories = []): QueryInterface
{
return $this->getStandardCategoriesQuery([$category->getId()], $excludedCategories);
$query = $this->getStandardCategoriesQuery([$category->getId()], $excludedCategories);
$query->setName(sprintf('(%s) standard category [%s]:%d', $category->getPath(), $category->getName(), $category->getId()));

return $query;
}

/**
Expand Down Expand Up @@ -426,10 +429,22 @@ private function getVirtualCategoryQuery(
}

$query = $category->getVirtualRule()->getConditions()->getSearchQuery($excludedCategories);
if ($query instanceof QueryInterface) {
$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);
if ($rootQuery) {
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['must' => array_filter([$query, $rootQuery])]);
$query->setName(
sprintf(
'(%s) virtual category [%s]:%d and its virtual root',
$category->getPath(),
$category->getName(),
$category->getId()
)
);
}
}

Expand Down Expand Up @@ -459,6 +474,14 @@ private function addChildrenQueries($query, CategoryInterface $category, $exclud
if (((bool) $childrenCategory->getIsVirtualCategory()) === true) {
$childrenQuery = $this->getCategorySearchQuery($childrenCategory, $excludedCategories);
if ($childrenQuery !== null) {
$childrenQuery->setName(
sprintf(
'(%s) child virtual category [%s]:%d',
$childrenCategory->getPath(),
$childrenCategory->getName(),
$childrenCategory->getId()
)
);
$queryParams['should'][] = $childrenQuery;
}
} else {
Expand All @@ -467,11 +490,29 @@ private function addChildrenQueries($query, CategoryInterface $category, $exclud
}

if (!empty($childrenCategoriesIds)) {
$queryParams['should'][] = $this->getStandardCategoriesQuery($childrenCategoriesIds, $excludedCategories);
$standardChildrenQuery = $this->getStandardCategoriesQuery($childrenCategoriesIds, $excludedCategories);
$standardChildrenQuery->setName(
sprintf(
'(%s) standard children of virtual category [%s]:%d',
$category->getPath(),
$category->getName(),
$category->getId()
)
);

$queryParams['should'][] = $standardChildrenQuery;
}

if (count($queryParams['should']) > 1) {
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);
$query->setName(
sprintf(
'(%s) category [%s]:%d and its children',
$category->getPath(),
$category->getName(),
$category->getId()
)
);
}
}

Expand Down Expand Up @@ -501,7 +542,7 @@ private function getChildrenCategories(CategoryInterface $category, $excludedCat
$categoryCollection->addAttributeToFilter('entity_id', ['nin' => $excludedCategories]);
}

$categoryCollection->addAttributeToSelect(['is_active', 'virtual_category_root', 'is_virtual_category', 'virtual_rule']);
$categoryCollection->addAttributeToSelect(['name', 'is_active', 'virtual_category_root', 'is_virtual_category', 'virtual_rule']);

return $categoryCollection;
}
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 b1bc7f4

Please sign in to comment.