使用solarium从php访问apache solr会引发意外的ValueException

使用solarium从php访问apache solr会引发意外的ValueException,php,solr,solarium,Php,Solr,Solarium,我刚开始使用ApacheSolr,我也不是一个PHP高手 Apache Solr正在运行,在浏览器中粘贴此查询将显示一个XML文档: http://localhost:8983/solr/my_test/select?q=name:%22A%20Clash%20of%20Kings%22 但是,以下代码会引发意外的ValueException: <?php require __DIR__.'/vendor/autoload.php'; // check solarium version

我刚开始使用ApacheSolr,我也不是一个PHP高手

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

http://localhost:8983/solr/my_test/select?q=name:%22A%20Clash%20of%20Kings%22
但是,以下代码会引发意外的ValueException:

<?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';
}

?>
在阅读Github上的帖子后,我改变了:

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

Solr JSON response could not be decoded
因为getData也引发了此异常

让我有点吃惊的是,他说

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) "
但是在浏览器中直接使用URL会返回XML。我需要在某处配置消息的格式吗?我是否需要将它从XML更改为JSON,或者反之亦然?我在Windows7上使用Solr5.3.1

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

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

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


这是基于Solarium文档中的一个示例。

按照您的说法,它现在可以工作了。然而,为此我不得不切换到Solarium版本3.4.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) "
$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();