IndexTerm.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DhammaTerm;
  4. use Illuminate\Console\Command;
  5. use App\Services\OpenSearchService;
  6. use App\Services\TermService;
  7. use Illuminate\Support\Facades\Log;
  8. class IndexTerm extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan opensearch:index-term --word=anomadassī
  13. * @var string
  14. */
  15. protected $signature = 'opensearch:index-term
  16. {--test}
  17. {--word= : index word. omit to all}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Index Term data into OpenSearch ';
  24. private $isTest = false;
  25. private $summary = false;
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(
  32. protected OpenSearchService $openSearchService,
  33. protected TermService $termService,
  34. ) {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return int
  41. */
  42. public function handle()
  43. {
  44. $word = $this->option('word');
  45. if ($this->option('test')) {
  46. $this->isTest = true;
  47. $this->info('test mode');
  48. }
  49. try {
  50. // Test OpenSearch connection
  51. [$connected, $message] = $this->openSearchService->testConnection();
  52. if (!$connected) {
  53. $this->error($message);
  54. Log::error($message);
  55. return 1;
  56. }
  57. $overallStatus = 0; // Track overall command status (0 for success, 1 for any failure)
  58. $total = DhammaTerm::count();
  59. $terms = DhammaTerm::select(['guid', 'word'])->orderBy('updated_at', 'asc');
  60. if ($word) {
  61. $terms = $terms->where('word', $word);
  62. }
  63. foreach ($terms->cursor() as $key => $term) {
  64. $percent = (int)(($key * 100) / $total);
  65. $this->info("[{$percent}%]-{$key} " . $term->word);
  66. $this->indexTerm($term->guid);
  67. }
  68. return $overallStatus;
  69. } catch (\Exception $e) {
  70. $this->error("Failed to index Pali data: " . $e->getMessage());
  71. Log::error("Failed to index Term data : ", ['error' => $e]);
  72. return 1;
  73. }
  74. }
  75. /**
  76. *
  77. */
  78. protected function indexTerm(string $id)
  79. {
  80. $termData = $this->termService->find($id, 'text');
  81. $channelName = $termData["channel"]['name'] ?? '';
  82. $isCommunity = $this->termService->isCommunity($termData["channel_id"]);
  83. $content = $termData['html'] ?? $termData['meaning'];
  84. $document = [
  85. 'id' => "term_{$id}",
  86. 'resource_id' => $id, // Use uid from getPaliData for resource_id
  87. 'resource_type' => 'term',
  88. 'title' => [
  89. 'pali' => $termData['word'],
  90. 'zh' => $termData['meaning'],
  91. 'suggest_pali' => [$termData['word']],
  92. 'suggest_zh' => [$termData['meaning']],
  93. ],
  94. 'summary' => [
  95. 'text' => $termData['summary'] ?? $termData['note'] ?? ''
  96. ],
  97. 'content' => [],
  98. 'bold_single' => [$termData['meaning'], $termData['word']],
  99. 'related_id' => $termData['word'],
  100. 'category' => [],
  101. 'tags' => $isCommunity ? ['community'] : [],
  102. 'language' => $termData['language'],
  103. 'updated_at' => now()->toIso8601String(),
  104. 'path' => $termData['studio']['realName'] . "/{$channelName}",
  105. ];
  106. if (strpos($termData['language'], 'zh') !== false) {
  107. $document['content']['zh'] = $content;
  108. } else {
  109. //TODO 判断语言 放在合适的字段
  110. $document['content']['zh'] = $content;
  111. }
  112. if ($this->isTest) {
  113. $this->info($document['title']['pali']);
  114. $this->info($document['summary']['text']);
  115. } else {
  116. $this->openSearchService->create($document['id'], $document);
  117. }
  118. return;
  119. }
  120. }