windows安装solr扩展
首先到pecl 搜索solr
http://pecl.php.net/package/solr
下载 2.4 dll版本
| Dependencies |
| Release 2.4.0: |
PHP Version: PHP 5.3 or newer
PEAR Package: PEAR 1.4.0 or newer
PHP Extension: libxml |
| Release 2.3.0: |
PHP Version: PHP version 5.99.99 or older
PHP Version: PHP 5.3 or newer
PEAR Package: PEAR 1.4.0 or newer
PHP Extension: libxml |
| Release 2.2.1: |
PHP Version: PHP version 5.99.99 or older
PHP Version: PHP 5.3 or newer
PEAR Package: PEAR 1.4.0 or newer
PHP Extension: libxml |
| Dependencies for older releases can be found on the release overview page. |
|
根据自己的PHP版本选择正确的文件下载,我这里使用的是5.5 TS×86
下载完以后将DLL 文件放在php5.5/ext文件夹
在php.ini中增加
extension=php_solr.dll
重启php,查看phpinfo 安装扩展成功
下载PHP-SOLR-CLIENT
可以使用solr已封装好的例子来查询
1、首先将要用到的solr-php-client下载下来,网站需要vpn才能访问
将下载下来的中的Aparche目录复制到项目目录
直接require就行 ,下面提供一个例子是使用php内置类连接solr查询数据代码
<?php
class SolrSearch
{
private $client = null;
private $option = array('hostname' => '192.168.1.100', 'port' => '8080', 'login' => 'admin', 'password' => 'admin', 'path' => 'solr/test', 'wt' => 'json');
private $timeout = 500;
function __construct($option = array())
{
if (false === empty($option)) $this->option = $option;
$this->client = new SolrClient($this->option);
var_dump($this->client);
}
public function tlist($params)
{
$conditions = (isset($params['conditions']) && !empty($params['conditions'])) ? trim($params['conditions']) : 'id:*';
$start = (isset($params['start']) && !empty($params['start'])) ? intval($params['start']) : 0;
$perPageCount = (isset($params['pagesize']) && !empty($params['pagesize'])) ? intval($params['pagesize']) : 30;
$fieldstr = (isset($params['fieldstr']) && !empty($params['fieldstr'])) ? $params['fieldstr'] : 'id';
$sort = (isset($params['sort']) && !empty($params['sort'])) ? $params['sort'] : '';
$wt = (isset($params['wt'])) ? $params['wt'] : '';
$ishighlight = (isset($params['ishighlight'])) ? $params['ishighlight'] : 0;
$highlightfield = (isset($params['highlightfield'])) ? trim($params['highlightfield']) : '';
$info = array('qtime' => 0, 'numFound' => 0, 'list' => array());
//查询
try {
$query = new SolrQuery();
$query->setQuery($conditions)->setStart($start)->setRows($perPageCount)->setTimeAllowed($this->timeout);
$query->addField($fieldstr); //显示的字段
if (!empty($sort)) $query->setParam('sort', $sort);
if ($ishighlight) {
//设置高亮
$query->setHighlight(true);
$query->setHighlightSimplePre('<span style="color:red">');
$query->setHighlightSimplePost('</span>');
//高亮字段
$highlightfield_arr = explode(',', $highlightfield);
if (count($highlightfield_arr) > 0) {
foreach ($highlightfield_arr as $vv) {
$query->addHighlightField($vv);
}
} else {
$info = array('qtime' => 0, 'numFound' => 0, 'list' => array(), 'msg' => '开启高亮显示,确没有合法的高亮字段');
return $info;
}
$query->setHighlightUsePhraseHighlighter(true);
$query_response = $this->client->query($query);
} else {
$query_response = $this->client->query($query);
}
$query_response = $this->client->query($query);
if ($query_response->getHttpStatus() == 200 && $query_response->success() == 1) {
$response = json_decode(json_encode($query_response->getResponse()), true);
//高亮处理
if ($ishighlight && count($highlightfield_arr) > 0) {
if (is_array($highlightfield_arr) && count($highlightfield_arr) > 0) {
foreach ($response['response']['docs'] as &$val) {
foreach ($highlightfield_arr as $vv) {
if (isset($response['highlighting'][$val['id']][$vv][0])) {
$val[$vv] = $response['highlighting'][$val['id']][$vv][0];
}
}
}
} else {
$info = array('qtime' => 0, 'numFound' => 0, 'list' => array(), 'msg' => '开启高亮显示,确没有合法的高亮字段');
return $info;
}
}
$info = array('qtime' => $response['responseHeader']['QTime'], 'numFound' => $response['response']['numFound'], 'list' => $response['response']['docs']);
} else {
$info = array('qtime' => 0, 'numFound' => 0, 'list' => array());
}
} catch (Exception $e) {
$info = array('qtime' => 0, 'numFound' => 0, 'list' => array());
}
return $info;
}
public function addDocument($data)
{
}
}
$obj = new SolrSearch();
$param = array();
$param['conditions'] = "test_title:美人纯棉";
$param['start'] = 0;
$param['fieldstr'] = 'id,content,author,description';
//$param['sort'] = 'id desc';
$param['pagesize'] = 10;
$param['ishighlight'] = true;
$param['highlightfield'] = 'id,test_title';
$res = $obj->tlist($param);
echo '<pre>';
print_r($res);
echo '</pre>';
评论