| Total Complexity | 77 |
| Total Lines | 605 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpdateReleasesIndexSchemaES often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UpdateReleasesIndexSchemaES, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class UpdateReleasesIndexSchemaES extends Command |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The name and signature of the console command. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $signature = 'nntmux:update-releases-index-es |
||
| 20 | {--add-fields : Add new media-related fields to releases index} |
||
| 21 | {--update-media-ids : Update existing indexed releases with media IDs from database} |
||
| 22 | {--batch-size=1000 : Batch size for bulk update operations} |
||
| 23 | {--movies-only : Only update releases with movie info} |
||
| 24 | {--tv-only : Only update releases with TV show info} |
||
| 25 | {--missing-only : Only update releases that have zero media IDs in index} |
||
| 26 | {--force : Force schema update even if fields exist} |
||
| 27 | {--recreate-index : Recreate the index with new schema (will delete existing data)}'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The console command description. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description = 'Update ElasticSearch releases index schema with new media fields and/or populate media IDs for existing releases'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The media fields that should exist in the releases index |
||
| 38 | */ |
||
| 39 | private array $mediaFields = [ |
||
| 40 | 'imdbid' => ['type' => 'integer'], |
||
| 41 | 'tmdbid' => ['type' => 'integer'], |
||
| 42 | 'traktid' => ['type' => 'integer'], |
||
| 43 | 'tvdb' => ['type' => 'integer'], |
||
| 44 | 'tvmaze' => ['type' => 'integer'], |
||
| 45 | 'tvrage' => ['type' => 'integer'], |
||
| 46 | 'videos_id' => ['type' => 'integer'], |
||
| 47 | 'movieinfo_id' => ['type' => 'integer'], |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Execute the console command. |
||
| 52 | */ |
||
| 53 | public function handle(): int |
||
| 54 | { |
||
| 55 | $driver = config('search.default', 'manticore'); |
||
| 56 | |||
| 57 | if ($driver !== 'elasticsearch') { |
||
| 58 | $this->warn("Current search driver is '{$driver}'. This command is for ElasticSearch."); |
||
| 59 | if (! $this->confirm('Do you want to continue anyway?', false)) { |
||
| 60 | return Command::SUCCESS; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->info('ElasticSearch releases index schema update utility'); |
||
| 65 | $this->newLine(); |
||
| 66 | |||
| 67 | // Test connection |
||
| 68 | try { |
||
| 69 | $health = Elasticsearch::cluster()->health(); |
||
| 70 | $this->info("Connected to ElasticSearch. Cluster status: {$health['status']}"); |
||
| 71 | } catch (\Exception $e) { |
||
| 72 | $this->error('Failed to connect to ElasticSearch: '.$e->getMessage()); |
||
| 73 | |||
| 74 | return Command::FAILURE; |
||
| 75 | } |
||
| 76 | |||
| 77 | $result = Command::SUCCESS; |
||
| 78 | |||
| 79 | // Handle recreate-index option |
||
| 80 | if ($this->option('recreate-index')) { |
||
| 81 | $result = $this->recreateIndexWithNewSchema(); |
||
| 82 | if ($result !== Command::SUCCESS) { |
||
| 83 | return $result; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | // Handle add-fields option |
||
| 88 | if ($this->option('add-fields')) { |
||
| 89 | $result = $this->addNewFields(); |
||
| 90 | if ($result !== Command::SUCCESS) { |
||
| 91 | return $result; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // Handle update-media-ids option |
||
| 96 | if ($this->option('update-media-ids')) { |
||
| 97 | $result = $this->updateMediaIds(); |
||
| 98 | } |
||
| 99 | |||
| 100 | // If no options specified, show current schema info |
||
| 101 | if (! $this->option('add-fields') && ! $this->option('update-media-ids') && ! $this->option('recreate-index')) { |
||
| 102 | $this->showSchemaInfo(); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $result; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Show current index schema information |
||
| 110 | */ |
||
| 111 | private function showSchemaInfo(): void |
||
| 112 | { |
||
| 113 | $this->info('Current releases index schema:'); |
||
| 114 | $this->newLine(); |
||
| 115 | |||
| 116 | try { |
||
| 117 | $indexName = config('search.drivers.elasticsearch.indexes.releases', 'releases'); |
||
| 118 | |||
| 119 | if (! Elasticsearch::indices()->exists(['index' => $indexName])) { |
||
| 120 | $this->warn("Index '{$indexName}' does not exist."); |
||
| 121 | $this->info('Run `php artisan nntmux:create-es-indexes` to create the index first.'); |
||
| 122 | |||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | $mapping = Elasticsearch::indices()->getMapping(['index' => $indexName]); |
||
| 127 | $properties = $mapping[$indexName]['mappings']['properties'] ?? []; |
||
| 128 | |||
| 129 | if (empty($properties)) { |
||
| 130 | $this->warn('Index has no mapped properties.'); |
||
| 131 | |||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | $headers = ['Field', 'Type', 'Properties']; |
||
| 136 | $rows = []; |
||
| 137 | $existingFields = []; |
||
| 138 | |||
| 139 | foreach ($properties as $field => $config) { |
||
| 140 | $type = $config['type'] ?? 'unknown'; |
||
| 141 | $props = []; |
||
| 142 | if (isset($config['analyzer'])) { |
||
| 143 | $props[] = "analyzer: {$config['analyzer']}"; |
||
| 144 | } |
||
| 145 | if (isset($config['index']) && ! $config['index']) { |
||
| 146 | $props[] = 'not indexed'; |
||
| 147 | } |
||
| 148 | if (isset($config['fields'])) { |
||
| 149 | $props[] = 'multi-field'; |
||
| 150 | } |
||
| 151 | $rows[] = [$field, $type, implode(', ', $props)]; |
||
| 152 | $existingFields[$field] = $type; |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->table($headers, $rows); |
||
| 156 | $this->newLine(); |
||
| 157 | |||
| 158 | // Check for missing media fields |
||
| 159 | $missingFields = []; |
||
| 160 | foreach ($this->mediaFields as $field => $config) { |
||
| 161 | if (! isset($existingFields[$field])) { |
||
| 162 | $missingFields[] = $field; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | if (! empty($missingFields)) { |
||
| 167 | $this->warn('Missing media fields that should be added:'); |
||
| 168 | foreach ($missingFields as $field) { |
||
| 169 | $this->line(" - {$field} ({$this->mediaFields[$field]['type']})"); |
||
| 170 | } |
||
| 171 | $this->newLine(); |
||
| 172 | $this->info('Run with --add-fields to add missing fields, or --recreate-index to rebuild with new schema.'); |
||
| 173 | } else { |
||
| 174 | $this->info('All expected media fields are present in the index.'); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Show usage hints |
||
| 178 | $this->newLine(); |
||
| 179 | $this->info('Available options:'); |
||
| 180 | $this->line(' --add-fields Add missing media-related fields to the index'); |
||
| 181 | $this->line(' --recreate-index Recreate index with new schema (data will be lost)'); |
||
| 182 | $this->line(' --update-media-ids Update indexed releases with media IDs from database'); |
||
| 183 | $this->line(' --movies-only Only update releases with movie info'); |
||
| 184 | $this->line(' --tv-only Only update releases with TV show info'); |
||
| 185 | $this->line(' --missing-only Only update releases with zero media IDs'); |
||
| 186 | $this->line(' --batch-size=N Set batch size for updates (default: 1000)'); |
||
| 187 | |||
| 188 | } catch (\Throwable $e) { |
||
| 189 | $this->error('Failed to get index mapping: '.$e->getMessage()); |
||
| 190 | $this->info('The index may not exist. Run `php artisan nntmux:create-es-indexes` first.'); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Add new fields to the releases index using mapping update |
||
| 196 | * |
||
| 197 | * Note: Elasticsearch only allows adding new fields, not modifying existing ones |
||
| 198 | */ |
||
| 199 | private function addNewFields(): int |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Recreate the index with the new schema including media fields |
||
| 270 | * WARNING: This will delete all existing data in the index |
||
| 271 | */ |
||
| 272 | private function recreateIndexWithNewSchema(): int |
||
| 273 | { |
||
| 274 | $indexName = config('search.drivers.elasticsearch.indexes.releases', 'releases'); |
||
| 275 | |||
| 276 | $this->warn("WARNING: This will DELETE all data in the '{$indexName}' index and recreate it with the new schema!"); |
||
| 277 | $this->warn('You will need to re-populate the index after this operation.'); |
||
| 278 | |||
| 279 | if (! $this->confirm('Are you sure you want to proceed?', false)) { |
||
| 280 | $this->info('Operation cancelled.'); |
||
| 281 | |||
| 282 | return Command::SUCCESS; |
||
| 283 | } |
||
| 284 | |||
| 285 | try { |
||
| 286 | // Delete existing index if it exists |
||
| 287 | if (Elasticsearch::indices()->exists(['index' => $indexName])) { |
||
| 288 | $this->info("Deleting existing '{$indexName}' index..."); |
||
| 289 | Elasticsearch::indices()->delete(['index' => $indexName]); |
||
| 290 | } |
||
| 291 | |||
| 292 | // Create new index with updated schema |
||
| 293 | $this->info("Creating '{$indexName}' index with new schema..."); |
||
| 294 | |||
| 295 | $releasesIndex = [ |
||
| 296 | 'index' => $indexName, |
||
| 297 | 'body' => [ |
||
| 298 | 'settings' => [ |
||
| 299 | 'number_of_shards' => 2, |
||
| 300 | 'number_of_replicas' => 0, |
||
| 301 | 'analysis' => [ |
||
| 302 | 'analyzer' => [ |
||
| 303 | 'release_analyzer' => [ |
||
| 304 | 'type' => 'custom', |
||
| 305 | 'tokenizer' => 'standard', |
||
| 306 | 'filter' => ['lowercase', 'asciifolding'], |
||
| 307 | ], |
||
| 308 | ], |
||
| 309 | ], |
||
| 310 | ], |
||
| 311 | 'mappings' => [ |
||
| 312 | 'properties' => [ |
||
| 313 | 'id' => [ |
||
| 314 | 'type' => 'long', |
||
| 315 | 'index' => false, |
||
| 316 | ], |
||
| 317 | 'name' => [ |
||
| 318 | 'type' => 'text', |
||
| 319 | 'analyzer' => 'release_analyzer', |
||
| 320 | ], |
||
| 321 | 'searchname' => [ |
||
| 322 | 'type' => 'text', |
||
| 323 | 'analyzer' => 'release_analyzer', |
||
| 324 | 'fields' => [ |
||
| 325 | 'keyword' => [ |
||
| 326 | 'type' => 'keyword', |
||
| 327 | 'ignore_above' => 256, |
||
| 328 | ], |
||
| 329 | 'sort' => [ |
||
| 330 | 'type' => 'keyword', |
||
| 331 | ], |
||
| 332 | ], |
||
| 333 | ], |
||
| 334 | 'plainsearchname' => [ |
||
| 335 | 'type' => 'text', |
||
| 336 | 'analyzer' => 'release_analyzer', |
||
| 337 | 'fields' => [ |
||
| 338 | 'keyword' => [ |
||
| 339 | 'type' => 'keyword', |
||
| 340 | 'ignore_above' => 256, |
||
| 341 | ], |
||
| 342 | 'sort' => [ |
||
| 343 | 'type' => 'keyword', |
||
| 344 | ], |
||
| 345 | ], |
||
| 346 | ], |
||
| 347 | 'categories_id' => [ |
||
| 348 | 'type' => 'integer', |
||
| 349 | ], |
||
| 350 | 'fromname' => [ |
||
| 351 | 'type' => 'text', |
||
| 352 | 'analyzer' => 'release_analyzer', |
||
| 353 | ], |
||
| 354 | 'filename' => [ |
||
| 355 | 'type' => 'text', |
||
| 356 | 'analyzer' => 'release_analyzer', |
||
| 357 | ], |
||
| 358 | 'add_date' => [ |
||
| 359 | 'type' => 'date', |
||
| 360 | 'format' => 'yyyy-MM-dd HH:mm:ss', |
||
| 361 | ], |
||
| 362 | 'post_date' => [ |
||
| 363 | 'type' => 'date', |
||
| 364 | 'format' => 'yyyy-MM-dd HH:mm:ss', |
||
| 365 | ], |
||
| 366 | // New media-related fields |
||
| 367 | 'imdbid' => ['type' => 'integer'], |
||
| 368 | 'tmdbid' => ['type' => 'integer'], |
||
| 369 | 'traktid' => ['type' => 'integer'], |
||
| 370 | 'tvdb' => ['type' => 'integer'], |
||
| 371 | 'tvmaze' => ['type' => 'integer'], |
||
| 372 | 'tvrage' => ['type' => 'integer'], |
||
| 373 | 'videos_id' => ['type' => 'integer'], |
||
| 374 | 'movieinfo_id' => ['type' => 'integer'], |
||
| 375 | ], |
||
| 376 | ], |
||
| 377 | ], |
||
| 378 | ]; |
||
| 379 | |||
| 380 | Elasticsearch::indices()->create($releasesIndex); |
||
| 381 | |||
| 382 | $this->info("Index '{$indexName}' created successfully with new schema!"); |
||
| 383 | $this->newLine(); |
||
| 384 | $this->warn('Remember to re-populate the index:'); |
||
| 385 | $this->line(' php artisan nntmux:populate-search-indexes --elastic --releases'); |
||
| 386 | |||
| 387 | return Command::SUCCESS; |
||
| 388 | |||
| 389 | } catch (\Throwable $e) { |
||
| 390 | $this->error('Failed to recreate index: '.$e->getMessage()); |
||
| 391 | |||
| 392 | return Command::FAILURE; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Update existing indexed releases with media IDs from database |
||
| 398 | */ |
||
| 399 | private function updateMediaIds(): int |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Prepare media data for a release |
||
| 574 | */ |
||
| 575 | private function prepareMediaData($release): array |
||
| 586 | ]; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Check if a document already has media IDs in the index |
||
| 591 | */ |
||
| 592 | private function documentHasMediaIds(string $indexName, int $id): bool |
||
| 617 | } |
||
| 618 | } |
||
| 620 |