2
2
3
3
namespace DsOpenSearchBundle \Command ;
4
4
5
- use DsOpenSearchBundle \Builder \ClientBuilderInterface ;
6
- use DsOpenSearchBundle \Service \IndexPersistenceService ;
7
- use DynamicSearchBundle \Builder \ContextDefinitionBuilderInterface ;
8
- use DynamicSearchBundle \Context \ContextDefinitionInterface ;
9
- use DynamicSearchBundle \Generator \IndexDocumentGeneratorInterface ;
10
- use DynamicSearchBundle \Provider \PreConfiguredIndexProviderInterface ;
5
+ use DsOpenSearchBundle \Manager \IndexManager ;
11
6
use Symfony \Component \Console \Command \Command ;
12
7
use Symfony \Component \Console \Helper \QuestionHelper ;
13
8
use Symfony \Component \Console \Input \InputInterface ;
@@ -22,9 +17,7 @@ class RebuildIndexCommand extends Command
22
17
protected static $ defaultDescription = 'Rebuild Index Mapping ' ;
23
18
24
19
public function __construct (
25
- protected ContextDefinitionBuilderInterface $ contextDefinitionBuilder ,
26
- protected IndexDocumentGeneratorInterface $ indexDocumentGenerator ,
27
- protected ClientBuilderInterface $ clientBuilder ,
20
+ protected IndexManager $ indexManager ,
28
21
protected TranslatorInterface $ translator
29
22
) {
30
23
parent ::__construct ();
@@ -41,75 +34,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
41
34
42
35
if (empty ($ contextName )) {
43
36
$ output ->writeln ('<error>no context definition name given</error> ' );
44
- return 0 ;
37
+ return Command:: FAILURE ;
45
38
}
46
39
47
- $ contextDefinition = $ this ->contextDefinitionBuilder ->buildContextDefinition ($ contextName , ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX );
40
+ /** @var QuestionHelper $helper */
41
+ $ helper = $ this ->getHelper ('question ' );
48
42
49
- if (!$ contextDefinition instanceof ContextDefinitionInterface) {
50
- $ output ->writeln (sprintf ('<error>no context definition with name "%s" found</error> ' , $ contextName ));
51
- return 0 ;
52
- }
53
-
54
- try {
55
- $ indexDocument = $ this ->indexDocumentGenerator ->generateWithoutData ($ contextDefinition , ['preConfiguredIndexProvider ' => true ]);
56
- } catch (\Throwable $ e ) {
57
- $ output ->writeln (
58
- sprintf (
59
- '%s. (The current context index provider also requires pre-configured indices. Please make sure your document definition implements the "%s" interface) ' ,
60
- $ e ->getMessage (), PreConfiguredIndexProviderInterface::class
61
- )
62
- );
63
-
64
- return 0 ;
65
- }
66
-
67
- if (!$ indexDocument ->hasIndexFields ()) {
68
- $ output ->writeln (
69
- sprintf (
70
- 'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface ' ,
71
- PreConfiguredIndexProviderInterface::class
72
- )
73
- );
74
-
75
- return 0 ;
76
- }
77
-
78
- $ options = $ contextDefinition ->getIndexProviderOptions ();
79
-
80
- $ client = $ this ->clientBuilder ->build ($ options );
81
- $ indexService = new IndexPersistenceService ($ client , $ options );
82
-
83
- if ($ indexService ->indexExists ()) {
84
-
85
- /** @var QuestionHelper $helper */
86
- $ helper = $ this ->getHelper ('question ' );
87
-
88
- $ text = $ this ->translator ->trans ('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message ' , [], 'admin ' );
89
- $ commandText = sprintf (' <info>%s (y/n)</info> [<comment>%s</comment>]: ' , $ text , 'no ' );
90
- $ question = new ConfirmationQuestion ($ commandText , false );
91
-
92
- if (!$ helper ->ask ($ input , $ output , $ question )) {
93
- return 0 ;
94
- }
43
+ $ text = $ this ->translator ->trans ('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message ' , [], 'admin ' );
44
+ $ commandText = sprintf (' <info>%s (y/n)</info> [<comment>%s</comment>]: ' , $ text , 'no ' );
45
+ $ question = new ConfirmationQuestion ($ commandText , false );
95
46
96
- try {
97
- $ indexService ->dropIndex ();
98
- } catch (\Throwable $ e ) {
99
- $ output ->writeln (sprintf ('Error while dropping index: %s ' , $ e ->getMessage ()));
100
- return 0 ;
101
- }
47
+ if (!$ helper ->ask ($ input , $ output , $ question )) {
48
+ return Command::SUCCESS ;
102
49
}
103
50
104
51
try {
105
- $ indexService -> createIndex ( $ indexDocument );
52
+ $ this -> indexManager -> rebuildIndexMapping ( $ contextName );
106
53
} catch (\Throwable $ e ) {
107
- $ output ->writeln (sprintf ('Error while creating index: %s ' , $ e ->getMessage ()));
108
- return 0 ;
54
+ $ output ->writeln (sprintf ('<error> Error rebuilding index mapping : %s</error> ' , $ e ->getMessage ()));
55
+ return Command:: FAILURE ;
109
56
}
110
57
111
58
$ output ->writeln (sprintf ('<info>%s</info> ' , $ this ->translator ->trans ('ds_index_provider_opensearch.actions.index.rebuild_mapping.success ' , [], 'admin ' )));
112
59
113
- return 0 ;
60
+ return Command:: SUCCESS ;
114
61
}
115
62
}
0 commit comments