5
5
*/
6
6
namespace Magento \Elasticsearch \Model \Indexer ;
7
7
8
+ use Magento \Catalog \Model \Category ;
8
9
use Magento \CatalogSearch \Model \Indexer \Fulltext ;
9
10
use Magento \Elasticsearch \Model \Adapter \Elasticsearch as ElasticsearchAdapter ;
10
11
use Magento \Elasticsearch \Model \Adapter \Index \IndexNameResolver ;
15
16
use Magento \Framework \Indexer \SaveHandler \Batch ;
16
17
use Magento \Framework \Indexer \SaveHandler \IndexerInterface ;
17
18
use Magento \Framework \Search \Request \Dimension ;
19
+ use Magento \Framework \Indexer \CacheContext ;
18
20
19
21
/**
20
22
* Indexer Handler for Elasticsearch engine.
@@ -73,6 +75,11 @@ class IndexerHandler implements IndexerInterface
73
75
*/
74
76
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/ ' ;
75
77
78
+ /**
79
+ * @var CacheContext
80
+ */
81
+ private $ cacheContext ;
82
+
76
83
/**
77
84
* IndexerHandler constructor.
78
85
* @param IndexStructureInterface $indexStructure
@@ -83,6 +90,7 @@ class IndexerHandler implements IndexerInterface
83
90
* @param array $data
84
91
* @param int $batchSize
85
92
* @param DeploymentConfig|null $deploymentConfig
93
+ * @param CacheContext|null $cacheContext
86
94
*/
87
95
public function __construct (
88
96
IndexStructureInterface $ indexStructure ,
@@ -91,8 +99,9 @@ public function __construct(
91
99
Batch $ batch ,
92
100
ScopeResolverInterface $ scopeResolver ,
93
101
array $ data = [],
94
- $ batchSize = self ::DEFAULT_BATCH_SIZE ,
95
- ?DeploymentConfig $ deploymentConfig = null
102
+ int $ batchSize = self ::DEFAULT_BATCH_SIZE ,
103
+ ?DeploymentConfig $ deploymentConfig = null ,
104
+ ?CacheContext $ cacheContext = null
96
105
) {
97
106
$ this ->indexStructure = $ indexStructure ;
98
107
$ this ->adapter = $ adapter ;
@@ -102,6 +111,7 @@ public function __construct(
102
111
$ this ->batchSize = $ batchSize ;
103
112
$ this ->scopeResolver = $ scopeResolver ;
104
113
$ this ->deploymentConfig = $ deploymentConfig ?: ObjectManager::getInstance ()->get (DeploymentConfig::class);
114
+ $ this ->cacheContext = $ cacheContext ?: ObjectManager::getInstance ()->get (CacheContext::class);
105
115
}
106
116
107
117
/**
@@ -119,11 +129,30 @@ public function saveIndex($dimensions, \Traversable $documents)
119
129
foreach ($ this ->batch ->getItems ($ documents , $ this ->batchSize ) as $ documentsBatch ) {
120
130
$ docs = $ this ->adapter ->prepareDocsPerStore ($ documentsBatch , $ scopeId );
121
131
$ this ->adapter ->addDocs ($ docs , $ scopeId , $ this ->getIndexerId ());
132
+ $ this ->updateCacheContext ($ docs );
122
133
}
123
134
$ this ->adapter ->updateAlias ($ scopeId , $ this ->getIndexerId ());
124
135
return $ this ;
125
136
}
126
137
138
+ /**
139
+ * Add category cache tags for the affected products to the cache context
140
+ *
141
+ * @param array $docs
142
+ * @return void
143
+ */
144
+ private function updateCacheContext (array $ docs ) : void
145
+ {
146
+ $ categoryIds = [];
147
+ foreach ($ docs as $ document ) {
148
+ array_push ($ categoryIds , ...$ document ['category_ids ' ]);
149
+ }
150
+ if (!empty ($ categoryIds )) {
151
+ $ categoryIds = array_unique ($ categoryIds );
152
+ $ this ->cacheContext ->registerEntities (Category::CACHE_TAG , $ categoryIds );
153
+ }
154
+ }
155
+
127
156
/**
128
157
* @inheritdoc
129
158
*/
0 commit comments