PaliQueryHealthCheck.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * Check whether the pali_synonyms dictionary is applied for a given word
  10. *
  11. * Usage:
  12. * php artisan app:pali-query-health-check # defaults to "dhamma"
  13. * php artisan app:pali-query-health-check dhamma # custom word
  14. */
  15. #[Signature('app:pali-query-health-check {word? : Pali word to check, defaults to dhamma}')]
  16. #[Description('Check whether the pali_synonyms dictionary is applied for a given word via the OpenSearch _analyze API')]
  17. class PaliQueryHealthCheck extends Command
  18. {
  19. /**
  20. * Execute the console command.
  21. *
  22. * @param OpenSearchService $service
  23. * @return int Command::SUCCESS | Command::FAILURE
  24. */
  25. public function handle(OpenSearchService $service): int
  26. {
  27. $word = $this->argument('word') ?? 'dhamma';
  28. $this->info("Checking synonym expansion for [{$word}]...");
  29. try {
  30. $tokens = $service->pali_query_health_check($word);
  31. } catch (Exception $e) {
  32. $this->error('Check failed: '.$e->getMessage());
  33. return self::FAILURE;
  34. }
  35. if (empty($tokens)) {
  36. $this->warn('No tokens returned. Please check that the index exists and the analyzer is configured correctly.');
  37. return self::FAILURE;
  38. }
  39. $this->table(
  40. ['#', 'Token'],
  41. collect($tokens)->values()->map(fn ($token, $i) => [$i + 1, $token])->toArray()
  42. );
  43. $expanded = count($tokens) > 1;
  44. if ($expanded) {
  45. $this->info("✅ Synonym dictionary is active — [{$word}] expanded into ".count($tokens).' token(s).');
  46. } else {
  47. $this->warn("⚠️ [{$word}] did not expand into any synonyms. Check that the entry exists in pali_synonyms.txt, or that the index has reloaded the file.");
  48. }
  49. return self::SUCCESS;
  50. }
  51. }