【问题标题】:Solr commands not returning json outputSolr 命令不返回 json 输出
【发布时间】:2014-03-10 09:10:57
【问题描述】:

我正在浏览 solr 的 php 文档,并想尝试基本示例。但是当我在现有项目中尝试以下代码时,没有收到预期的输出并返回空白页。

<?php
include "bootstrap.php";

$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login'    => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port'     => SOLR_SERVER_PORT,
);

$client = new SolrClient($options);

$doc = new SolrInputDocument();



$doc->addField('ID', 334455);
//$doc->addField('cat', 'Software');
//$doc->addField('cat', 'Lucene');

$updateResponse = $client->addDocument($doc);

print_r($updateResponse->getResponse());

?>

bootstrap.php 是这样的:

/* Domain name of the Solr server */
define('SOLR_SERVER_HOSTNAME', 'localhost');

/* Whether or not to run in secure mode */
define('SOLR_SECURE', false);

/* HTTP Port to connection */
define('SOLR_SERVER_PORT', ((SOLR_SECURE) ? 8443 : 8080));

/* HTTP Basic Authentication Username */
define('SOLR_SERVER_USERNAME', 'admin');

/* HTTP Basic Authentication password */
define('SOLR_SERVER_PASSWORD', 'changeit');

/* HTTP connection timeout */
/* This is maximum time in seconds allowed for the http data transfer operation.Default value is 30 seconds */
define('SOLR_SERVER_TIMEOUT', 10);

/* File name to a PEM-formatted private key + private certificate (concatenated in that    order) */
define('SOLR_SSL_CERT', 'certs/combo.pem');

/* File name to a PEM-formatted private certificate only */
define('SOLR_SSL_CERT_ONLY', 'certs/solr.crt');

/* File name to a PEM-formatted private key */
define('SOLR_SSL_KEY', 'certs/solr.key');

/* Password for PEM-formatted private key file */
define('SOLR_SSL_KEYPASSWORD', 'StrongAndSecurePassword');

/* Name of file holding one or more CA certificates to verify peer with*/
define('SOLR_SSL_CAINFO', 'certs/cacert.crt');

/* Name of directory holding multiple CA certificates to verify peer with */
define('SOLR_SSL_CAPATH', 'certs/');

?>

我正在使用 solr_server_username/password 的 tomcat 用户名和密码。并且 solr 在 UI 上工作正常。我正在使用 solr 4.6.1 和 php solrclient 1.0.2

    <?php

// make sure browsers see this page as utf-8 encoded HTML
header('Content-Type: text/html; charset=utf-8');

$limit = 10;
$query = isset($_REQUEST['q']) ? $_REQUEST['q'] : false;
$results = false;

if ($query)
{
  // The Apache Solr Client library should be on the include path
  // which is usually most easily accomplished by placing in the
  // same directory as this script ( . or current directory is a default
  // php include path entry in the php.ini)
  require_once('/usr/local/apache-tomcat-7.0.50/SolrPhpClient/Apache/Solr/Service.php');

  // create a new solr service instance - host, port, and webapp
  // path (all defaults in this example)
  $solr = new Apache_Solr_Service('localhost', 8080, '/solr/collection1');


  // if magic quotes is enabled then stripslashes will be needed
  if (get_magic_quotes_gpc() == 1)
  {
    $query = stripslashes($query);
  }

  // in production code you'll always want to use a try /catch for any
  // possible exceptions emitted  by searching (i.e. connection
  // problems or a query parsing error)
  try
  {
    $results = $solr->search($query, 0, $limit);
  }
  catch (Exception $e)
  {
    // in production you'd probably log or email this error to an admin
        // and then show a special message to the user but for this example
        // we're going to show the full exception

        die("<html><head><title>SEARCH EXCEPTION</title><body><pre>{$e->__toString()}</pre></body></html>");
  }
}

?>
<html>
  <head>
    <title>PHP Solr Client Example</title>
  </head>
  <body>
      <h1>ABCD</h1>
    <form  accept-charset="utf-8" method="get">
      <label for="q">Search:</label>
      <input id="q" name="q" type="text" value="<?php echo htmlspecialchars($query, ENT_QUOTES, 'utf-8'); ?>"/>
      <input type="submit"/>
    </form>
<?php

// display results
if ($results)
{
  $total = (int) $results->response->numFound;
  $start = min(1, $total);
  $end = min($limit, $total);
?>
    <div>Results <?php echo $start; ?> - <?php echo $end;?> of <?php echo $total; ?>:</div>
    <ol>
<?php
  // iterate result documents
  foreach ($results->response->docs as $doc)
  {
?>
      <li>
        <table style="border: 1px solid black; text-align: left">
<?php
    // iterate document fields / values
    foreach ($doc as $field => $value)
    {
?>
          <tr>
            <th><?php echo htmlspecialchars($field, ENT_NOQUOTES, 'utf-8'); ?></th>
            <td><?php echo htmlspecialchars($value, ENT_NOQUOTES, 'utf-8'); ?></td>
          </tr>
    <?php
    }
    ?>
        </table>
      </li>
    <?php
  }
?>
    </ol>
    <?php
    }?>

在尝试上述替代代码时,遇到以下错误:

异常“Apache_Solr_HttpTransportException”与消息“0” 状态:通讯错误'在 /usr/local/apache-tomcat-7.0.50/SolrPhpClient/Apache/Solr/Service.php:338 堆栈跟踪:

0 /usr/local/apache-tomcat-7.0.50/SolrPhpClient/Apache/Solr/Service.php(1170):

Apache_Solr_Service->_sendRawGet - ('http://localhos...')

1 /var/www/html/testproject/tryout.php(33): Apache_Solr_Service->search('abhilash', 0, 10)

2 {主}

【问题讨论】:

  • 你可能想在代码中使用它们之前定义常量($options)。
  • 我已经在 bootstrap.php 中定义了它,它显示在代码第一段的正下方。哎呀,我的坏!我现在将对其进行编辑。
  • 所以你说第三个代码片段的注释行有效?
  • 再次抱歉,这些行在程序中并没有真正注释。粘贴到这里是一个错误。但是,是的,该段是引发错误的原因。对于一个格式错误的问题,我真的很抱歉。我现在已经做出改变了。
  • 这可能会对您有所帮助:groups.google.com/forum/#!topic/php-solr-client/GoMBww0jl5Y 如果不尝试注释所有行并从顶部开始调试变量 - 如果工作转到下一行 - 否则在此处发布错误

标签: php apache tomcat solr


【解决方案1】:

Solr 的默认响应编写器是 XML。

如果您想以 JSON 格式返回结果,您应该通过定义 URL 参数 wt=json 来指定它

例子

http://localhost:8983/solr/customers/select?q=customerSearchName&wt=json

确保你这样做。

【讨论】:

  • 好的,但是当我使用 $solr->search($query, 0, $limit); 时我在哪里可以做到这一点;
  • 我对 Solr 的 PHP Api 不熟悉。据我了解,以下页面可能对您有所帮助php.net/manual/en/solrclient.setresponsewriter.php 您应该执行类似 $solr->setResponseWriter(``type of response here'') 的操作,其中响应类型可以是 XML 或“phpnative”。似乎不支持 JSON。如果您想将 JSON 作为返回类型,您可以使用 PHP 中的 curl 方法来完成此操作并取回您的 JSON。
【解决方案2】:

你需要添加

'wt' => 'json'

到你传递给 SolrClient() 的数组

http://php.net/manual/en/solrclient.construct.php

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2011-12-11
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多