3

I only just started with Apache Solr and I'm not a huge PHP crack either.

Apache Solr is running and pasting this query in the browser shows an XML document:

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

However, the following code throws an 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';
}

?>

The output:

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

After reading a post on Github I changed:

var_dump($result->getData());

in the ping query to

var_dump($result->getResponse());

because getData also threw this exception.

What surprises me a bit is that is says

Solr JSON response could not be decoded

but directly using the URL in the browser returns XML. Do I need to configure the format of the message somewhere? Do I need to change it from XML to JSON or vice versa or something? I am using Solr 5.3.1 on windows 7.

When I comment out the line that throws the exception, the reponse is:

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) "
user3629892
  • 2,960
  • 9
  • 33
  • 64

1 Answers1

3

When you configure the path to your Solr index, it should not include any handler or query parameters. In your case, the Solr path should be /solr/my_test. And there are a couple of other code changes you should make. Try this...

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

This is based on a phrase escaping example from the Solarium docs.

Patrick Lee
  • 1,990
  • 1
  • 19
  • 24