1111 * source code.
1212 */
1313
14+ use Flowpack \ElasticSearch \ContentRepositoryAdaptor \LoggerInterface ;
1415use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Mapping \NodeTypeMappingBuilder ;
1516use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Service \IndexWorkspaceTrait ;
17+ use Flowpack \ElasticSearch \Domain \Model \Mapping ;
18+ use Flowpack \ElasticSearch \Transfer \Exception \ApiException ;
19+ use Symfony \Component \Yaml \Yaml ;
1620use TYPO3 \Flow \Annotations as Flow ;
1721use TYPO3 \Flow \Cli \CommandController ;
22+ use TYPO3 \Flow \Configuration \ConfigurationManager ;
23+ use TYPO3 \Flow \Object \ObjectManagerInterface ;
1824use TYPO3 \Neos \Controller \CreateContentContextTrait ;
1925use TYPO3 \TYPO3CR \Domain \Model \Workspace ;
26+ use TYPO3 \TYPO3CR \Domain \Repository \NodeDataRepository ;
27+ use TYPO3 \TYPO3CR \Domain \Repository \WorkspaceRepository ;
28+ use TYPO3 \TYPO3CR \Domain \Service \ContentDimensionPresetSourceInterface ;
29+ use TYPO3 \TYPO3CR \Search \Indexer \NodeIndexerInterface ;
2030
2131/**
2232 * Provides CLI features for index handling
@@ -30,19 +40,19 @@ class NodeIndexCommandController extends CommandController
3040
3141 /**
3242 * @Flow\Inject
33- * @var \TYPO3\TYPO3CR\Search\Indexer\ NodeIndexerInterface
43+ * @var NodeIndexerInterface
3444 */
3545 protected $ nodeIndexer ;
3646
3747 /**
3848 * @Flow\Inject
39- * @var \TYPO3\TYPO3CR\Domain\Repository\ WorkspaceRepository
49+ * @var WorkspaceRepository
4050 */
4151 protected $ workspaceRepository ;
4252
4353 /**
4454 * @Flow\Inject
45- * @var \TYPO3\TYPO3CR\Domain\Repository\ NodeDataRepository
55+ * @var NodeDataRepository
4656 */
4757 protected $ nodeDataRepository ;
4858
@@ -54,7 +64,7 @@ class NodeIndexCommandController extends CommandController
5464
5565 /**
5666 * @Flow\Inject
57- * @var \TYPO3\TYPO3CR\Domain\Service\ ContentDimensionPresetSourceInterface
67+ * @var ContentDimensionPresetSourceInterface
5868 */
5969 protected $ contentDimensionPresetSource ;
6070
@@ -66,13 +76,13 @@ class NodeIndexCommandController extends CommandController
6676
6777 /**
6878 * @Flow\Inject
69- * @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\ LoggerInterface
79+ * @var LoggerInterface
7080 */
7181 protected $ logger ;
7282
7383 /**
7484 * @Flow\Inject
75- * @var \TYPO3\Flow\Configuration\ ConfigurationManager
85+ * @var ConfigurationManager
7686 */
7787 protected $ configurationManager ;
7888
@@ -88,8 +98,8 @@ class NodeIndexCommandController extends CommandController
8898 */
8999 public function initializeObject ($ cause )
90100 {
91- if ($ cause === \ TYPO3 \ Flow \ Object \ ObjectManagerInterface::INITIALIZATIONCAUSE_CREATED ) {
92- $ this ->settings = $ this ->configurationManager ->getConfiguration (\ TYPO3 \ Flow \ Configuration \ ConfigurationManager::CONFIGURATION_TYPE_SETTINGS , 'TYPO3.TYPO3CR.Search ' );
101+ if ($ cause === ObjectManagerInterface::INITIALIZATIONCAUSE_CREATED ) {
102+ $ this ->settings = $ this ->configurationManager ->getConfiguration (ConfigurationManager::CONFIGURATION_TYPE_SETTINGS , 'TYPO3.TYPO3CR.Search ' );
93103 }
94104 }
95105
@@ -102,8 +112,8 @@ public function showMappingCommand()
102112 {
103113 $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
104114 foreach ($ nodeTypeMappingCollection as $ mapping ) {
105- /** @var \Flowpack\ElasticSearch\Domain\Model\ Mapping $mapping */
106- $ this ->output (\ Symfony \ Component \ Yaml \ Yaml::dump ($ mapping ->asArray (), 5 , 2 ));
115+ /** @var Mapping $mapping */
116+ $ this ->output (Yaml::dump ($ mapping ->asArray (), 5 , 2 ));
107117 $ this ->outputLine ();
108118 }
109119 $ this ->outputLine ('------------ ' );
@@ -181,6 +191,7 @@ public function indexNodeCommand($identifier, $workspace = null)
181191 $ indexInWorkspace ($ identifier , $ workspace );
182192 }
183193 } else {
194+ /** @var Workspace $workspaceInstance */
184195 $ workspaceInstance = $ this ->workspaceRepository ->findByIdentifier ($ workspace );
185196 if ($ workspaceInstance === null ) {
186197 $ this ->outputLine ('The given workspace (%s) does not exist. ' , [$ workspace ]);
@@ -198,7 +209,7 @@ public function indexNodeCommand($identifier, $workspace = null)
198209 * @param integer $limit Amount of nodes to index at maximum
199210 * @param boolean $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
200211 * @param string $workspace name of the workspace which should be indexed
201- * @param string $postfix Index postfix, index with the same postifix will be deleted if exist
212+ * @param string $postfix Index postfix, index with the same postfix will be deleted if exist
202213 * @return void
203214 */
204215 public function buildCommand ($ limit = null , $ update = false , $ workspace = null , $ postfix = null )
@@ -211,21 +222,9 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
211222 if ($ update === true ) {
212223 $ this ->logger ->log ('!!! Update Mode (Development) active! ' , LOG_INFO );
213224 } else {
214- $ this ->nodeIndexer ->setIndexNamePostfix ($ postfix ?: time ());
215- if ($ this ->nodeIndexer ->getIndex ()->exists () === true ) {
216- $ this ->logger ->log (sprintf ('Deleted index with the same postfix (%s)! ' , $ postfix ), LOG_WARNING );
217- $ this ->nodeIndexer ->getIndex ()->delete ();
218- }
219- $ this ->nodeIndexer ->getIndex ()->create ();
220- $ this ->logger ->log ('Created index ' . $ this ->nodeIndexer ->getIndexName (), LOG_INFO );
221-
222- $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
223- foreach ($ nodeTypeMappingCollection as $ mapping ) {
224- /** @var \Flowpack\ElasticSearch\Domain\Model\Mapping $mapping */
225- $ mapping ->apply ();
226- }
227- $ this ->logger ->log ('Updated Mapping. ' , LOG_INFO );
225+ $ this ->createNewIndex ($ postfix );
228226 }
227+ $ this ->applyMapping ();
229228
230229 $ this ->logger ->log (sprintf ('Indexing %snodes ... ' , ($ limit !== null ? 'the first ' . $ limit . ' ' : '' )), LOG_INFO );
231230
@@ -277,9 +276,41 @@ public function cleanupCommand()
277276 } else {
278277 $ this ->logger ->log ('Nothing to remove. ' );
279278 }
280- } catch (\ Flowpack \ ElasticSearch \ Transfer \ Exception \ ApiException $ exception ) {
279+ } catch (ApiException $ exception ) {
281280 $ response = json_decode ($ exception ->getResponse ());
282281 $ this ->logger ->log (sprintf ('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s" ' , $ response ->status , $ response ->error ->type , $ response ->error ->reason ));
283282 }
284283 }
284+
285+ /**
286+ * Create a new index with the given $postfix.
287+ *
288+ * @param string $postfix
289+ * @return void
290+ */
291+ protected function createNewIndex ($ postfix )
292+ {
293+ $ this ->nodeIndexer ->setIndexNamePostfix ($ postfix ?: time ());
294+ if ($ this ->nodeIndexer ->getIndex ()->exists () === true ) {
295+ $ this ->logger ->log (sprintf ('Deleted index with the same postfix (%s)! ' , $ postfix ), LOG_WARNING );
296+ $ this ->nodeIndexer ->getIndex ()->delete ();
297+ }
298+ $ this ->nodeIndexer ->getIndex ()->create ();
299+ $ this ->logger ->log ('Created index ' . $ this ->nodeIndexer ->getIndexName (), LOG_INFO );
300+ }
301+
302+ /**
303+ * Apply the mapping to the current index.
304+ *
305+ * @return void
306+ */
307+ protected function applyMapping ()
308+ {
309+ $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
310+ foreach ($ nodeTypeMappingCollection as $ mapping ) {
311+ /** @var Mapping $mapping */
312+ $ mapping ->apply ();
313+ }
314+ $ this ->logger ->log ('Updated Mapping. ' , LOG_INFO );
315+ }
285316}
0 commit comments