【问题标题】:accessing apache solr from php with solarium throws UnexpectedValueException使用日光浴室从 php 访问 apache solr 会引发 UnexpectedValueException
【发布时间】:2016-03-12 00:35:58
【问题描述】:

我才刚开始使用 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 上的一篇文章后,我改变了:

var_dump($result->getData());

在ping查询到

var_dump($result->getResponse());

因为 getData 也抛出了这个异常。

让我有点吃惊的是它说

Solr JSON response could not be decoded

但在浏览器中直接使用 URL 返回 XML。我需要在某处配置消息的格式吗?我是否需要将其从 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) "

【问题讨论】:

    标签: php solr solarium


    【解决方案1】:

    当您配置 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

    【讨论】:

    • 它现在可以工作了,就像你说的那样。但是,为此我不得不切换到 Solarium 版本 3.4.1。
    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 2016-03-03
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多