UpdateOpenSearchIndex.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\OpenSearchService;
  4. use Illuminate\Console\Command;
  5. class UpdateOpenSearchIndex extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. * php artisan create:opensearch.index
  10. * @var string
  11. */
  12. protected $signature = 'update:opensearch.index';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Command description';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $openSearch = app(OpenSearchService::class);
  36. // Test OpenSearch connection
  37. $open = $openSearch->testConnection();
  38. if ($open[0]) {
  39. $this->info($open[1]);
  40. } else {
  41. $this->error($open[1]);
  42. return 1; // Exit with error code
  43. }
  44. // Attempt to create or update index
  45. $this->warn('Index already exists, attempting to update...');
  46. try {
  47. $update = $openSearch->updateIndex();
  48. if (!empty($update['settings']) && $update['settings']['acknowledged']) {
  49. $this->info('Index settings updated successfully');
  50. }
  51. if (!empty($update['mappings']) && $update['mappings']['acknowledged']) {
  52. $this->info('Index mappings updated successfully');
  53. }
  54. if (empty($update['settings']) && empty($update['mappings'])) {
  55. $this->warn('No settings or mappings provided for update');
  56. }
  57. } catch (\Exception $updateException) {
  58. $this->error('Failed to update index: ' . $updateException->getMessage());
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. }