UpdateOpenSearchIndex.php 1.9 KB

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