PaliSearch.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $client = new \Mint\Tulip\V1\SearchClient($host, [
  10. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  11. ]);
  12. return $client;
  13. }
  14. public static function search($words,$books,$matchMode='case',$index=0,$size=10){
  15. $client = PaliSearch::connect();
  16. $request = new \Mint\Tulip\V1\SearchRequest();
  17. $request->setKeywords($words);
  18. $request->setBooks($books);
  19. $request->setMatchMode($matchMode);
  20. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  21. $page->setIndex($index);
  22. $page->setSize($size);
  23. $request->setPage($page);
  24. list($response, $status) = $client->Pali($request)->wait();
  25. if ($status->code !== \Grpc\STATUS_OK) {
  26. Log::error("ERROR: " . $status->code . ", " . $status->details);
  27. return false;
  28. }
  29. $output = [];
  30. $output['total'] = $response->getTotal();
  31. $output['page'] = $response->getPage();
  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 update($book,$paragraph,
  70. $bold1,$bold2,$bold3,
  71. $content,$pcd_book_id){
  72. $client = PaliSearch::connect();
  73. $request = new \Mint\Tulip\V1\UpdateRequest();
  74. $request->setBook($book);
  75. $request->setParagraph($paragraph);
  76. $request->setLevel(0);
  77. $request->setBold1($bold1);
  78. $request->setBold2($bold2);
  79. $request->setBold3($bold3);
  80. $request->setContent($content);
  81. $request->setPcdBookId($pcd_book_id);
  82. list($response, $status) = $client->Update($request)->wait();
  83. if ($status->code !== \Grpc\STATUS_OK) {
  84. Log::error("ERROR: " . $status->code . ", " . $status->details);
  85. return false;
  86. }
  87. return $response->getCount();
  88. }
  89. }