使用日光浴室从 php 访问 apache solr 抛出 UnexpectedValueException

accessing apache solr from php with solarium throws UnexpectedValueException

我才刚刚开始使用 Apache Solr,我也不是什么大 PHP 高手。

Apache Solr 是 运行,在浏览器中粘贴此查询会显示一个 XML 文档:

http://localhost:8983/solr/my_test/select?q=name:%22A%20Clash%20of%20Kings%22

但是,以下代码抛出 UnexpectedValueException:

<?php
require __DIR__.'/vendor/autoload.php';
// check solarium version available
echo 'Solarium library version: ' . Solarium\Client::VERSION . ' - ';

$config = array(
    'endpoint' => array(
        'localhost' => array(
            'host' => '127.0.0.1', 'port' => '8983', 'path' => '/solr/#/my_test/select?q=name:"A Clash of Kings"'
        )
    )
);


// create a client instance
$client = new Solarium\Client($config);

// // get a select query instance
$query = $client->createQuery($client::QUERY_SELECT);

// // this executes the query and returns the result
$resultset = $client->execute($query);

// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound(); // THROWS EXCEPTION

// create a ping query
$ping = $client->createPing();

// // execute the ping query
 try {
    $result = $client->ping($ping);
    echo 'Ping query successful';
     echo '<br/><pre>';
     var_dump($result->getResponse());
    echo '</pre>';
} catch (Solarium\Exception $e) {
    echo 'Ping query failed';
}

?>

输出:

Solarium library version: 3.0.0 - 
Fatal error: Uncaught exception 'Solarium\Exception\UnexpectedValueException' 
with message 'Solr JSON response could not be decoded' in C:\Server\xampp\htdocs\HTML\php\vendor\solarium\solarium\library\Solarium\Core\Query\Result\Result.php:158 Stack trace: #0 
C:\Server\xampp\htdocs\HTML\php\vendor\solarium\solarium\library\Solarium\QueryType\Select\ResponseParser\ResponseParser.php(61): 
Solarium\Core\Query\Result\Result->getData() #1 
C:\Server\xampp\htdocs\HTML\php\vendor\solarium\solarium\library\Solarium\Core\Query\Result\QueryType.php(73): 
Solarium\QueryType\Select\ResponseParser\ResponseParser->parse(Object(Solarium\QueryType\Select\Result\Result)) #2 
C:\Server\xampp\htdocs\HTML\php\vendor\solarium\solarium\library\Solarium\QueryType\Select\Result\Result.php(144): Solarium\Core\Query\Result\QueryType->parseResponse() #3 C:\Server\xampp\htdocs\HTML\php\lucene.php(25): 
Solarium\QueryType\Select\Result\Result->getNumFound() #4 {main} thrown in C:\Server\xampp\htdocs\HTML\php\vendor\solarium\solarium\library\Solarium\Core\Query\Result\Result.php on line 158

阅读 Github 上的 post 后,我更改了:

var_dump($result->getData());

的 ping 查询中
var_dump($result->getResponse());

因为getData也抛出了这个异常

让我有点惊讶的是

Solr JSON response could not be decoded

而是在浏览器中直接使用URLreturnsXML。我需要在某处配置消息的格式吗?我需要将它从 XML 更改为 JSON 还是相反?我在 windows 7.

上使用 Solr 5.3.1

当我注释掉抛出异常的行时,响应是:

Solarium library version: 3.0.0 - Ping query successful
object(Solarium\Core\Client\Response)#8 (4) {
  ["headers":protected]=>
  array(1) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"
  }
  ["body":protected]=>
  string(6243) "

配置 Solr 索引的路径时,不应包含任何处理程序或查询参数。在您的情况下,Solr 路径应该是 /solr/my_test。还有一些您应该进行的其他代码更改。试试这个...

$config = array(
    'endpoint' => array(
        'localhost' => array(
            'host' => '127.0.0.1',
            'port' => '8983',
            'path' => '/solr/my_test/'
        )
    )
);

$client = new Solarium\Client($config);
$query = $client->createSelect();
$helper = $query->getHelper();
$query->setQuery('name:' . $helper->escapePhrase('A Clash of Kings'));
$resultset = $client->select($query);
echo 'NumFound: ' . $resultset->getNumFound();

这是基于 Solarium 文档中的 phrase escaping example