Solarium簡易使用

Solarium是什麼

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

SolariumSolrPHP客戶端類庫html

Solarium是一個精確建模Solr概念的PHP Solr客戶端庫。使用Solarium能夠更專業於業務層面, 不用去理會Solr的底層通訊

官方描述:git

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;

demogithub

$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);
相關文章
相關標籤/搜索