Skip to content

Commit a053872

Browse files
committed
MAGETWO-58174: When catalog is being indexed it should index in place or leverage an index alias so store can still function during a long index run
- Refactor to use adapter-specific index switchers
1 parent 630c345 commit a053872

File tree

5 files changed

+235
-2
lines changed

5 files changed

+235
-2
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/IndexSwitcherProxy.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Framework\ObjectManagerInterface;
1111
use Magento\Store\Model\ScopeInterface;
1212

13+
/**
14+
* Proxy for adapter-specific index switcher
15+
*/
1316
class IndexSwitcherProxy implements IndexSwitcherInterface
1417
{
1518
/**
@@ -59,7 +62,7 @@ public function __construct(
5962
}
6063

6164
/**
62-
* @inheritDoc
65+
* {@inheritDoc}
6366
*
6467
* As index switcher is an optional part of the search SPI, it may be not defined by a search engine.
6568
* It is especially reasonable for search engines with pre-defined indexes declaration (like old SOLR and Sphinx)

app/code/Magento/CatalogSearch/Model/Indexer/Scope/ScopeProxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ScopeProxy implements \Magento\Framework\Search\Request\IndexScopeResolver
2020
*
2121
* @var \Magento\Framework\ObjectManagerInterface
2222
*/
23-
protected $objectManager = null;
23+
private $objectManager;
2424

2525
/**
2626
* @var array
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Model\Indexer;
8+
9+
10+
use Magento\CatalogSearch\Model\Indexer\IndexSwitcherInterface;
11+
12+
class IndexSwitcherMock extends \PHPUnit_Framework_Assert implements IndexSwitcherInterface
13+
{
14+
private $isSwitched = false;
15+
16+
/**
17+
* @var \Magento\CatalogSearch\Model\Indexer\IndexSwitcherInterface
18+
*/
19+
private $indexSwitcher;
20+
21+
/**
22+
* @param \Magento\CatalogSearch\Model\Indexer\IndexSwitcherInterface $indexSwitcher
23+
*/
24+
public function __construct(
25+
IndexSwitcherInterface $indexSwitcher
26+
) {
27+
$this->indexSwitcher = $indexSwitcher;
28+
}
29+
30+
31+
/**
32+
* Switch current index with temporary index
33+
*
34+
* It will drop current index table and rename temporary index table to the current index table.
35+
*
36+
* @param array $dimensions
37+
* @return void
38+
*/
39+
public function switchIndex(array $dimensions)
40+
{
41+
$this->isSwitched = true;
42+
$this->indexSwitcher->switchIndex($dimensions);
43+
}
44+
45+
/**
46+
* @return bool
47+
*/
48+
public function isSwitched()
49+
{
50+
return $this->isSwitched;
51+
}
52+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\Indexer;
7+
8+
use Magento\Catalog\Model\Product;
9+
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
/**
13+
* @magentoDbIsolation disabled
14+
* @magentoDataFixture Magento/CatalogSearch/_files/indexer_fulltext.php
15+
*/
16+
class SwitcherUsedInFulltextTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var \Magento\Framework\Indexer\IndexerInterface
20+
*/
21+
protected $indexer;
22+
23+
/**
24+
* @var \Magento\CatalogSearch\Model\ResourceModel\Engine
25+
*/
26+
protected $engine;
27+
28+
/**
29+
* @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext
30+
*/
31+
protected $resourceFulltext;
32+
33+
/**
34+
* @var \Magento\CatalogSearch\Model\Fulltext
35+
*/
36+
protected $fulltext;
37+
38+
/**
39+
* @var \Magento\Search\Model\QueryFactory
40+
*/
41+
protected $queryFactory;
42+
43+
/**
44+
* @var Product
45+
*/
46+
protected $productApple;
47+
48+
/**
49+
* @var Product
50+
*/
51+
protected $productBanana;
52+
53+
/**
54+
* @var Product
55+
*/
56+
protected $productOrange;
57+
58+
/**
59+
* @var Product
60+
*/
61+
protected $productPapaya;
62+
63+
/**
64+
* @var Product
65+
*/
66+
protected $productCherry;
67+
68+
/**
69+
* @var \Magento\Framework\Search\Request\Dimension
70+
*/
71+
protected $dimension;
72+
73+
protected function setUp()
74+
{
75+
/** @var \Magento\Framework\Indexer\IndexerInterface indexer */
76+
$this->indexer = Bootstrap::getObjectManager()->create(
77+
\Magento\Indexer\Model\Indexer::class
78+
);
79+
$this->indexer->load('catalogsearch_fulltext');
80+
81+
$objectManager = Bootstrap::getObjectManager();
82+
$this->engine = $objectManager->get(
83+
\Magento\CatalogSearch\Model\ResourceModel\Engine::class
84+
);
85+
86+
$this->resourceFulltext = Bootstrap::getObjectManager()->get(
87+
\Magento\CatalogSearch\Model\ResourceModel\Fulltext::class
88+
);
89+
90+
$this->queryFactory = Bootstrap::getObjectManager()->get(
91+
\Magento\Search\Model\QueryFactory::class
92+
);
93+
94+
$this->dimension = Bootstrap::getObjectManager()->create(
95+
\Magento\Framework\Search\Request\Dimension::class,
96+
['name' => 'scope', 'value' => '1']
97+
);
98+
99+
$this->productApple = $this->getProductBySku('fulltext-1');
100+
$this->productBanana = $this->getProductBySku('fulltext-2');
101+
$this->productOrange = $this->getProductBySku('fulltext-3');
102+
$this->productPapaya = $this->getProductBySku('fulltext-4');
103+
$this->productCherry = $this->getProductBySku('fulltext-5');
104+
}
105+
106+
public function testReindexAll()
107+
{
108+
$this->indexer->reindexAll();
109+
110+
$products = $this->search('Apple');
111+
$this->assertCount(1, $products);
112+
$this->assertEquals($this->productApple->getId(), $products[0]->getId());
113+
114+
$products = $this->search('Simple Product');
115+
$this->assertCount(5, $products);
116+
/** @var \Magento\CatalogSearch\Model\Indexer\IndexSwitcherMock $indexSwitcher */
117+
$indexSwitcher = Bootstrap::getObjectManager()->get(
118+
\Magento\CatalogSearch\Model\Indexer\IndexSwitcherMock::class
119+
);
120+
$this->assertTrue($indexSwitcher->isSwitched());
121+
}
122+
123+
/**
124+
* Search the text and return result collection
125+
*
126+
* @param string $text
127+
* @return Product[]
128+
*/
129+
protected function search($text)
130+
{
131+
$this->resourceFulltext->resetSearchResults();
132+
$query = $this->queryFactory->get();
133+
$query->unsetData();
134+
$query->setQueryText($text);
135+
$query->saveIncrementalPopularity();
136+
$products = [];
137+
$collection = Bootstrap::getObjectManager()->create(
138+
Collection::class,
139+
[
140+
'searchRequestName' => 'quick_search_container'
141+
]
142+
);
143+
$collection->addSearchFilter($text);
144+
foreach ($collection as $product) {
145+
$products[] = $product;
146+
}
147+
return $products;
148+
}
149+
150+
/**
151+
* Return product by SKU
152+
*
153+
* @param string $sku
154+
* @return Product
155+
*/
156+
protected function getProductBySku($sku)
157+
{
158+
/** @var Product $product */
159+
$product = Bootstrap::getObjectManager()->get(
160+
\Magento\Catalog\Model\Product::class
161+
);
162+
return $product->loadByAttribute('sku', $sku);
163+
}
164+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="\Magento\CatalogSearch\Model\Indexer\Fulltext">
10+
<arguments>
11+
<argument name="indexSwitcher" xsi:type="object">\Magento\CatalogSearch\Model\Indexer\IndexSwitcherMock</argument>
12+
</arguments>
13+
</type>
14+
</config>

0 commit comments

Comments
 (0)