IndexPaliText.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Services\SearchPaliDataService;
  5. use App\Services\OpenSearchService;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Models\PaliText;
  8. class IndexPaliText extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan opensearch:index-pali 93 --para=5
  13. * @var string
  14. */
  15. protected $signature = 'opensearch:index-pali {book : The book ID to index data for} {--para= : index paragraph No. omit to all} {--granularity= : The granularity to index (paragraph, sutta, sentence; omit to index all)}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Index Pali data into OpenSearch for a specified book and optional granularity (all granularities if not specified)';
  22. protected $searchPaliDataService;
  23. protected $openSearchService;
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(
  30. SearchPaliDataService $searchPaliDataService,
  31. OpenSearchService $openSearchService
  32. ) {
  33. parent::__construct();
  34. $this->searchPaliDataService = $searchPaliDataService;
  35. $this->openSearchService = $openSearchService;
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return int
  41. */
  42. public function handle()
  43. {
  44. $book = $this->argument('book');
  45. $granularity = $this->option('granularity');
  46. $paragraph = $this->option('para');
  47. try {
  48. // Test OpenSearch connection
  49. [$connected, $message] = $this->openSearchService->testConnection();
  50. if (!$connected) {
  51. $this->error($message);
  52. Log::error($message);
  53. return 1;
  54. }
  55. $overallStatus = 0; // Track overall command status (0 for success, 1 for any failure)
  56. if ((int)$book === 0) {
  57. $maxBookId = PaliText::max('book');
  58. $booksId = range(1, $maxBookId);
  59. } else {
  60. $booksId = [$book];
  61. }
  62. foreach ($booksId as $key => $bookId) {
  63. $this->indexPaliParagraphs($bookId, $paragraph);
  64. }
  65. return $overallStatus;
  66. } catch (\Exception $e) {
  67. $this->error("Failed to index Pali data: " . $e->getMessage());
  68. Log::error("Failed to index Pali data for book: $book, granularity: " . ($granularity ?: 'all'), ['error' => $e]);
  69. return 1;
  70. }
  71. }
  72. /**
  73. *
  74. */
  75. protected function indexPaliParagraph($paraInfo, $paraContent, $related_id)
  76. {
  77. $paraId = $paraInfo['book'] . '_' . $paraInfo['paragraph'];
  78. $resource_id = $paraInfo['uid'];
  79. $path = json_decode($paraInfo['path']);
  80. if (is_array($path) && count($path) > 0) {
  81. $title = end($path)->title;
  82. } else {
  83. $title = '';
  84. }
  85. $document = [
  86. 'id' => "pali_para_{$paraId}",
  87. 'resource_id' => $resource_id, // Use uid from getPaliData for resource_id
  88. 'resource_type' => 'original_text',
  89. 'title' => [
  90. 'pali' => $title,
  91. ],
  92. 'summary' => [
  93. 'text' => ''
  94. ],
  95. 'content' => [
  96. 'pali' => $paraContent['markdown'],
  97. 'suggest' => $paraContent['words'],
  98. ],
  99. 'bold_single' => implode(' ', $paraContent['bold1']),
  100. 'bold_multi' => implode(' ', array_merge($paraContent['bold2'], $paraContent['bold3'])),
  101. 'related_id' => $related_id,
  102. 'category' => 'pali', // Assuming Pali paragraphs are sutta; adjust as needed
  103. 'language' => 'pali',
  104. 'updated_at' => now()->toIso8601String(),
  105. 'granularity' => 'paragraph',
  106. 'path' => $this->getPathTitle($path),
  107. ];
  108. if ($paraInfo['level'] < 8) {
  109. $document['title']['suggest'] = $paraContent['words'];
  110. }
  111. return $this->openSearchService->create($document['id'], $document);
  112. }
  113. /**
  114. *
  115. */
  116. protected function indexPaliSession($paraInfo, $contents, $currChapter, $related_id)
  117. {
  118. $markdown = [];
  119. $text = [];
  120. $bold_single = [];
  121. $bold_multi = [];
  122. foreach ($contents as $key => $content) {
  123. $markdown[] = $content['markdown'];
  124. $text[] = $content['text'];
  125. $bold_single = array_merge($bold_single, $content['bold1']);
  126. $bold_multi = array_merge($bold_multi, $content['bold2'], $content['bold3']);
  127. }
  128. $document = [
  129. 'id' => "pali_session_{$related_id}",
  130. 'resource_id' => $paraInfo['uid'], // Use uid from getPaliData for resource_id
  131. 'resource_type' => 'original_text',
  132. 'title' => [
  133. 'pali' => "{$currChapter} paragraph {$paraInfo['paragraph']}"
  134. ],
  135. 'summary' => [
  136. 'text' => ''
  137. ],
  138. 'content' => [
  139. 'pali' => implode("\n\n", $markdown),
  140. ],
  141. 'bold_single' => implode(" ", $bold_single),
  142. 'bold_multi' => implode(" ", $bold_multi),
  143. 'related_id' => $related_id,
  144. 'category' => 'pali', // Assuming Pali paragraphs are sutta; adjust as needed
  145. 'language' => 'pali',
  146. 'updated_at' => now()->toIso8601String(),
  147. 'granularity' => 'session',
  148. 'path' => $this->getPathTitle(json_decode($paraInfo['path'])),
  149. ];
  150. return $this->openSearchService->create($document['id'], $document);
  151. }
  152. private function getPathTitle(array $input)
  153. {
  154. $output = [];
  155. foreach ($input as $key => $node) {
  156. $output[] = $node->title;
  157. }
  158. return implode('/', $output);
  159. }
  160. /**
  161. * Index Pali paragraphs for a given book.
  162. *
  163. * @param int $book
  164. * @return int
  165. */
  166. protected function indexPaliParagraphs($book, $paragraph)
  167. {
  168. $this->info("Starting to index paragraphs for book: $book");
  169. $total = 0;
  170. if ($paragraph) {
  171. $paragraphs = PaliText::where('book', $book)
  172. ->where('paragraph', $paragraph)
  173. ->orderBy('paragraph')->cursor();
  174. } else {
  175. $paragraphs = PaliText::where('book', $book)
  176. ->orderBy('paragraph')->cursor();
  177. }
  178. $headings = [];
  179. $currChapterTitle = '';
  180. $commentaryId = '';
  181. $currSession = [];
  182. foreach ($paragraphs as $key => $para) {
  183. $total++;
  184. if ($para->level < 8) {
  185. $currChapterTitle = $para->toc;
  186. }
  187. if ($para->class === 'nikaya') {
  188. $nikaya = $para->text;
  189. }
  190. $paraContent = $this->searchPaliDataService
  191. ->getParaContent($para['book'], $para['paragraph']);
  192. if (!empty($commentaryId)) {
  193. $currSession[] = $paraContent;
  194. }
  195. if (isset($paraContent['commentary'])) {
  196. if (!empty($commentaryId)) {
  197. //保存 session
  198. $this->indexPaliSession($para->toArray(), $currSession, $currChapterTitle, $commentaryId);
  199. $currSession = [];
  200. }
  201. $commentaryId = $paraContent['commentary'];
  202. }
  203. $this->indexPaliParagraph($para->toArray(), $paraContent, $commentaryId);
  204. $this->info("{$para['book']}-[{$para['paragraph']}]-[{$commentaryId}]");
  205. //usleep(200 * 1000);
  206. }
  207. $this->info("Successfully indexed $total paragraphs for book: $book");
  208. Log::info("Indexed $total paragraphs for book: $book");
  209. return 0;
  210. }
  211. /**
  212. * Index Pali suttas for a given book (placeholder for future implementation).
  213. *
  214. * @param int $book
  215. * @return int
  216. */
  217. protected function indexPaliSutta($book)
  218. {
  219. $this->warn("Sutta indexing is not yet implemented for book: $book");
  220. Log::warning("Sutta indexing not implemented for book: $book");
  221. return 1;
  222. }
  223. /**
  224. * Index Pali sentences for a given book (placeholder for future implementation).
  225. *
  226. * @param int $book
  227. * @return int
  228. */
  229. protected function indexPaliSentences($book)
  230. {
  231. $this->warn("Sentence indexing is not yet implemented for book: $book");
  232. Log::warning("Sentence indexing not implemented for book: $book");
  233. return 1;
  234. }
  235. }