PaliSearch.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Tools;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\Log;
  5. class PaliSearch
  6. {
  7. public static function connect(){
  8. $host = config('mint.server.rpc.tulip.host') . ':' . config('mint.server.rpc.tulip.port');
  9. Log::debug('tulip host='.$host);
  10. $client = new \Mint\Tulip\V1\SearchClient($host, [
  11. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  12. ]);
  13. return $client;
  14. }
  15. public static function search($words,$books,$matchMode='case',$index=0,$size=10){
  16. $client = PaliSearch::connect();
  17. $request = new \Mint\Tulip\V1\SearchRequest();
  18. $request->setKeywords($words);
  19. $request->setBooks($books);
  20. $request->setMatchMode($matchMode);
  21. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  22. $page->setIndex($index);
  23. $page->setSize($size);
  24. $request->setPage($page);
  25. list($response, $status) = $client->Pali($request)->wait();
  26. if ($status->code !== \Grpc\STATUS_OK) {
  27. Log::error("ERROR: " . $status->code . ", " . $status->details);
  28. return false;
  29. }
  30. $output = [];
  31. $output['total'] = $response->getTotal();
  32. $output['rows'] = [];
  33. foreach ($response->getItems() as $key => $value) {
  34. $output['rows'][] = (object)[
  35. 'rank' => $value->getRank(),
  36. 'highlight' => $value->getHighlight(),
  37. 'book' => $value->getBook(),
  38. 'paragraph' => $value->getParagraph(),
  39. 'content' => $value->getContent(),
  40. ];
  41. }
  42. return $output;
  43. }
  44. public static function book_list($words,$books,$matchMode='case',$index=0,$size=10){
  45. $client = PaliSearch::connect();
  46. $request = new \Mint\Tulip\V1\SearchRequest();
  47. $request->setKeywords($words);
  48. $request->setBooks($books);
  49. $request->setMatchMode($matchMode);
  50. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  51. $page->setIndex($index);
  52. $page->setSize($size);
  53. $request->setPage($page);
  54. list($response, $status) = $client->BookList($request)->wait();
  55. if ($status->code !== \Grpc\STATUS_OK) {
  56. Log::error("ERROR: " . $status->code . ", " . $status->details);
  57. return false;
  58. }
  59. $output = [];
  60. $output['rows'] = [];
  61. foreach ($response->getItems() as $key => $value) {
  62. $output['rows'][] = (object)[
  63. 'pcd_book_id' => $value->getBook(),
  64. 'co' => $value->getCount(),
  65. ];
  66. }
  67. return $output;
  68. }
  69. public static function upload_dict($data){
  70. $client = PaliSearch::connect();
  71. $request = new \Mint\Tulip\V1\UploadDictionaryRequest();
  72. $request->setData($data);
  73. list($response, $status) = $client->UploadDictionary($request)->wait();
  74. if ($status->code !== \Grpc\STATUS_OK) {
  75. Log::error("ERROR: " . $status->code . ", " . $status->details);
  76. return false;
  77. }
  78. return $response->getError();
  79. }
  80. public static function update($book,$paragraph,
  81. $bold1,$bold2,$bold3,
  82. $content,$pcd_book_id){
  83. $client = PaliSearch::connect();
  84. $request = new \Mint\Tulip\V1\UpdateRequest();
  85. $request->setBook($book);
  86. $request->setParagraph($paragraph);
  87. $request->setLevel(0);
  88. $request->setBold1($bold1);
  89. $request->setBold2($bold2);
  90. $request->setBold3($bold3);
  91. $request->setContent($content);
  92. $request->setPcdBookId($pcd_book_id);
  93. list($response, $status) = $client->Update($request)->wait();
  94. if ($status->code !== \Grpc\STATUS_OK) {
  95. Log::error("ERROR: " . $status->code . ", " . $status->details);
  96. return false;
  97. }
  98. return $response->getCount();
  99. }
  100. }