Yii 中 solr 的使用 [ 2.0 版本 ]
安装扩展
composer require solarium/solarium
添加配置 config/params.php
/* solr 配置*/
'solr' => [
'endpoint'=>[
'localhost'=>[
'scheme' => 'http',
'host' => 'majiameng.com',
'port' => 8080,
'path' => '/solr/',
'core' => null,
'timeout' => 5,
'wt'=>'json',
]
]
],
solr - model
<?php
namespace flow\models\solr;
class IndexSolr
{
/**
* 仓库名称
* Author: TinyMeng <666@majiameng.com>
* @var string
*/
private $_dbName = 'index';
/**
* Author: TinyMeng <666@majiameng.com>
* @var \Solarium\Client|null
*/
private $client = null;
/**
* Author: TinyMeng <666@majiameng.com>
* @return $this
*/
public function init(){
$config = \yii::$app->params['solr'];
$config['endpoint']['localhost']['path'] .= $this->_dbName;
$this->client = new \Solarium\Client($config);
return $this;
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => '',
'title' => '',
'type' => '',
'inputtime' => '',
'updatetime' => '',
'status' => '',
'share_url' => '',
//视频字段
'user_id' => '',
'project_id' => '',
'slogan' =>'',
'type_id' => '',
'cate_id' => '',
'views' => '',
'describe' =>'',
'is_hot' =>'',
'type_name' =>'',
'cate_name' => '',
'cover_url' =>'',
'video_url' => '',
'play_time_format' =>'',
'file_size' =>'',
'item_logo' =>'',
//项目
'catid' =>'',
'ispay' => '',
'item_title' => '',
'item_brandword' =>'',
'brand' => '',
'brandword' => '',
'logo' => '',
'funds' =>'',
'franchisee' => '',
'3gurl' =>'',
'mobile_url' =>'',
'companyId' =>'',
'app_url' =>'',
'gbooks' => '',
'dpa_item_logo' =>'',
'dpa_img_dan1' => '',
'dpa_img_dan2' =>'',
//资讯
'img' => '',
'itemid' =>'',
'create_time' =>'',
'announcer' => '',
//问答
'tag' =>'',
'flag' => '',
'uid' =>'',
'touid' => '',
'username' =>'',
'content' =>'',
'answercount' => '',
'anonymity' => '',
'praise' =>'',
'resource' => '',
];
}
/**
* Description: 更新solr数据
* @param $data
* Author: TinyMeng <666@majiameng.com>
* @return bool
*/
public function edit($data){
//是否有本条记录
$result = $this->getOne($data);
if($result !== false){//修改本条记录
$data['_id'] = $result['_id'];
}
$update = $this->client->createUpdate();
$doc = $update->createDocument();
$attr = $this->attributeLabels();
foreach ($data as $key=>$value){
if(isset($attr[$key])){
$doc->$key=$value;
}
}
$update->addDocument($doc);
$update->addCommit();
$updateResponse=$this->client->update($update);
$res = $updateResponse->getResponse();
if($res->getStatusCode() == 200){
return true;
}
return false;
}
/**
* Description: 获取一条记录
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param $params
* @return bool
*/
public function getOne($params){
$where = [
'id'=>empty($params['id']) ? 0 : $params['id'],
'type'=>empty($params['type']) ? 0 : $params['type'],
];
$result = $this->select('',$where);
if(empty($result['response']['docs'])){
return false;
}else{
return $result['response']['docs'][0];
}
}
/**
* Description: 获取多条数据
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param string $keyword
* @param array $where
* @param int $page
* @param int $pageSize
* @param array $sort
* @return bool|mixed
*/
public function select($keyword = '', $where = [], $page=1, $pageSize=10, $sort=array()){
$query = $this->client
->createSelect()
->setStart( ($page - 1) * $pageSize)
->setRows($pageSize)
->addSorts($sort);
//设置q 查询字符串。查询所有是*:* , 根据指定字段查询
if(empty($keyword)){
$query->setQuery('*:*');
}else{
$query->setQuery('searchText:'.$keyword);
}
//fq 过滤器
foreach ($where as $key=>$value){
$query->createFilterQuery($key)->setQuery($key.":".$value);
}
$result = $this->client->execute($query);
$res = $result ->getResponse();
// var_dump($result->getQuery());
if($res->getStatusCode() == 200){
return json_decode($res->getBody(),true);
}
return false;
}
}
数据更新或添加
$solr = new IndexSolr();
$result = $solr->init()->edit($data);
数据批量查询
$solr = new IndexSolr();
$where = [
'status'=>1
];
$result = $solr->init()->select("*:*",$where);
tinymeng 北京
注册时间:2018-03-05
最后登录:2024-10-24
在线时长:36小时43分
最后登录:2024-10-24
在线时长:36小时43分
- 粉丝7
- 金钱25065
- 威望20
- 积分25625
热门源码
- 基于 Yii 2 + Bootstrap 3 搭建一套后台管理系统 CMF
- 整合完 yii2-rbac+yii2-admin+adminlte 等库的基础开发后台源码
- 适合初学者学习的一款通用的管理后台
- yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程
- yii-log-target - 监控系统异常且多渠道发送异常信息通知
- 店滴云1.3.0
- 面向对象的一小步:添加 ActiveRecord 的 Scope 功能
- Yii2 开源商城 FecShop
- 基于 Yii2 开发的多店铺商城系统,免费开源 + 适合二开
- leadshop - 基于 Yii2 开发的一款免费开源且支持商业使用的商城管理系统
共 0 条评论