PaliSearch.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 update($book,$paragraph,
  70. $bold1,$bold2,$bold3,
  71. $content,$pcd_book_id){
  72. $client = PaliSearch::connect();
  73. Log::debug('tulip update',['book'=>$book,'paragraph'=>$paragraph]);
  74. $request = new \Mint\Tulip\V1\UpdateRequest();
  75. $request->setBook($book);
  76. $request->setParagraph($paragraph);
  77. $request->setLevel(0);
  78. $request->setBold1($bold1);
  79. $request->setBold2($bold2);
  80. $request->setBold3($bold3);
  81. $request->setContent($content);
  82. $request->setPcdBookId($pcd_book_id);
  83. list($response, $status) = $client->Update($request)->wait();
  84. if ($status->code !== \Grpc\STATUS_OK) {
  85. Log::error("ERROR: " . $status->code . ", " . $status->details);
  86. return false;
  87. }
  88. Log::debug('tulip update success',['book'=>$book,'paragraph'=>$paragraph]);
  89. return $response->getCount();
  90. }
  91. }