海涛博客

php安装solr扩展并使用SolrQuery类查询数据

记录安装 PHP Solr 扩展并使用 SolrQuery 类查询数据的过程,包含环境配置、连接参数与查询示例。

评论
windows安装solr扩展 首先到pecl 搜索solr http://pecl.php.net/package/solr 下载 2.4 dll版本
Available Releases
Version State Release Date Downloads
2.4.0 stable 2016-03-30 solr-2.4.0.tgz (250.3kB)  DLL [Changelog]
2.3.0 stable 2015-12-01 solr-2.3.0.tgz (148.7kB)  DLL [Changelog]
2.2.1 stable 2015-09-27 solr-2.2.1.tgz (139.0kB)  DLL [Changelog]
2.2.0 stable 2015-09-26 solr-2.2.0.tgz (139.0kB) [Changelog]
2.1.0 stable 2015-01-17 solr-2.1.0.tgz (119.2kB)  DLL [Changelog]
2.0.0 stable 2014-06-24 solr-2.0.0.tgz (111.0kB)  DLL [Changelog]
1.1.1 stable 2014-06-20 solr-1.1.1.tgz (108.6kB)  DLL [Changelog]
1.1.0 stable 2014-06-19 solr-1.1.0.tgz (106.2kB) [Changelog]
1.1.0b beta 2014-03-17 solr-1.1.0b.tgz (102.9kB)  DLL [Changelog]
2.0.0b beta 2014-02-28 solr-2.0.0b.tgz (105.7kB)  DLL [Changelog]
1.0.2 stable 2011-11-28 solr-1.0.2.tgz (98.9kB)  DLL [Changelog]
1.0.1 stable 2011-06-04 solr-1.0.1.tgz (98.3kB) [Changelog]
0.9.11 beta 2010-06-22 solr-0.9.11.tgz (96.1kB) [Changelog]
 
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 List
PHP 7.1 7.1 Non Thread Safe (NTS) x86 7.1 Thread Safe (TS) x86 7.1 Non Thread Safe (NTS) x64 7.1 Thread Safe (TS) x64
PHP 7.0 7.0 Non Thread Safe (NTS) x86 7.0 Thread Safe (TS) x86 7.0 Non Thread Safe (NTS) x64 7.0 Thread Safe (TS) x64
PHP 5.6 5.6 Non Thread Safe (NTS) x86 5.6 Thread Safe (TS) x86 5.6 Non Thread Safe (NTS) x64 5.6 Thread Safe (TS) x64
PHP 5.5 5.5 Non Thread Safe (NTS) x86 5.5 Thread Safe (TS) x86 5.5 Non Thread Safe (NTS) x64 5.5 Thread Safe (TS) x64
PHP 5.4 5.4 Non Thread Safe (NTS) x86 5.4 Thread Safe (TS) x86
PHP 5.3 5.3 Non Thread Safe (NTS) x86 5.3 Thread Safe (TS) x86
下载完以后将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>';
   

评论

推荐阅读

订阅邮件

订阅更新,及时收到最新文章。

作者