php solarium使用教程,Solarium简易使用

Solarium是什么

Solarium是Solr的PHP客户端类库

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值