PaliSearch.php 3.8 KB

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