博文 > php中简单使用solr

php中简单使用solr

Search 10002016-01-01 09:29:46

安装扩展

composer require solarium/solarium

添加 config/params.php

  1. /* solr 配置*/
  2. 'solr' => [
  3. 'endpoint'=>[
  4. 'localhost'=>[
  5. 'scheme' => 'http',
  6. 'host' => 'majiameng.com',
  7. 'port' => 8080,
  8. 'path' => '/solr/',
  9. 'core' => null,
  10. 'timeout' => 5,
  11. 'wt'=>'json',
  12. ]
  13. ]
  14. ],
solr - model
  1. <?php
  2. namespace flow\models\solr;
  3. class IndexSolr
  4. {
  5. /**
  6. * 仓库名称
  7. * Author: TinyMeng <666@majiameng.com>
  8. * @var string
  9. */
  10. private $_dbName = 'index';
  11. /**
  12. * Author: TinyMeng <666@majiameng.com>
  13. * @var \Solarium\Client|null
  14. */
  15. private $client = null;
  16. /**
  17. * Author: TinyMeng <666@majiameng.com>
  18. * @return $this
  19. */
  20. public function init(){
  21. $config = \yii::$app->params['solr'];
  22. $config['endpoint']['localhost']['path'] .= $this->_dbName;
  23. $this->client = new \Solarium\Client($config);
  24. return $this;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function attributeLabels()
  30. {
  31. return [
  32. 'id' => '',
  33. 'title' => '',
  34. 'type' => '',
  35. 'inputtime' => '',
  36. 'updatetime' => '',
  37. 'status' => '',
  38. 'share_url' => '',
  39. //视频字段
  40. 'user_id' => '',
  41. 'project_id' => '',
  42. 'slogan' =>'',
  43. 'type_id' => '',
  44. 'cate_id' => '',
  45. 'views' => '',
  46. 'describe' =>'',
  47. 'is_hot' =>'',
  48. 'type_name' =>'',
  49. 'cate_name' => '',
  50. 'cover_url' =>'',
  51. 'video_url' => '',
  52. 'play_time_format' =>'',
  53. 'file_size' =>'',
  54. 'item_logo' =>'',
  55. //项目
  56. 'catid' =>'',
  57. 'ispay' => '',
  58. 'item_title' => '',
  59. 'item_brandword' =>'',
  60. 'brand' => '',
  61. 'brandword' => '',
  62. 'logo' => '',
  63. 'funds' =>'',
  64. 'franchisee' => '',
  65. '3gurl' =>'',
  66. 'mobile_url' =>'',
  67. 'companyId' =>'',
  68. 'app_url' =>'',
  69. 'gbooks' => '',
  70. 'dpa_item_logo' =>'',
  71. 'dpa_img_dan1' => '',
  72. 'dpa_img_dan2' =>'',
  73. //资讯
  74. 'img' => '',
  75. 'itemid' =>'',
  76. 'create_time' =>'',
  77. 'announcer' => '',
  78. //问答
  79. 'tag' =>'',
  80. 'flag' => '',
  81. 'uid' =>'',
  82. 'touid' => '',
  83. 'username' =>'',
  84. 'content' =>'',
  85. 'answercount' => '',
  86. 'anonymity' => '',
  87. 'praise' =>'',
  88. 'resource' => '',
  89. ];
  90. }
  91. /**
  92. * Description: 更新solr数据
  93. * @param $data
  94. * Author: TinyMeng <666@majiameng.com>
  95. * @return bool
  96. */
  97. public function edit($data){
  98. //是否有本条记录
  99. $result = $this->getOne($data);
  100. if($result !== false){//修改本条记录
  101. $data['_id'] = $result['_id'];
  102. }
  103. $update = $this->client->createUpdate();
  104. $doc = $update->createDocument();
  105. $attr = $this->attributeLabels();
  106. foreach ($data as $key=>$value){
  107. if(isset($attr[$key])){
  108. $doc->$key=$value;
  109. }
  110. }
  111. $update->addDocument($doc);
  112. $update->addCommit();
  113. $updateResponse=$this->client->update($update);
  114. $res = $updateResponse->getResponse();
  115. if($res->getStatusCode() == 200){
  116. return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Description: 获取一条记录
  122. * Author: JiaMeng <666@majiameng.com>
  123. * Updater:
  124. * @param $params
  125. * @return bool
  126. */
  127. public function getOne($params){
  128. $where = [
  129. 'id'=>empty($params['id']) ? 0 : $params['id'],
  130. 'type'=>empty($params['type']) ? 0 : $params['type'],
  131. ];
  132. $result = $this->select('',$where);
  133. if(empty($result['response']['docs'])){
  134. return false;
  135. }else{
  136. return $result['response']['docs'][0];
  137. }
  138. }
  139. /**
  140. * Description: 获取多条数据
  141. * Author: JiaMeng <666@majiameng.com>
  142. * Updater:
  143. * @param string $keyword
  144. * @param array $where
  145. * @param int $page
  146. * @param int $pageSize
  147. * @param array $sort
  148. * @return bool|mixed
  149. */
  150. public function select($keyword = '', $where = [], $page=1, $pageSize=10, $sort=array()){
  151. $query = $this->client
  152. ->createSelect()
  153. ->setStart( ($page - 1) * $pageSize)
  154. ->setRows($pageSize)
  155. ->addSorts($sort);
  156. //设置q 查询字符串。查询所有是*:* , 根据指定字段查询
  157. if(empty($keyword)){
  158. $query->setQuery('*:*');
  159. }else{
  160. $query->setQuery('searchText:'.$keyword);
  161. }
  162. //fq 过滤器
  163. foreach ($where as $key=>$value){
  164. $query->createFilterQuery($key)->setQuery($key.":".$value);
  165. }
  166. $result = $this->client->execute($query);
  167. $res = $result ->getResponse();
  168. // var_dump($result->getQuery());
  169. if($res->getStatusCode() == 200){
  170. return json_decode($res->getBody(),true);
  171. }
  172. return false;
  173. }
  174. }

数据更新或添加

  1. $solr = new IndexSolr();
  2. $result = $solr->init()->edit($data);

数据批量查询

  1. $solr = new IndexSolr();
  2. $where = [
  3. 'status'=>1
  4. ];
  5. $result = $solr->init()->select("*:*",$where);