Solarium简易用法

Solarium是什么

SolariumSolrPHP客户端类库

官方描述:

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);

我来吐槽

*

*

已有 4 条评论

  1. 果树交易平台

    博主精通的东西很多呀,看起来有点复杂,可能我是外行不是很懂这些代码

    1. Hoe

      过奖了, 雕虫小技而已😊

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