IndexPaliText.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. class IndexPaliText extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. * php artisan opensearch:index-pali 93
  12. * @var string
  13. */
  14. protected $signature = 'opensearch:index-pali {book : The book ID to index data for} {--granularity= : The granularity to index (paragraph, sutta, sentence; omit to index all)}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Index Pali data into OpenSearch for a specified book and optional granularity (all granularities if not specified)';
  21. protected $searchPaliDataService;
  22. protected $openSearchService;
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(SearchPaliDataService $searchPaliDataService, OpenSearchService $openSearchService)
  29. {
  30. parent::__construct();
  31. $this->searchPaliDataService = $searchPaliDataService;
  32. $this->openSearchService = $openSearchService;
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $book = $this->argument('book');
  42. $granularity = $this->option('granularity');
  43. try {
  44. // Test OpenSearch connection
  45. [$connected, $message] = $this->openSearchService->testConnection();
  46. if (!$connected) {
  47. $this->error($message);
  48. Log::error($message);
  49. return 1;
  50. }
  51. // Define all possible granularities
  52. $granularities = ['paragraph', 'sutta', 'sentence'];
  53. // If granularity is not set, index all granularities; otherwise, index the specified one
  54. $targetGranularities = empty($granularity) ? $granularities : [$granularity];
  55. $overallStatus = 0; // Track overall command status (0 for success, 1 for any failure)
  56. foreach ($targetGranularities as $gran) {
  57. // Validate granularity
  58. if (!in_array($gran, $granularities)) {
  59. $this->error("Invalid granularity: $gran. Supported values: " . implode(', ', $granularities));
  60. Log::error("Invalid granularity provided: $gran");
  61. $overallStatus = 1;
  62. continue;
  63. }
  64. // Route to appropriate indexing method
  65. switch ($gran) {
  66. case 'paragraph':
  67. $status = $this->indexPaliParagraphs($book);
  68. break;
  69. case 'sutta':
  70. $status = $this->indexPaliSutta($book);
  71. break;
  72. case 'sentence':
  73. $status = $this->indexPaliSentences($book);
  74. break;
  75. default:
  76. $status = 1; // Should not reach here due to validation
  77. }
  78. // Update overall status if any indexing fails
  79. $overallStatus = max($overallStatus, $status);
  80. }
  81. if ($overallStatus === 0) {
  82. $this->info("Successfully completed indexing for book: $book");
  83. } else {
  84. $this->warn("Indexing completed with errors for book: $book");
  85. }
  86. return $overallStatus;
  87. } catch (\Exception $e) {
  88. $this->error("Failed to index Pali data: " . $e->getMessage());
  89. Log::error("Failed to index Pali data for book: $book, granularity: " . ($granularity ?: 'all'), ['error' => $e->getMessage()]);
  90. return 1;
  91. }
  92. }
  93. /**
  94. * Index Pali paragraphs for a given book.
  95. *
  96. * @param int $book
  97. * @return int
  98. */
  99. protected function indexPaliParagraphs($book)
  100. {
  101. $this->info("Starting to index paragraphs for book: $book");
  102. // Fetch all paragraphs for the book
  103. $result = $this->searchPaliDataService->getPaliData($book, 1, null);
  104. $paragraphs = $result['rows'];
  105. $total = count($paragraphs);
  106. if ($total === 0) {
  107. $this->warn("No paragraphs found for book: $book");
  108. return 0;
  109. }
  110. $this->info("Found $total paragraphs to index");
  111. // Create progress bar
  112. $bar = $this->output->createProgressBar($total);
  113. $bar->start();
  114. foreach ($paragraphs as $paragraph) {
  115. // Map paragraph data to OpenSearch document structure
  116. $document = [
  117. 'id' => "pali_para_{$book}_{$paragraph['paragraph']}",
  118. 'resource_id' => $paragraph['uid'], // Use uid from getPaliData for resource_id
  119. 'resource_type' => 'paragraph',
  120. 'title' => [
  121. 'display' => "Paragraph {$paragraph['paragraph']} of Book {$book}"
  122. ],
  123. 'summary' => [
  124. 'text' => $paragraph['text']
  125. ],
  126. 'content' => [
  127. 'display' => $paragraph['markdown'],
  128. 'text' => $paragraph['text'], // Remove markdown for plain text
  129. 'exact' => $paragraph['text'],
  130. ],
  131. 'bold_single' => $paragraph['bold1'],
  132. 'bold_multi' => $paragraph['bold2'] . ' ' . $paragraph['bold3'],
  133. 'related_id' => $paragraph['pcd_book_id'],
  134. 'category' => 'pali', // Assuming Pali paragraphs are sutta; adjust as needed
  135. 'language' => 'pali',
  136. 'updated_at' => now()->toIso8601String(),
  137. 'granularity' => 'paragraph',
  138. ];
  139. // Index the document in OpenSearch
  140. $this->openSearchService->create($document['id'], $document);
  141. $bar->advance();
  142. }
  143. $bar->finish();
  144. $this->newLine();
  145. $this->info("Successfully indexed $total paragraphs for book: $book");
  146. Log::info("Indexed $total paragraphs for book: $book");
  147. return 0;
  148. }
  149. /**
  150. * Index Pali suttas for a given book (placeholder for future implementation).
  151. *
  152. * @param int $book
  153. * @return int
  154. */
  155. protected function indexPaliSutta($book)
  156. {
  157. $this->warn("Sutta indexing is not yet implemented for book: $book");
  158. Log::warning("Sutta indexing not implemented for book: $book");
  159. return 1;
  160. }
  161. /**
  162. * Index Pali sentences for a given book (placeholder for future implementation).
  163. *
  164. * @param int $book
  165. * @return int
  166. */
  167. protected function indexPaliSentences($book)
  168. {
  169. $this->warn("Sentence indexing is not yet implemented for book: $book");
  170. Log::warning("Sentence indexing not implemented for book: $book");
  171. return 1;
  172. }
  173. }