6
6
7
7
use Doctrine \Persistence \ManagerRegistry ;
8
8
use Meilisearch \Bundle \Collection ;
9
- use Meilisearch \Bundle \Exception \ InvalidSettingName ;
9
+ use Meilisearch \Bundle \EventListener \ ConsoleOutputSubscriber ;
10
10
use Meilisearch \Bundle \Exception \TaskException ;
11
11
use Meilisearch \Bundle \Model \Aggregator ;
12
12
use Meilisearch \Bundle \SearchService ;
13
- use Meilisearch \Bundle \SettingsProvider ;
13
+ use Meilisearch \Bundle \Services \ SettingsUpdater ;
14
14
use Meilisearch \Client ;
15
15
use Meilisearch \Exceptions \TimeOutException ;
16
16
use Symfony \Component \Console \Input \InputInterface ;
17
17
use Symfony \Component \Console \Input \InputOption ;
18
18
use Symfony \Component \Console \Output \OutputInterface ;
19
+ use Symfony \Component \Console \Style \SymfonyStyle ;
20
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
19
21
20
22
final class MeilisearchImportCommand extends IndexCommand
21
23
{
22
- private const DEFAULT_RESPONSE_TIMEOUT = 5000 ;
24
+ private Client $ searchClient ;
25
+ private ManagerRegistry $ managerRegistry ;
26
+ private SettingsUpdater $ settingsUpdater ;
27
+ private EventDispatcherInterface $ eventDispatcher ;
23
28
24
- protected Client $ searchClient ;
25
- protected ManagerRegistry $ managerRegistry ;
26
-
27
- public function __construct (SearchService $ searchService , ManagerRegistry $ managerRegistry , Client $ searchClient )
29
+ public function __construct (SearchService $ searchService , ManagerRegistry $ managerRegistry , Client $ searchClient , SettingsUpdater $ settingsUpdater , EventDispatcherInterface $ eventDispatcher )
28
30
{
29
31
parent ::__construct ($ searchService );
30
32
31
33
$ this ->managerRegistry = $ managerRegistry ;
32
34
$ this ->searchClient = $ searchClient ;
35
+ $ this ->settingsUpdater = $ settingsUpdater ;
36
+ $ this ->eventDispatcher = $ eventDispatcher ;
33
37
}
34
38
35
39
public static function getDefaultName (): string
@@ -50,8 +54,9 @@ protected function configure(): void
50
54
->addOption (
51
55
'update-settings ' ,
52
56
null ,
53
- InputOption::VALUE_NONE ,
54
- 'Update settings related to indices to the search engine '
57
+ InputOption::VALUE_NEGATABLE ,
58
+ 'Update settings related to indices to the search engine ' ,
59
+ true
55
60
)
56
61
->addOption ('batch-size ' , null , InputOption::VALUE_REQUIRED )
57
62
->addOption (
@@ -73,32 +78,20 @@ protected function configure(): void
73
78
74
79
protected function execute (InputInterface $ input , OutputInterface $ output ): int
75
80
{
81
+ $ this ->eventDispatcher ->addSubscriber (new ConsoleOutputSubscriber (new SymfonyStyle ($ input , $ output )));
82
+
76
83
$ indexes = $ this ->getEntitiesFromArgs ($ input , $ output );
84
+ $ entitiesToIndex = $ this ->entitiesToIndex ($ indexes );
77
85
$ config = $ this ->searchService ->getConfiguration ();
78
-
79
- foreach ($ indexes as $ key => $ index ) {
80
- $ entityClassName = $ index ['class ' ];
81
- if (is_subclass_of ($ entityClassName , Aggregator::class)) {
82
- $ indexes ->forget ($ key );
83
-
84
- $ indexes = new Collection (array_merge (
85
- $ indexes ->all (),
86
- array_map (
87
- fn ($ entity ) => ['class ' => $ entity ],
88
- $ entityClassName ::getEntities ()
89
- )
90
- ));
91
- }
92
- }
93
-
94
- $ entitiesToIndex = array_unique ($ indexes ->all (), SORT_REGULAR );
86
+ $ updateSettings = $ input ->getOption ('update-settings ' );
95
87
$ batchSize = $ input ->getOption ('batch-size ' ) ?? '' ;
96
88
$ batchSize = ctype_digit ($ batchSize ) ? (int ) $ batchSize : $ config ->get ('batchSize ' );
97
89
$ responseTimeout = ((int ) $ input ->getOption ('response-timeout ' )) ?: self ::DEFAULT_RESPONSE_TIMEOUT ;
98
90
99
91
/** @var array $index */
100
92
foreach ($ entitiesToIndex as $ index ) {
101
93
$ entityClassName = $ index ['class ' ];
94
+
102
95
if (!$ this ->searchService ->isSearchable ($ entityClassName )) {
103
96
continue ;
104
97
}
@@ -148,36 +141,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
148
141
);
149
142
}
150
143
151
- if (isset ($ index ['settings ' ])
152
- && is_array ($ index ['settings ' ])
153
- && count ($ index ['settings ' ]) > 0 ) {
154
- $ indexInstance = $ this ->searchClient ->index ($ index ['name ' ]);
155
- foreach ($ index ['settings ' ] as $ variable => $ value ) {
156
- $ method = sprintf ('update%s ' , ucfirst ($ variable ));
157
-
158
- if (!method_exists ($ indexInstance , $ method )) {
159
- throw new InvalidSettingName (sprintf ('Invalid setting name: "%s" ' , $ variable ));
160
- }
161
-
162
- if (isset ($ value ['_service ' ]) && $ value ['_service ' ] instanceof SettingsProvider) {
163
- $ value = $ value ['_service ' ]();
164
- } elseif ('distinctAttribute ' === $ variable && is_array ($ value )) {
165
- $ value = $ value [0 ] ?? null ;
166
- }
167
-
168
- // Update
169
- $ task = $ indexInstance ->{$ method }($ value );
170
-
171
- // Get task information using uid
172
- $ indexInstance ->waitForTask ($ task ['taskUid ' ], $ responseTimeout );
173
- $ task = $ indexInstance ->getTask ($ task ['taskUid ' ]);
174
-
175
- if ('failed ' === $ task ['status ' ]) {
176
- throw new TaskException ($ task ['error ' ]);
177
- }
178
-
179
- $ output ->writeln ('<info>Settings updated of " ' .$ index ['name ' ].'".</info> ' );
180
- }
144
+ if ($ updateSettings ) {
145
+ $ this ->settingsUpdater ->update ($ index ['prefixed_name ' ], $ responseTimeout );
181
146
}
182
147
183
148
++$ page ;
@@ -220,4 +185,27 @@ private function formatIndexingResponse(array $batch, int $responseTimeout): arr
220
185
221
186
return $ formattedResponse ;
222
187
}
188
+
189
+ private function entitiesToIndex ($ indexes ): array
190
+ {
191
+ foreach ($ indexes as $ key => $ index ) {
192
+ $ entityClassName = $ index ['class ' ];
193
+
194
+ if (!is_subclass_of ($ entityClassName , Aggregator::class)) {
195
+ continue ;
196
+ }
197
+
198
+ $ indexes ->forget ($ key );
199
+
200
+ $ indexes = new Collection (array_merge (
201
+ $ indexes ->all (),
202
+ array_map (
203
+ static fn ($ entity ) => ['name ' => $ index ['name ' ], 'prefixed_name ' => $ index ['prefixed_name ' ], 'class ' => $ entity ],
204
+ $ entityClassName ::getEntities ()
205
+ )
206
+ ));
207
+ }
208
+
209
+ return array_unique ($ indexes ->all (), SORT_REGULAR );
210
+ }
223
211
}
0 commit comments