当然,使用现有的客户之一。对于多核支持,它与为每个solr实例创建客户机实例一样简单。
Solr扩展功能更强大,但使用起来仍然非常直观。这里有两个示例代码片段,用于进行基本查询并使用这两个库返回结果:
PHP Solr extension
<?php
$options = array
(
'hostname' => 'localhost',
'port' => '8080',
'path' => '/solr'
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('fox');
$query->setStart(0);
$query->setRows(50);
// specify which fields do we want to retrieve
$query->addField('id')->addField('title_t')->addField('source_t');
$res = $client->query($query)->getResponse();
// how does he response look like?
var_dump($res);
/*
object(SolrObject)[4]
public 'responseHeader' =>
object(SolrObject)[5]
public 'status' => int 0
public 'QTime' => int 0
public 'params' =>
object(SolrObject)[6]
public 'fl' => string 'id,title_t,source_t' (length=19)
public 'indent' => string 'on' (length=2)
public 'start' => string '0' (length=1)
public 'q' => string 'fox' (length=3)
public 'wt' => string 'xml' (length=3)
public 'rows' => string '50' (length=2)
public 'version' => string '2.2' (length=3)
public 'response' =>
object(SolrObject)[7]
public 'numFound' => int 39
public 'start' => int 0
public 'docs' =>
array
0 =>
object(SolrObject)[8]
...
1 =>
object(SolrObject)[9]
...
2 =>
object(SolrObject)[10]
...
(...)
*/
// how does a document look like?
var_dump($res->reponse->docs[0]);
/*
object(SolrObject)[8]
public 'id' => int 11408
public 'source_t' => string 'CBD News Headlines' (length=18)
public 'title_t' => string 'Hunting across Southeast Asia weakens forests' survival' (length=55)
*/
solr-php-client
(
official example of use
)
require_once 'library/SolrPhpClient/Apache/Solr/Service.php';
$solr = new Apache_Solr_Service('localhost', '8080', '/solr');
if (!$solr->ping()) {
exit('Solr service not responding.');
}
$offset = 0;
$limit = 50;
$query = 'fox';
$res = $solr->search($query, $offset, $limit);
// how does he response look like?
var_dump($res->response);
/*
object(stdClass)[6]
public 'numFound' => int 39
public 'start' => int 0
public 'docs' =>
array
0 =>
object(Apache_Solr_Document)[46]
protected '_documentBoost' => boolean false
protected '_fields' =>
array
...
protected '_fieldBoosts' =>
array
...
1 =>
object(Apache_Solr_Document)[47]
protected '_documentBoost' => boolean false
protected '_fields' =>
array
...
protected '_fieldBoosts' =>
array
...
(...)
*/
// how does a document look like?
var_dump($res->response->doc[0]);
/*
object(Apache_Solr_Document)[46]
protected '_documentBoost' => boolean false
protected '_fields' =>
array
'publicationTime_i' => int 1257724800
'publicationDate_t' => string 'Mon, 9 Nov 2009' (length=15)
'url_s' => string 'http://news.mongabay.com/2009/1108-hance_corlett.html' (length=53)
'language_s' => string 'EN' (length=2)
'title_t' => string 'Hunting across Southeast Asia weakens forests' survival' (length=55)
'text' => string 'A large flying fox eats a fruit ingesting its seeds.' (length=52)
'id' => int 11408
'relevance_i' => int 27
'source_t' => string 'CBD News Headlines' (length=18)
protected '_fieldBoosts' =>
array
'publicationTime_i' => boolean false
'publicationDate_t' => boolean false
'url_s' => boolean false
'language_s' => boolean false
'title_t' => boolean false
'text' => boolean false
'id' => boolean false
'relevance_i' => boolean false
'source_t' => boolean false
*/