当前位置: 首页 > 工具软件 > php_solr > 使用案例 >

php 使用solr,php中简单使用solr

蒋高扬
2023-12-01

安装扩展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);

 类似资料: