Skip to content

Commit

Permalink
Merge pull request #352 from afoucret/virtual-category-optimization
Browse files Browse the repository at this point in the history
Virtual category optimization
  • Loading branch information
afoucret authored Mar 14, 2017
2 parents 6c5eb4f + 7c91485 commit befc3b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/module-elasticsuite-virtual-category/Model/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ private function preparePreviewItems($products = [])
/**
* Return the filter applied to the query.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @return QueryInterface
*/
private function getQueryFilter()
Expand Down
39 changes: 34 additions & 5 deletions src/module-elasticsuite-virtual-category/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,20 @@ private function getVirtualRootCategory(CategoryInterface $category)
*/
private function getStandardCategoryQuery(CategoryInterface $category, $excludedCategories = [])
{
$conditionsParams = ['data' => ['attribute' => 'category_ids', 'operator' => '()', 'value' => $category->getId()]];
return $this->getStandardCategoriesQuery([$category->getId()], $excludedCategories);
}

/**
* Transform a category ids list in query rule.
*
* @param array $categoryIds Categories included in the query.
* @param array $excludedCategories Categories ignored in subquery filters.
*
* @return QueryInterface
*/
private function getStandardCategoriesQuery(array $categoryIds, $excludedCategories)
{
$conditionsParams = ['data' => ['attribute' => 'category_ids', 'operator' => '()', 'value' => $categoryIds]];
$categoryCondition = $this->productConditionsFactory->create($conditionsParams);

return $this->queryBuilder->getSearchQuery($categoryCondition, $excludedCategories);
Expand Down Expand Up @@ -250,6 +263,8 @@ private function getVirtualCategoryQuery(CategoryInterface $category, $excludedC
/**
* Append children queries to the rule.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @param QueryInterface|NULL $query Base query.
* @param CategoryInterface $category Current cayegory.
* @param array $excludedCategories Category already used into the building stack. Avoid short circuit.
Expand All @@ -258,17 +273,27 @@ private function getVirtualCategoryQuery(CategoryInterface $category, $excludedC
*/
private function addChildrenQueries($query, CategoryInterface $category, $excludedCategories = [])
{
$childrenCategories = $this->getChildrenCategories($category, $excludedCategories);
$childrenCategories = $this->getChildrenCategories($category, $excludedCategories);
$childrenCategoriesIds = [];

if ($childrenCategories->getSize() > 0 && $query !== null) {
$queryParams = ['should' => [$query], 'cached' => empty($excludedCategories)];

foreach ($childrenCategories as $childrenCategory) {
$childrenQuery = $this->getCategorySearchQuery($childrenCategory, $excludedCategories);
if ($childrenQuery !== null) {
$queryParams['should'][] = $childrenQuery;
if (((bool) $childrenCategory->getIsVirtualCategory()) === true) {
$childrenQuery = $this->getCategorySearchQuery($childrenCategory, $excludedCategories);
if ($childrenQuery !== null) {
$queryParams['should'][] = $childrenQuery;
}
} else {
$childrenCategoriesIds[] = $childrenCategory->getId();
}
}

if (!empty($childrenCategoriesIds)) {
$queryParams['should'][] = $this->getChildrenCategories($childrenCategoriesIds, $excludedCategories);
}

if (count($queryParams['should']) > 1) {
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);
}
Expand All @@ -292,6 +317,10 @@ private function getChildrenCategories(CategoryInterface $category, $excludedCat

$categoryCollection->addIsActiveFilter()->addPathFilter(sprintf('%s/.*', $category->getPath()));

if (((bool) $category->getIsVirtualCategory()) === false) {
$categoryCollection->addFieldToFilter('is_virtual_category', '1');
}

if (!empty($excludedCategories)) {
$categoryCollection->addAttributeToFilter('entity_id', ['nin' => $excludedCategories]);
}
Expand Down

0 comments on commit befc3b2

Please sign in to comment.