IndexTerm.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. *
  13. * @var string
  14. *
  15. * @example
  16. * php artisan opensearch:index-term
  17. * php artisan opensearch:index-term --word=anomadassī
  18. * php artisan opensearch:index-term --test
  19. */
  20. protected $signature = 'opensearch:index-term
  21. {--test}
  22. {--word= : 指定单个词条进行索引,省略则索引全部}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = 'Index Term data into OpenSearch';
  29. /** @var bool 是否为测试模式(只打印,不写入 OpenSearch) */
  30. private bool $isTest = false;
  31. /**
  32. * Create a new command instance.
  33. */
  34. public function __construct(
  35. protected OpenSearchService $openSearchService,
  36. protected TermService $termService,
  37. ) {
  38. parent::__construct();
  39. }
  40. /**
  41. * Execute the console command.
  42. *
  43. * 遍历所有(或指定)DhammaTerm,逐条构建文档并写入 OpenSearch。
  44. * 测试模式下(--test)只打印文档内容,不执行写入。
  45. *
  46. * @return int 0 表示成功,1 表示失败
  47. */
  48. public function handle(): int
  49. {
  50. $word = $this->option('word');
  51. if ($this->option('test')) {
  52. $this->isTest = true;
  53. $this->info('test mode');
  54. }
  55. try {
  56. [$connected, $message] = $this->openSearchService->testConnection();
  57. if (!$connected) {
  58. $this->error($message);
  59. Log::error($message);
  60. return 1;
  61. }
  62. $total = DhammaTerm::count();
  63. $terms = DhammaTerm::select(['guid', 'word'])->orderBy('updated_at', 'asc');
  64. if ($word) {
  65. $terms = $terms->where('word', $word);
  66. }
  67. $overallStatus = 0;
  68. foreach ($terms->cursor() as $key => $term) {
  69. $percent = (int) (($key * 100) / $total);
  70. $this->info("[{$percent}%]-{$key} " . $term->word);
  71. $this->indexTerm($term->guid);
  72. }
  73. return $overallStatus;
  74. } catch (\Exception $e) {
  75. $this->error('Failed to index Term data: ' . $e->getMessage());
  76. Log::error('Failed to index Term data', ['error' => $e]);
  77. return 1;
  78. }
  79. }
  80. /**
  81. * 构建单条词条文档并写入 OpenSearch
  82. *
  83. * 文档结构遵循新版 mapping:
  84. * title.text.pali / title.text.zh → 全文检索
  85. * title.suggest.pali / title.suggest.zh → 自动建议
  86. * content.text.pali / content.text.zh → 正文内容
  87. *
  88. * @param string $id DhammaTerm 的 guid
  89. * @return void
  90. */
  91. protected function indexTerm(string $id): void
  92. {
  93. $termData = $this->termService->find($id, 'text');
  94. $channelName = $termData['channel']['name'] ?? '';
  95. $isCommunity = $this->termService->isCommunity($termData['channel_id']);
  96. $content = $termData['html'] ?? $termData['meaning'];
  97. $document = [
  98. 'id' => "term_{$id}",
  99. 'resource_id' => $id,
  100. 'resource_type' => 'term',
  101. 'title' => [
  102. 'text' => [
  103. 'pali' => $termData['word'],
  104. 'zh' => $termData['meaning'],
  105. ],
  106. 'suggest' => [
  107. 'pali' => [$termData['word']],
  108. 'zh' => [$termData['meaning']],
  109. ],
  110. ],
  111. 'summary' => [
  112. 'text' => $termData['summary'] ?? $termData['note'] ?? '',
  113. ],
  114. 'content' => [],
  115. 'bold_single' => [$termData['meaning'], $termData['word']],
  116. 'related_id' => $termData['word'],
  117. 'category' => [],
  118. 'tags' => $isCommunity ? ['community'] : [],
  119. 'language' => $termData['language'],
  120. 'updated_at' => now()->toIso8601String(),
  121. 'path' => $termData['studio']['realName'] . "/{$channelName}",
  122. ];
  123. // TODO: 补充语言判断,将内容放入对应的 text.pali 或 text.zh 字段
  124. $plainText = strip_tags($content);
  125. if (str_contains($termData['language'], 'zh')) {
  126. $document['content']['text']['zh'] = $plainText;
  127. } else {
  128. $document['content']['text']['zh'] = $plainText;
  129. }
  130. $document['content']['display'] = $content; // 展示
  131. if ($this->isTest) {
  132. $this->info($document['title']['text']['pali']);
  133. $this->info($document['summary']['text']);
  134. } else {
  135. $this->openSearchService->create($document['id'], $document);
  136. }
  137. }
  138. }