PaliSearch.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 search($words,$books,$matchMode='case',$index=0,$size=10){
  8. $host = config('mint.server.rpc.tulip');
  9. Log::debug('tulip host='.$host);
  10. $client = new \Mint\Tulip\V1\SearchClient($host, [
  11. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  12. ]);
  13. $request = new \Mint\Tulip\V1\SearchRequest();
  14. $request->setKeywords($words);
  15. $request->setBooks($books);
  16. $request->setMatchMode($matchMode);
  17. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  18. $page->setIndex($index);
  19. $page->setSize($size);
  20. $request->setPage($page);
  21. list($response, $status) = $client->Pali($request)->wait();
  22. if ($status->code !== \Grpc\STATUS_OK) {
  23. Log::error("ERROR: " . $status->code . ", " . $status->details);
  24. return false;
  25. }
  26. $output = [];
  27. $output['total'] = $response->getTotal();
  28. $output['rows'] = [];
  29. foreach ($response->getItems() as $key => $value) {
  30. $output['rows'][] = [
  31. 'rank' => $value->getRank(),
  32. 'highlight' => $value->getHighlight(),
  33. 'book' => $value->getBook(),
  34. 'paragraph' => $value->getParagraph(),
  35. 'content' => $value->getContent(),
  36. ];
  37. }
  38. return $output;
  39. }
  40. public static function book_list($words,$books,$matchMode='case',$index=0,$size=10){
  41. $host = config('mint.server.rpc.tulip');
  42. Log::debug('tulip host='.$host);
  43. $client = new \Mint\Tulip\V1\SearchClient($host, [
  44. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  45. ]);
  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'][] = [
  63. 'pcd_book_id' => $value->getBook(),
  64. 'co' => $value->getCount(),
  65. ];
  66. }
  67. return $output;
  68. }
  69. }