Skip to content

Commit d0eea07

Browse files
author
Valeriy Nayda
committed
MAGETWO-51716: [Performance Bug] Build all layered nav filters on every search query regardless of result set
1 parent 764431b commit d0eea07

File tree

15 files changed

+597
-18
lines changed

15 files changed

+597
-18
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Api;
8+
9+
interface AttributeSetFinderInterface
10+
{
11+
/**
12+
* Get attribute set ids by product ids
13+
*
14+
* @param array $productIds
15+
* @return array
16+
*/
17+
public function findAttributeSetIdsByProductIds(array $productIds);
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Attribute;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
11+
use Magento\Catalog\Api\AttributeSetFinderInterface;
12+
use Magento\Framework\DB\Select;
13+
14+
class AttributeSetFinder implements AttributeSetFinderInterface
15+
{
16+
/**
17+
* @var CollectionFactory
18+
*/
19+
private $productCollectionFactory;
20+
21+
/**
22+
* @param CollectionFactory $productCollectionFactory
23+
*/
24+
public function __construct(CollectionFactory $productCollectionFactory)
25+
{
26+
$this->productCollectionFactory = $productCollectionFactory;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function findAttributeSetIdsByProductIds(array $productIds)
33+
{
34+
/** @var $collection Collection */
35+
$collection = $this->productCollectionFactory->create();
36+
$select = $collection
37+
->getSelect()
38+
->reset(Select::COLUMNS)
39+
->columns(ProductInterface::ATTRIBUTE_SET_ID)
40+
->where('entity_id IN (?)', $productIds)
41+
->group(ProductInterface::ATTRIBUTE_SET_ID);
42+
$result = $collection->getConnection()->fetchCol($select);
43+
return $result;
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Model\Product\Attribute\AttributeSetFinder;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
17+
class AttributeSetFinderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $productCollection;
23+
24+
/**
25+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $productCollectionFactory;
28+
29+
/**
30+
* @var AttributeSetFinder
31+
*/
32+
protected $attributeSetFinder;
33+
34+
protected function setUp()
35+
{
36+
$this->productCollection = $this->getMockBuilder(Collection::class)
37+
->disableOriginalConstructor()
38+
->getMock();
39+
$this->productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
40+
->setMethods(['create'])
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->productCollectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
44+
45+
$this->attributeSetFinder = (new ObjectManager($this))->getObject(
46+
AttributeSetFinder::class,
47+
[
48+
'productCollectionFactory' => $this->productCollectionFactory,
49+
]
50+
);
51+
}
52+
53+
public function testFindAttributeIdsByProductIds()
54+
{
55+
$productIds = [1, 2, 3];
56+
$attributeSetIds = [3, 4, 6];
57+
58+
$select = $this->getMockBuilder(Select::class)
59+
->disableOriginalConstructor()
60+
->getMock();
61+
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)->willReturnSelf();
62+
$select->expects($this->once())->method('columns')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
63+
$select->expects($this->once())->method('where')->with('entity_id IN (?)', $productIds)->willReturnSelf();
64+
$select->expects($this->once())->method('group')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
65+
66+
$connection = $this->getMock(AdapterInterface::class);
67+
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn($attributeSetIds);
68+
69+
$this->productCollection->expects($this->once())->method('getSelect')->willReturn($select);
70+
$this->productCollection->expects($this->once())->method('getConnection')->willReturn($connection);
71+
72+
$this->assertEquals($attributeSetIds, $this->attributeSetFinder->findAttributeSetIdsByProductIds($productIds));
73+
}
74+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<preference for="Magento\Catalog\Api\AttributeSetManagementInterface" type="Magento\Catalog\Model\Product\Attribute\SetManagement" />
4444
<preference for="Magento\Catalog\Api\AttributeSetRepositoryInterface" type="Magento\Catalog\Model\Product\Attribute\SetRepository" />
4545
<preference for="Magento\Catalog\Api\ProductManagementInterface" type="Magento\Catalog\Model\ProductManagement" />
46+
<preference for="Magento\Catalog\Api\AttributeSetFinderInterface" type="Magento\Catalog\Model\Product\Attribute\AttributeSetFinder" />
4647
<type name="Magento\Customer\Model\ResourceModel\Visitor">
4748
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
4849
</type>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\Adapter\Aggregation;
7+
8+
use Magento\Catalog\Api\AttributeSetFinderInterface;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface;
12+
use Magento\Framework\Search\Request\BucketInterface;
13+
use Magento\Framework\Search\RequestInterface;
14+
15+
class AggregationResolver implements AggregationResolverInterface
16+
{
17+
/**
18+
* @var AttributeSetFinderInterface
19+
*/
20+
private $attributeSetFinder;
21+
22+
/**
23+
* @var ProductAttributeRepositoryInterface
24+
*/
25+
private $productAttributeRepository;
26+
27+
/**
28+
* @var SearchCriteriaBuilder
29+
*/
30+
private $searchCriteriaBuilder;
31+
32+
/**
33+
* AggregationResolver constructor
34+
*
35+
* @param AttributeSetFinderInterface $attributeSetFinder
36+
* @param ProductAttributeRepositoryInterface $productAttributeRepository
37+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
38+
*/
39+
public function __construct(
40+
AttributeSetFinderInterface $attributeSetFinder,
41+
ProductAttributeRepositoryInterface $productAttributeRepository,
42+
SearchCriteriaBuilder $searchCriteriaBuilder
43+
) {
44+
$this->attributeSetFinder = $attributeSetFinder;
45+
$this->productAttributeRepository = $productAttributeRepository;
46+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function resolve(RequestInterface $request, array $documentIds)
53+
{
54+
$attributeCodes = $this->getApplicableAttributeCodes($documentIds);
55+
56+
$resolvedAggregation = array_filter($request->getAggregation(), function ($bucket) use ($attributeCodes) {
57+
/** @var BucketInterface $bucket */
58+
return in_array($bucket->getField(), $attributeCodes);
59+
});
60+
return $resolvedAggregation;
61+
}
62+
63+
/**
64+
* Get applicable attributes
65+
*
66+
* @param array $documentIds
67+
* @return array
68+
*/
69+
private function getApplicableAttributeCodes(array $documentIds)
70+
{
71+
$attributeSetIds = $this->attributeSetFinder->findAttributeSetIdsByProductIds($documentIds);
72+
73+
$searchCriteria = $this->searchCriteriaBuilder
74+
->addFilter('attribute_set_id', $attributeSetIds, 'in')
75+
->create();
76+
$result = $this->productAttributeRepository->getList($searchCriteria);
77+
78+
$attributeCodes = [];
79+
foreach ($result->getItems() as $attribute) {
80+
$attributeCodes[] = $attribute->getAttributeCode();
81+
}
82+
return $attributeCodes;
83+
}
84+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Test\Unit\Model\Adapter\Aggregation;
7+
8+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\Data\ProductAttributeSearchResultsInterface;
10+
use Magento\CatalogSearch\Model\Adapter\Aggregation\AggregationResolver;
11+
use Magento\Catalog\Api\AttributeSetFinderInterface;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Framework\Api\SearchCriteriaInterface;
15+
use Magento\Framework\Search\Request\BucketInterface;
16+
use Magento\Framework\Search\RequestInterface;
17+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
18+
19+
class AggregationResolverTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var AttributeSetFinderInterface|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $attributeSetFinder;
25+
26+
/**
27+
* @var ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $productAttributeRepository;
30+
31+
/**
32+
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $searchCriteriaBuilder;
35+
36+
/**
37+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $request;
40+
41+
/**
42+
* @var AggregationResolver
43+
*/
44+
private $aggregationResolver;
45+
46+
protected function setUp()
47+
{
48+
$this->attributeSetFinder = $this->getMock(AttributeSetFinderInterface::class);
49+
$this->productAttributeRepository = $this->getMock(ProductAttributeRepositoryInterface::class);
50+
$this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->request = $this->getMock(RequestInterface::class);
54+
55+
$this->aggregationResolver = (new ObjectManager($this))->getObject(
56+
AggregationResolver::class,
57+
[
58+
'attributeSetFinder' => $this->attributeSetFinder,
59+
'productAttributeRepository' => $this->productAttributeRepository,
60+
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
61+
]
62+
);
63+
}
64+
65+
public function testResolve()
66+
{
67+
$documentIds = [1, 2, 3];
68+
$attributeSetIds = [4, 5];
69+
70+
$this->attributeSetFinder
71+
->expects($this->once())
72+
->method('findAttributeSetIdsByProductIds')
73+
->with($documentIds)
74+
->willReturn($attributeSetIds);
75+
76+
$searchCriteria = $this->getMock(SearchCriteriaInterface::class);
77+
78+
$this->searchCriteriaBuilder
79+
->expects($this->once())
80+
->method('addFilter')
81+
->with('attribute_set_id', $attributeSetIds, 'in')
82+
->willReturnSelf();
83+
$this->searchCriteriaBuilder
84+
->expects($this->once())
85+
->method('create')
86+
->willReturn($searchCriteria);
87+
88+
$attributeFirst = $this->getMock(ProductAttributeInterface::class);
89+
$attributeFirst->expects($this->once())
90+
->method('getAttributeCode')
91+
->willReturn('code_1');
92+
$attributeSecond = $this->getMock(ProductAttributeInterface::class);
93+
$attributeSecond->expects($this->once())
94+
->method('getAttributeCode')
95+
->willReturn('code_2');
96+
97+
$searchResult = $this->getMock(ProductAttributeSearchResultsInterface::class);
98+
$searchResult->expects($this->once())
99+
->method('getItems')
100+
->willReturn([$attributeFirst, $attributeSecond]);
101+
102+
$this->productAttributeRepository
103+
->expects($this->once())
104+
->method('getList')
105+
->with($searchCriteria)
106+
->willReturn($searchResult);
107+
108+
$bucketFirst = $this->getMock(BucketInterface::class);
109+
$bucketFirst->expects($this->once())
110+
->method('getField')
111+
->willReturn('code_1');
112+
$bucketSecond = $this->getMock(BucketInterface::class);
113+
$bucketSecond->expects($this->once())
114+
->method('getField')
115+
->willReturn('some_another_code');
116+
117+
$this->request->expects($this->once())
118+
->method('getAggregation')
119+
->willReturn([$bucketFirst, $bucketSecond]);
120+
121+
$this->assertEquals([$bucketFirst], $this->aggregationResolver->resolve($this->request, $documentIds));
122+
}
123+
}

app/code/Magento/CatalogSearch/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,11 @@
227227
</argument>
228228
</arguments>
229229
</type>
230+
<type name="Magento\Framework\Search\Adapter\Aggregation\AggregationResolver">
231+
<arguments>
232+
<argument name="resolvers" xsi:type="array">
233+
<item name="catalogsearch_fulltext" xsi:type="object">Magento\CatalogSearch\Model\Adapter\Aggregation\AggregationResolver</item>
234+
</argument>
235+
</arguments>
236+
</type>
230237
</config>

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<preference for="Magento\Framework\Search\Adapter\Mysql\Field\ResolverInterface" type="Magento\Framework\Search\Adapter\Mysql\Field\Resolver" />
1717
<preference for="Magento\Framework\Search\Request\Aggregation\StatusInterface" type="Magento\Framework\Search\Request\Aggregation\Status" />
1818
<preference for="Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface" type="Magento\Framework\Search\Adapter\Mysql\Field\Field"/>
19+
<preference for="Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface" type="Magento\Framework\Search\Adapter\Aggregation\AggregationResolver"/>
1920
<preference for="Magento\Framework\App\RequestInterface" type="Magento\Framework\App\Request\Http" />
2021
<preference for="Magento\Framework\App\Request\PathInfoProcessorInterface" type="Magento\Store\App\Request\PathInfoProcessor" />
2122
<preference for="Magento\Framework\App\ResponseInterface" type="Magento\Framework\App\Response\Http" />

0 commit comments

Comments
 (0)