PHP Conference Japan 2024

SolrClient::setResponseWriter

(PECL solr >= 0.9.11)

SolrClient::setResponseWriter設定用於準備 Solr 回應的回應寫入器

說明

public SolrClient::setResponseWriter(字串 $responseWriter): void

設定用於準備 Solr 回應的回應寫入器

參數

responseWriter

以下其中之一

  • json
  • phps
  • xml

回傳值

不回傳任何值。

範例

範例 #1 SolrClient::setResponseWriter() 範例

<?php

// 這是我的自訂物件類別
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 代表獨立屬性,1 代表合併屬性

try
{

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

$resp = $response->getResponse();

print_r($response);

print_r($resp);

} catch (
Exception $e) {

print_r($e);

}

?>

新增筆記

使用者貢獻的筆記 1 則筆記

edwardtam at et-it dot net
9 年前
我發現 'php' 也被支援。

<?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 持續顯示

[12-Aug-2015 12:34:56 Asia/Hong_Kong] PHP 警告: SolrClient::setResponseWriter(): 不支援的回應寫入器 php。此值將在 C:\www\...\solr.php 的第 21 行被忽略

但回傳的資料卻如預期般正常

<?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;
?>
To Top