1515use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Service \IndexWorkspaceTrait ;
1616use Neos \Flow \Annotations as Flow ;
1717use Neos \Flow \Cli \CommandController ;
18- use Neos \ContentRepository \Domain \Model \NodeInterface ;
18+ use Neos \Neos \Controller \CreateContentContextTrait ;
19+ use Neos \ContentRepository \Domain \Model \Workspace ;
1920
2021/**
2122 * Provides CLI features for index handling
2526class NodeIndexCommandController extends CommandController
2627{
2728 use IndexWorkspaceTrait;
29+ use CreateContentContextTrait;
2830
2931 /**
3032 * @Flow\Inject
@@ -62,11 +64,6 @@ class NodeIndexCommandController extends CommandController
6264 */
6365 protected $ nodeTypeMappingBuilder ;
6466
65- /**
66- * @var integer
67- */
68- protected $ limit ;
69-
7067 /**
7168 * @Flow\Inject
7269 * @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
@@ -131,6 +128,64 @@ public function showMappingCommand()
131128 }
132129 }
133130
131+ /**
132+ * Index a single node by the given identifier and workspace name
133+ *
134+ * @param string $identifier
135+ * @param string $workspace
136+ * @return void
137+ */
138+ public function indexNodeCommand ($ identifier , $ workspace = null )
139+ {
140+ if ($ workspace === null && $ this ->settings ['indexAllWorkspaces ' ] === false ) {
141+ $ workspace = 'live ' ;
142+ }
143+
144+ $ indexNode = function ($ identifier , Workspace $ workspace , array $ dimensions ) {
145+ $ context = $ this ->createContentContext ($ workspace ->getName (), $ dimensions );
146+ $ node = $ context ->getNodeByIdentifier ($ identifier );
147+ if ($ node === null ) {
148+ $ this ->outputLine ('Node with the given identifier is not found. ' );
149+ $ this ->quit ();
150+ }
151+ $ this ->outputLine ();
152+ $ this ->outputLine ('Index node "%s" (%s) ' , [
153+ $ node ->getLabel (),
154+ $ node ->getIdentifier (),
155+ ]);
156+ $ this ->outputLine (' workspace: %s ' , [
157+ $ workspace ->getName ()
158+ ]);
159+ $ this ->outputLine (' node type: %s ' , [
160+ $ node ->getNodeType ()->getName ()
161+ ]);
162+ $ this ->outputLine (' dimensions: %s ' , [
163+ json_encode ($ dimensions )
164+ ]);
165+ $ this ->nodeIndexer ->indexNode ($ node );
166+ };
167+
168+ $ indexInWorkspace = function ($ identifier , Workspace $ workspace ) use ($ indexNode ) {
169+ $ combinations = $ this ->contentDimensionCombinator ->getAllAllowedCombinations ();
170+ if ($ combinations === []) {
171+ $ indexNode ($ identifier , $ workspace , []);
172+ } else {
173+ foreach ($ combinations as $ combination ) {
174+ $ indexNode ($ identifier , $ workspace , $ combination );
175+ }
176+ }
177+ };
178+
179+ if ($ workspace === null ) {
180+ foreach ($ this ->workspaceRepository ->findAll () as $ workspace ) {
181+ $ indexInWorkspace ($ identifier , $ workspace );
182+ }
183+ } else {
184+ $ workspace = $ this ->workspaceRepository ->findByIdentifier ($ workspace );
185+ $ indexInWorkspace ($ identifier , $ workspace );
186+ }
187+ }
188+
134189 /**
135190 * Index all nodes by creating a new index and when everything was completed, switch the index alias.
136191 *
@@ -166,7 +221,6 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
166221 $ this ->logger ->log (sprintf ('Indexing %snodes ... ' , ($ limit !== null ? 'the first ' . $ limit . ' ' : '' )), LOG_INFO );
167222
168223 $ count = 0 ;
169- $ this ->limit = $ limit ;
170224
171225 if ($ workspace === null && $ this ->settings ['indexAllWorkspaces ' ] === false ) {
172226 $ workspace = 'live ' ;
@@ -181,10 +235,10 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
181235 };
182236 if ($ workspace === null ) {
183237 foreach ($ this ->workspaceRepository ->findAll () as $ workspace ) {
184- $ count += $ this ->indexWorkspace ($ workspace ->getName ());
238+ $ count += $ this ->indexWorkspace ($ workspace ->getName (), $ limit , $ callback );
185239 }
186240 } else {
187- $ count = $ this ->indexWorkspace ($ workspace );
241+ $ count + = $ this ->indexWorkspace ($ workspace, $ limit , $ callback );
188242 }
189243
190244 $ this ->nodeIndexingManager ->flushQueues ();
@@ -206,81 +260,17 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
206260 public function cleanupCommand ()
207261 {
208262 try {
209- $ removedIndices = $ this ->nodeIndexer ->removeOldIndices ();
210- if (count ($ removedIndices ) > 0 ) {
211- if (count ($ removedIndices ) === 1 ) {
212- $ this ->logger ->log ('Removed old index ' . current ($ removedIndices ) . '. ' );
213- } else {
214- $ this ->logger ->log ('Removed old indices ' . implode (', ' , $ removedIndices ) . '. ' );
263+ $ indicesToBeRemoved = $ this ->nodeIndexer ->removeOldIndices ();
264+ if (count ($ indicesToBeRemoved ) > 0 ) {
265+ foreach ($ indicesToBeRemoved as $ indexToBeRemoved ) {
266+ $ this ->logger ->log ('Removing old index ' . $ indexToBeRemoved );
215267 }
216268 } else {
217269 $ this ->logger ->log ('Nothing to remove. ' );
218270 }
219271 } catch (\Flowpack \ElasticSearch \Transfer \Exception \ApiException $ exception ) {
220272 $ response = json_decode ($ exception ->getResponse ());
221- $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s " ' , $ response ->status , $ response ->error -> type , $ response -> error -> reason ));
273+ $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s" ' , $ response ->status , $ response ->error ));
222274 }
223275 }
224-
225- /**
226- * @param string $workspaceName
227- * @return int
228- */
229- protected function indexWorkspace ($ workspaceName )
230- {
231- $ indexedNodes = 0 ;
232- $ combinations = $ this ->contentDimensionCombinator ->getAllAllowedCombinations ();
233- if ($ combinations === []) {
234- $ indexedNodes += $ this ->indexWorkspaceWithDimensions ($ workspaceName );
235- } else {
236- foreach ($ combinations as $ combination ) {
237- $ indexedNodes += $ this ->indexWorkspaceWithDimensions ($ workspaceName , $ combination );
238- }
239- }
240-
241- return $ indexedNodes ;
242- }
243-
244- /**
245- * @param string $workspaceName
246- * @param array $dimensions
247- * @return int
248- */
249- protected function indexWorkspaceWithDimensions ($ workspaceName , array $ dimensions = [])
250- {
251- $ indexedNodes = 0 ;
252- $ context = $ this ->contextFactory ->create (['workspaceName ' => $ workspaceName , 'dimensions ' => $ dimensions ]);
253- $ rootNode = $ context ->getRootNode ();
254-
255- $ indexedNodes += $ this ->traverseNodes ($ rootNode );
256-
257- if ($ dimensions === []) {
258- $ this ->outputLine ('Workspace " ' . $ workspaceName . '" without dimensions done. (Indexed ' . $ indexedNodes . ' nodes) ' );
259- } else {
260- $ this ->outputLine ('Workspace " ' . $ workspaceName . '" and dimensions " ' . json_encode ($ dimensions ) . '" done. (Indexed ' . $ indexedNodes . ' nodes) ' );
261- }
262-
263- return $ indexedNodes ;
264- }
265-
266- /**
267- * @param NodeInterface $currentNode
268- * @param integer $traversedUntilNow
269- * @return integer Indexed nodes in this traversal
270- */
271- protected function traverseNodes (NodeInterface $ currentNode , $ traversedUntilNow = 0 )
272- {
273- if ($ this ->limit !== null && $ traversedUntilNow > $ this ->limit ) {
274- return $ traversedUntilNow ;
275- }
276-
277- $ this ->nodeIndexingManager ->indexNode ($ currentNode );
278- $ traversedUntilNow ++;
279-
280- foreach ($ currentNode ->getChildNodes () as $ childNode ) {
281- $ traversedUntilNow = $ this ->traverseNodes ($ childNode , $ traversedUntilNow );
282- }
283-
284- return $ traversedUntilNow ;
285- }
286276}
0 commit comments