66namespace Magento \CatalogSearch \Model \Indexer ;
77
88use Magento \CatalogSearch \Model \Indexer \Fulltext \Action \FullFactory ;
9- use Magento \CatalogSearch \Model \Indexer \Scope \State ;
9+ use Magento \CatalogSearch \Model \Indexer \Scope \StateFactory ;
1010use Magento \CatalogSearch \Model \ResourceModel \Fulltext as FulltextResource ;
11- use Magento \Framework \App \ObjectManager ;
12- use Magento \Framework \Search \Request \Config as SearchRequestConfig ;
13- use Magento \Framework \Search \Request \DimensionFactory ;
14- use Magento \Store \Model \StoreManagerInterface ;
11+ use Magento \Framework \Indexer \Dimension \DimensionProviderInterface ;
12+ use Magento \Store \Model \StoreDimensionProvider ;
1513
1614/**
1715 * Provide functionality for Fulltext Search indexing.
1816 *
1917 * @api
2018 * @since 100.0.2
2119 */
22- class Fulltext implements \Magento \Framework \Indexer \ActionInterface, \Magento \Framework \Mview \ActionInterface
20+ class Fulltext implements
21+ \Magento \Framework \Indexer \ActionInterface,
22+ \Magento \Framework \Mview \ActionInterface,
23+ \Magento \Framework \Indexer \Dimension \DimensionalIndexerInterface
2324{
2425 /**
2526 * Indexer ID in configuration
@@ -36,16 +37,6 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
3637 */
3738 private $ indexerHandlerFactory ;
3839
39- /**
40- * @var StoreManagerInterface
41- */
42- private $ storeManager ;
43-
44- /**
45- * @var \Magento\Framework\Search\Request\DimensionFactory
46- */
47- private $ dimensionFactory ;
48-
4940 /**
5041 * @var \Magento\CatalogSearch\Model\Indexer\Fulltext\Action\Full
5142 */
@@ -56,11 +47,6 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
5647 */
5748 private $ fulltextResource ;
5849
59- /**
60- * @var \Magento\Framework\Search\Request\Config
61- */
62- private $ searchRequestConfig ;
63-
6450 /**
6551 * @var IndexSwitcherInterface
6652 */
@@ -71,90 +57,97 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
7157 */
7258 private $ indexScopeState ;
7359
60+ /**
61+ * @var DimensionProviderInterface
62+ */
63+ private $ dimensionProvider ;
64+
7465 /**
7566 * @param FullFactory $fullActionFactory
7667 * @param IndexerHandlerFactory $indexerHandlerFactory
77- * @param StoreManagerInterface $storeManager
78- * @param DimensionFactory $dimensionFactory
7968 * @param FulltextResource $fulltextResource
80- * @param SearchRequestConfig $searchRequestConfig
8169 * @param array $data
8270 * @param IndexSwitcherInterface $indexSwitcher
83- * @param Scope\State $indexScopeState
71+ * @param StateFactory $indexScopeStateFactory
72+ * @param DimensionProviderInterface $dimensionProvider
8473 */
8574 public function __construct (
8675 FullFactory $ fullActionFactory ,
8776 IndexerHandlerFactory $ indexerHandlerFactory ,
88- StoreManagerInterface $ storeManager ,
89- DimensionFactory $ dimensionFactory ,
9077 FulltextResource $ fulltextResource ,
91- SearchRequestConfig $ searchRequestConfig ,
92- array $ data ,
93- IndexSwitcherInterface $ indexSwitcher = null ,
94- State $ indexScopeState = null
78+ IndexSwitcherInterface $ indexSwitcher ,
79+ StateFactory $ indexScopeStateFactory ,
80+ DimensionProviderInterface $ dimensionProvider ,
81+ array $ data
9582 ) {
9683 $ this ->fullAction = $ fullActionFactory ->create (['data ' => $ data ]);
9784 $ this ->indexerHandlerFactory = $ indexerHandlerFactory ;
98- $ this ->storeManager = $ storeManager ;
99- $ this ->dimensionFactory = $ dimensionFactory ;
10085 $ this ->fulltextResource = $ fulltextResource ;
101- $ this ->searchRequestConfig = $ searchRequestConfig ;
10286 $ this ->data = $ data ;
103- if (null === $ indexSwitcher ) {
104- $ indexSwitcher = ObjectManager::getInstance ()->get (IndexSwitcherInterface::class);
105- }
106- if (null === $ indexScopeState ) {
107- $ indexScopeState = ObjectManager::getInstance ()->get (State::class);
108- }
10987 $ this ->indexSwitcher = $ indexSwitcher ;
110- $ this ->indexScopeState = $ indexScopeState ;
88+ $ this ->indexScopeState = $ indexScopeStateFactory ->create ();
89+ $ this ->dimensionProvider = $ dimensionProvider ;
11190 }
11291
11392 /**
11493 * Execute materialization on ids entities
11594 *
116- * @param int[] $ids
95+ * @param int[] $entityIds
11796 * @return void
97+ * @throws \InvalidArgumentException
11898 */
119- public function execute ($ ids )
99+ public function execute ($ entityIds )
120100 {
121- $ storeIds = array_keys ($ this ->storeManager ->getStores ());
122- /** @var IndexerHandler $saveHandler */
123- $ saveHandler = $ this ->indexerHandlerFactory ->create ([
124- 'data ' => $ this ->data
125- ]);
126- foreach ($ storeIds as $ storeId ) {
127- $ dimension = $ this ->dimensionFactory ->create (['name ' => 'scope ' , 'value ' => $ storeId ]);
128- $ productIds = array_unique (array_merge ($ ids , $ this ->fulltextResource ->getRelationsByChild ($ ids )));
129- $ saveHandler ->deleteIndex ([$ dimension ], new \ArrayObject ($ productIds ));
130- $ saveHandler ->saveIndex ([$ dimension ], $ this ->fullAction ->rebuildStoreIndex ($ storeId , $ ids ));
101+ foreach ($ this ->dimensionProvider ->getIterator () as $ dimension ) {
102+ $ this ->executeByDimension ($ dimension , new \ArrayIterator ($ entityIds ));
131103 }
132104 }
133105
134106 /**
135- * Execute full indexation
136- *
137- * @return void
107+ * {@inheritdoc}
108+ * @throws \InvalidArgumentException
138109 */
139- public function executeFull ( )
110+ public function executeByDimension ( array $ dimensions , \ Traversable $ entityIds = null )
140111 {
141- $ storeIds = array_keys ($ this ->storeManager ->getStores ());
142- /** @var IndexerHandler $saveHandler */
112+ if (count ($ dimensions ) > 1 || !isset ($ dimensions [StoreDimensionProvider::DIMENSION_NAME ])) {
113+ throw new \InvalidArgumentException ('Indexer " ' . self ::INDEXER_ID . '" support only Store dimension ' );
114+ }
115+ $ storeId = $ dimensions [StoreDimensionProvider::DIMENSION_NAME ]->getValue ();
143116 $ saveHandler = $ this ->indexerHandlerFactory ->create ([
144117 'data ' => $ this ->data
145118 ]);
146- foreach ($ storeIds as $ storeId ) {
147- $ dimensions = [$ this ->dimensionFactory ->create (['name ' => 'scope ' , 'value ' => $ storeId ])];
148- $ this ->indexScopeState ->useTemporaryIndex ();
149119
120+ if (null === $ entityIds ) {
121+ $ this ->indexScopeState ->useTemporaryIndex ();
150122 $ saveHandler ->cleanIndex ($ dimensions );
151123 $ saveHandler ->saveIndex ($ dimensions , $ this ->fullAction ->rebuildStoreIndex ($ storeId ));
152124
153125 $ this ->indexSwitcher ->switchIndex ($ dimensions );
154126 $ this ->indexScopeState ->useRegularIndex ();
127+
128+ $ this ->fulltextResource ->resetSearchResultsByStore ($ storeId );
129+ } else {
130+ // internal implementation works only with array
131+ $ entityIds = iterator_to_array ($ entityIds );
132+ $ productIds = array_unique (
133+ array_merge ($ entityIds , $ this ->fulltextResource ->getRelationsByChild ($ entityIds ))
134+ );
135+ $ saveHandler ->deleteIndex ($ dimensions , new \ArrayIterator ($ productIds ));
136+ $ saveHandler ->saveIndex ($ dimensions , $ this ->fullAction ->rebuildStoreIndex ($ storeId , $ entityIds ));
137+ }
138+ }
139+
140+ /**
141+ * Execute full indexation
142+ *
143+ * @return void
144+ * @throws \InvalidArgumentException
145+ */
146+ public function executeFull ()
147+ {
148+ foreach ($ this ->dimensionProvider ->getIterator () as $ dimension ) {
149+ $ this ->executeByDimension ($ dimension );
155150 }
156- $ this ->fulltextResource ->resetSearchResults ();
157- $ this ->searchRequestConfig ->reset ();
158151 }
159152
160153 /**
0 commit comments