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['page'] = $response->getPage();
  33. $output['rows'] = [];
  34. foreach ($response->getItems() as $key => $value) {
  35. $output['rows'][] = (object)[
  36. 'rank' => $value->getRank(),
  37. 'highlight' => $value->getHighlight(),
  38. 'book' => $value->getBook(),
  39. 'paragraph' => $value->getParagraph(),
  40. 'content' => $value->getContent(),
  41. ];
  42. }
  43. return $output;
  44. }
  45. public static function book_list($words,$books,$matchMode='case',$index=0,$size=10){
  46. $client = PaliSearch::connect();
  47. $request = new \Mint\Tulip\V1\SearchRequest();
  48. $request->setKeywords($words);
  49. $request->setBooks($books);
  50. $request->setMatchMode($matchMode);
  51. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  52. $page->setIndex($index);
  53. $page->setSize($size);
  54. $request->setPage($page);
  55. list($response, $status) = $client->BookList($request)->wait();
  56. if ($status->code !== \Grpc\STATUS_OK) {
  57. Log::error("ERROR: " . $status->code . ", " . $status->details);
  58. return false;
  59. }
  60. $output = [];
  61. $output['rows'] = [];
  62. foreach ($response->getItems() as $key => $value) {
  63. $output['rows'][] = (object)[
  64. 'pcd_book_id' => $value->getBook(),
  65. 'co' => $value->getCount(),
  66. ];
  67. }
  68. return $output;
  69. }
  70. public static function update($book,$paragraph,
  71. $bold1,$bold2,$bold3,
  72. $content,$pcd_book_id){
  73. $client = PaliSearch::connect();
  74. Log::debug('tulip update',['book'=>$book,'paragraph'=>$paragraph]);
  75. $request = new \Mint\Tulip\V1\UpdateRequest();
  76. $request->setBook($book);
  77. $request->setParagraph($paragraph);
  78. $request->setLevel(0);
  79. $request->setBold1($bold1);
  80. $request->setBold2($bold2);
  81. $request->setBold3($bold3);
  82. $request->setContent($content);
  83. $request->setPcdBookId($pcd_book_id);
  84. list($response, $status) = $client->Update($request)->wait();
  85. if ($status->code !== \Grpc\STATUS_OK) {
  86. Log::error("ERROR: " . $status->code . ", " . $status->details);
  87. return false;
  88. }
  89. Log::debug('tulip update success',['book'=>$book,'paragraph'=>$paragraph]);
  90. return $response->getCount();
  91. }
  92. }