ExportPaliSynonymsSetPath.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\OpenSearchService;
  4. use Exception;
  5. use Illuminate\Console\Attributes\Description;
  6. use Illuminate\Console\Attributes\Signature;
  7. use Illuminate\Console\Command;
  8. /**
  9. * 把索引的 pali_synonyms filter 指向新的同义词文件(两步流程的第二步)
  10. *
  11. * 第一步 export:pali.synonyms 把文件导出到 storage/app/private,
  12. * ops 脚本把文件分发到 OpenSearch 各节点后再执行本命令。
  13. * 实际路径 = OPENSEARCH_SYNONYMS_PATH + '/' + name,
  14. * 文件必须已存在于各节点,否则索引关闭后无法重新打开。
  15. *
  16. * Usage:
  17. * php artisan export:pali.synonyms.set-path --name=pali-synonyms-20261021.txt
  18. */
  19. #[Signature('export:pali.synonyms.set-path {--name= : 同义词文件名,不能带路径}')]
  20. #[Description('切换 OpenSearch 索引使用的巴利语同义词文件')]
  21. class ExportPaliSynonymsSetPath extends Command
  22. {
  23. /**
  24. * Execute the console command.
  25. *
  26. * @return int Command::SUCCESS | Command::FAILURE
  27. */
  28. public function handle(OpenSearchService $service): int
  29. {
  30. $name = (string) $this->option('name');
  31. if (preg_match(ExportPaliSynonyms::FILENAME_PATTERN, $name) !== 1) {
  32. $this->error('--name must be a plain file name matching '.ExportPaliSynonyms::FILENAME_PATTERN);
  33. return self::FAILURE;
  34. }
  35. try {
  36. $result = $service->updatePaliSynonymsPath($name);
  37. } catch (Exception $e) {
  38. $this->error('Update failed: '.$e->getMessage());
  39. return self::FAILURE;
  40. }
  41. $this->info("synonyms_path => {$result['path']}");
  42. return self::SUCCESS;
  43. }
  44. }