在PHP中处理Solr结果的理想方法?
首先,我知道有一些类似的问题与此类似,但我认为这种情况不同,足以证明它自己的问题 我正在运行Solr索引,通过安装在LAMP服务器上的jetty。我目前使用在PHP中处理Solr结果的理想方法?,php,solr,simplexml,Php,Solr,Simplexml,首先,我知道有一些类似的问题与此类似,但我认为这种情况不同,足以证明它自己的问题 我正在运行Solr索引,通过安装在LAMP服务器上的jetty。我目前使用simplexml\u load\u file函数引入搜索结果,然后通过几个函数解析它们。我对这个过程很满意,直到我开始遇到一个根本性的问题 字段名不会通过simplexml函数传递。比如这个结果, <doc> <float name="score">0.73325396</float> <s
simplexml\u load\u file
函数引入搜索结果,然后通过几个函数解析它们。我对这个过程很满意,直到我开始遇到一个根本性的问题
字段名不会通过simplexml函数传递。比如这个结果,
<doc>
<float name="score">0.73325396</float>
<str name="add1">Ravensbridge Drive</str>
<str name="comments">0</str>
<str name="company">Stratstone Lotus Leicester</str>
<str name="feed_id"/>
<str name="id">1711765</str>
<str name="pcode">LE4 0BX</str>
<str name="psearch">LE4</str>
<str name="rating">0</str>
</doc>
当找到一个完整的数据集时,数组中存储了11位数据,但当其中一些数据丢失时,数据就会四处移动,我的解析器就会崩溃
所以,我已经研究了库/类来正确地完成它。即两个主要的,;但两者似乎都过于复杂,几乎没有实际的例子,而且它们都不支持不同的solr内核,我使用了几种
最好用什么?我现在被困在这里,任何帮助都将不胜感激
谢谢 当然可以使用现有的客户端之一。至于多核支持,只需为Solr的每个实例创建一个客户端实例即可 Solr扩展功能强大得多,但使用起来仍然非常直观。这里有两个示例代码段,它们使用两个库进行基本查询并返回结果:
感谢回复nuqqsa!我认为选项数组中的路径var将是核心路径,对吗?solr文件目前在我的根目录之外,所以我想我必须把它们移到根目录里面?我还有一些问题,但我会先尝试了解一下文档,再次感谢!这些示例在
http://localhost:8080/solr
,其中/solr(path)是相对于域的路径。这必须适应Solr API实例所在的位置。文件在根目录之外是什么意思?是的,这是一个很好的起点!:)solr php客户端使用JSON而不是XML解析响应。这在大多数情况下都很好,但当Solr的JSON响应编写器做了它不应该做的事情(比如在同一级别上设置同名键)时,就会中断。例如,在使用多个排序规则时会发生这种情况。
[doc] => Array
(
[0] => SimpleXMLElement Object
(
[float] => 0.73325396
[str] => Array
(
[0] => Ravensbridge Drive
[1] => 0
[2] => Stratstone Lotus Leicester
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => feed_id
)
)
[4] => 1711765
[5] => LE4 0BX
[6] => LE4
[7] => 0
)
)
<?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)
*/
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
*/