Solarium简易使用

Solarium是什么

原文: https://www.hoehub.com/PHP/97.html

SolariumSolrPHP客户端类库

Solarium是一个精确建模Solr概念的PHP Solr客户端库。使用Solarium可以更专业于业务层面, 不用去理会Solr的底层通信

官方描述:

What is Solarium?
Solarium is a PHP Solr client library that accurately model Solr concepts. Where many other Solr libraries only handle the communication with Solr, Solarium also relieves you of handling all the complex Solr query parameters using a well documented API.

简易使用

// 引入类
use Solarium\Core\Client\Client as SolrClient;

demo

$config = [
            'endpoint' => [
                'endpoint1' => [
                    'host' => 'localhost',
                    'port' => '8983',
                    'path' => '/solr',
                    'core' => 'endpoint1',
                    'timeout' => 15,
                ],
                'endpoint2' => [
                    'host' => $host,
                    'port' => $port,
                    'path' => $path,
                    'core' => 'endpoint2',
                    'timeout' => 15,
                ],
                ...
            ]
        ];
// 实例client
$solrClient = new SolrClient($config);
// 设置默认的Endpoint
$solrClient->setDefaultEndpoint('endpoint1');
// 实例查询器
$query = $solrClient->createSelect();

// 查询姓名为张小明的文档
$query->createFilterQuery('name')->setQuery('name:张小明');
// 对应url大概是这样 http://localhost:8983/solr/SResume/select?q=name%3A张小明&wt=json&indent=true

// 查询性别为m的
$query->createFilterQuery('gender')->setQuery('gender:m');
// 对应url大概是这样 http://localhost:8983/solr/SResume/select?q=gender%3Am&wt=json&indent=true

// 排除已经删除的
$query->createFilterQuery('deleted_at')->setQuery('-deleted_at:*');
// 对应url大概是这样 http://localhost:8983/solr/SResume/select?q=*%3A*&fq=-deleted_at%3A*&wt=json&indent=true

// 查询年龄在20岁以上的
$query->createFilterQuery('age')->setQuery('age:[20 TO *]');
// 对应url大概是这样 http://localhost:8983/solr/SResume/select?q=age%3A%5B20+TO+*%5D&wt=json&indent=true

// 区间查询
$query->createFilterQuery('age')->setQuery('age:[20 TO 30]');
// 对应url大概是这样 http://localhost:8983/solr/SResume/select?q=age%3A%5B20+TO+30%5D&wt=json&indent=true

$query->setFields('score', 'name', 'gender', 'deleted_at', 'age');
$query->setSorts(['score' => $query::SORT_DESC]); // 按分数排序
$query->setOmitHeader(false);

// 获取结果
$resultSet = $this->solrClient->select($query);

相关推荐