SearchService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. require_once(APP_PATH.'/protected/vendors/osearch/CloudsearchClient.php');
  3. require_once(APP_PATH.'/protected/vendors/osearch/CloudsearchIndex.php');
  4. require_once(APP_PATH.'/protected/vendors/osearch/CloudsearchDoc.php');
  5. require_once(APP_PATH.'/protected/vendors/osearch/CloudsearchSearch.php');
  6. class SearchService extends Service{
  7. public function __construct(){
  8. $access_key = "CH79ggzPzBJsAbMN";
  9. $secret = "DKu53eAKAhkF10PjZMFcyANNC3Gsqw";
  10. $host = "http://opensearch-cn-beijing.aliyuncs.com";
  11. $key_type = "aliyun";
  12. $opts = array('host'=>$host);
  13. $this->client = new CloudsearchClient($access_key,$secret,$opts,$key_type);
  14. }
  15. public function addTopic($topics){
  16. $doc_obj = new CloudsearchDoc('topic',$this->client);
  17. $json = json_encode($topics);
  18. $res = $doc_obj->add($json,"main");
  19. return $res;
  20. }
  21. public function addMessage($message){
  22. $doc_obj = new CloudsearchDoc('message',$this->client);
  23. $json = json_encode($message);
  24. $res = $doc_obj->add($json,"main");
  25. return $res;
  26. }
  27. public function addPost($post){
  28. $doc_obj = new CloudsearchDoc('post',$this->client);
  29. $json = json_encode($post);
  30. $res = $doc_obj->add($json,"main");
  31. return $res;
  32. }
  33. public function addUser($users){
  34. $doc_obj = new CloudsearchDoc('user',$this->client);
  35. $json = json_encode($users);
  36. $res = $doc_obj->add($json,"main");
  37. return $res;
  38. }
  39. public function searchTopic($keywords,$page,$pagesize,$status='all'){
  40. $start = 0;
  41. if($page>=1){
  42. $start = ($page-1)*$pagesize;
  43. }
  44. $search_obj = new CloudsearchSearch($this->client);
  45. $search_obj->addIndex('topic');
  46. $search_obj->setQueryString("default:$keywords");
  47. if($status!=='all'){
  48. $search_obj->addFilter("status=$status");
  49. }
  50. $search_obj->setFormat("json");
  51. $json = $search_obj->search(array('start'=>$start,'hits'=>$pagesize));
  52. $result = json_decode($json,true);
  53. return $result;
  54. }
  55. public function searchMessage($keywords,$page,$pagesize,$status='all'){
  56. $start = 0;
  57. if($page>=1){
  58. $start = ($page-1)*$pagesize;
  59. }
  60. $search_obj = new CloudsearchSearch($this->client);
  61. $search_obj->addIndex('message');
  62. $search_obj->setQueryString("default:$keywords");
  63. if($status!=='all'){
  64. $search_obj->addFilter("status=$status");
  65. }
  66. $search_obj->setFormat("json");
  67. $json = $search_obj->search(array('start'=>$start,'hits'=>$pagesize));
  68. $result = json_decode($json,true);
  69. return $result;
  70. }
  71. public function searchPost($keywords,$page,$pagesize,$status='all'){
  72. $start = 0;
  73. if($page>=1){
  74. $start = ($page-1)*$pagesize;
  75. }
  76. $search_obj = new CloudsearchSearch($this->client);
  77. $search_obj->addIndex('post');
  78. if($status!=='all'){
  79. $search_obj->addFilter("status=$status");
  80. }
  81. $search_obj->setQueryString("default:$keywords");
  82. $search_obj->setFormat("json");
  83. $json = $search_obj->search(array('start'=>$start,'hits'=>$pagesize));
  84. $result = json_decode($json,true);
  85. return $result;
  86. }
  87. public function searchUser($keywords,$page,$pagesize){
  88. $start = 0;
  89. if($page>=1){
  90. $start = ($page-1)*$pagesize;
  91. }
  92. $search_obj = new CloudsearchSearch($this->client);
  93. $search_obj->addIndex('user');
  94. $search_obj->setQueryString("default:$keywords");
  95. $search_obj->setFormat("json");
  96. $json = $search_obj->search(array('start'=>$start,'hits'=>$pagesize));
  97. $result = json_decode($json,true);
  98. return $result;
  99. }
  100. }
  101. ?>