Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual category optimization #352

Merged
merged 3 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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