SolrClient::setResponseWriter

(PECL solr >= 0.9.11)

SolrClient::setResponseWriterSets the response writer used to prepare the response from Solr

说明

public void SolrClient::setResponseWriter ( string $responseWriter )

Sets the response writer used to prepare the response from Solr

参数

responseWriter

One of the following:

  • json
  • phps
  • xml

返回值

没有返回值。

范例

Example #1 SolrClient::setResponseWriter() example

<?php

// This is my custom class for objects
class SolrClass
{
   public 
$_properties = array();

   public function 
__get($property_name) {
      
      if (
property_exists($this$property_name)) {
      
          return 
$this->$property_name;
      
      } else if (isset(
$_properties[$property_name])) {
      
          return 
$_properties[$property_name];
      }
      
      return 
null;
   }
}

$options = array
(
  
'hostname' => 'localhost',
  
'port' => 8983,
  
'path' => '/solr/core1'
);

$client = new SolrClient($options);

$client->setResponseWriter("json");

//$response = $client->ping();

$query = new SolrQuery();

$query->setQuery("*:*");

$query->set("objectClassName""SolrClass");

$query->set("objectPropertiesStorageMode"1); // 0 for independent properties, 1 for combined

try
{

$response $client->query($query);

$resp $response->getResponse();

print_r($response);

print_r($resp);

} catch (
Exception $e) {

print_r($e);

}

?>

User Contributed Notes

edwardtam at et-it dot net 12-Aug-2015 04:44
i found that 'php' is also supported.

<?php
$solr_server
= array
(
   
'hostname'     => $solr_hostname,
   
'port'         => $solr_port,
   
'path'         => $solr_path,
);
$solr_client = new SolrClient($solr_server);

$solr_response_writer = 'php'; // "wt"
$solr_client->setResponseWriter($solr_response_writer);

$solr_response = new SolrObject();

$solr_query = new SolrQuery();
$solr_query->setQuery($solr_query_string); // "q"

try {
   
$query_response = $solr_client->query($solr_query);
   
$solr_response = $query_response->getResponse();
    return
'';
} catch (
Exception $e) {
    return (
$e);
}
?>

php_error.log keeps on saying:

[12-Aug-2015 12:34:56 Asia/Hong_Kong] PHP Warning:  SolrClient::setResponseWriter(): Unsupported response writer php. This value will be ignored in C:\www\...\solr.php on line 21

but the returned data is good as expected:

<?php
$html
.= '<table cellSpacing="1" cellPadding="1" border="1">';
$html .= '<tr>';
$html .= '<td>id</td>';
$html .= '<td>title</td>';
$html .= '<td>score</td>';
$html .= '</tr>';

for (
$i_solr = 0; $i_solr <= ($solr_rows - 1); $i_solr++) {
   
$html .= '<tr>';
   
$html .= '<td>' . $solr_response['response']['docs'][$i_solr]['id'] . '</td>';
   
$html .= '<td>' . $solr_response['response']['docs'][$i_solr]['title'][0] . '</td>';
   
$html .= '<td>' . $solr_response['response']['docs'][$i_solr]['score'] . '</td>';
   
$html .= '</tr>';
}
$html .= '</table>';
echo
$html;
?>