From 65f6f1b03f8b44f021c3ecf67d541deb89bc8d82 Mon Sep 17 00:00:00 2001 From: Vincent ENJALBERT Date: Mon, 1 May 2023 16:54:58 +0200 Subject: [PATCH] #2097 Virtual Categories - Slow Search Queries fix --- .../Model/Rule.php | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/module-elasticsuite-virtual-category/Model/Rule.php b/src/module-elasticsuite-virtual-category/Model/Rule.php index b502d7c66..ccb0088eb 100644 --- a/src/module-elasticsuite-virtual-category/Model/Rule.php +++ b/src/module-elasticsuite-virtual-category/Model/Rule.php @@ -369,25 +369,41 @@ private function getVirtualCategoryQuery( */ private function addChildrenQueries($query, CategoryInterface $category, $excludedCategories = []): QueryInterface { - $childrenCategories = $this->getChildrenCategories($category, $excludedCategories); - $childrenCategoriesIds = []; + $isStandardCategory = ! $category->getIsVirtualCategory(); + $childrenStandardCategoriesIds = []; + $childrenCategories = $this->getChildrenCategories($category, $excludedCategories); if ($query !== null && $childrenCategories->getSize() > 0) { $queryParams = ['should' => [$query], 'cached' => empty($excludedCategories)]; + $childrenCategoriesIds = []; foreach ($childrenCategories as $childrenCategory) { - if (((bool) $childrenCategory->getIsVirtualCategory()) === true) { - $childrenQuery = $this->getCategorySearchQuery($childrenCategory, $excludedCategories); + $childrenCategoriesIds[] = $childrenCategory->getId(); + } + + foreach ($childrenCategories as $childrenCategory) { + $isChildrenVirtual = (bool) $childrenCategory->getIsVirtualCategory(); + if ($isChildrenVirtual) { + $childrenQuery = null; + $virtualRootCategory = $childrenCategory->getVirtualCategoryRoot(); + if (!$virtualRootCategory + || ( + !in_array($virtualRootCategory, $childrenCategoriesIds) + && $virtualRootCategory != $category->getId() + ) + ) { + $childrenQuery = $this->getCategorySearchQuery($childrenCategory, $excludedCategories); + } if ($childrenQuery !== null) { $queryParams['should'][] = $childrenQuery; } - } else { - $childrenCategoriesIds[] = $childrenCategory->getId(); + } elseif (!$isStandardCategory) { + $childrenStandardCategoriesIds[] = $childrenCategory->getId(); } } - if (!empty($childrenCategoriesIds)) { - $queryParams['should'][] = $this->getStandardCategoriesQuery($childrenCategoriesIds, $excludedCategories); + if (!empty($childrenStandardCategoriesIds)) { + $queryParams['should'][] = $this->getStandardCategoriesQuery($childrenStandardCategoriesIds, $excludedCategories); } if (count($queryParams['should']) > 1) { @@ -413,10 +429,6 @@ 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]); }