• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Solr 学习(六) ―- Solr的PHP客户端

php 搞代码 4年前 (2022-01-23) 39次浏览 已收录 0个评论

Solr 学习(6) ―- Solr的PHP客户端

solr查询返回只是xml格式或是json格式,并不像我们平时使用google或百度时的那种漂亮清爽的界面,实际上solr只负责数据的查询和返回,并不负责显示,如果要生成如google那样的界面,还的借助solr的php客户端,通过客户端进行查询后,再通过php代码显示出来。

本文的做法是一台服务器使用tomcat,运行solr,另一台服务器使用apache,负责和用户的交互与显示。

?

solr的php客户端也有好几个,本文选择了一个简单易用的,?php-solr-client ,该项目的地址为?http://code.google.com/p/solr-php-client/,下载后解压放到apache的网站根目录。

?

?下面是一个简单的查询例子:

?

?

<?php// make sure browsers see this page as utf-8 encoded HTMLheader('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<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b><strong>搞gaodaima代码</strong> php.ini)  require_once('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', 8983, '/solr/');  // 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("<title>SEARCH EXCEPTION</title><body><pre class="prettyprint linenums">{$e->__toString()}

“); }}?> PHP Solr Client Example <body> “/> <?php// display resultsif ($results){ $total = (int) $results->response->numFound; $start = min(1, $total); $end = min($limit, $total);?>

Results <?php echo $start; ?> – <?php echo $end;?> of <?php echo $total; ?>:
    <?php // iterate result documents foreach ($results->response->docs as $doc) {?>

  1. <?php // iterate document fields / values foreach ($doc as $field => $value) {?>

    <?php }?>

    <?php echo htmlspecialchars($field, ENT_NOQUOTES, ‘utf-8’); ?> <?php echo htmlspecialchars($value, ENT_NOQUOTES, ‘utf-8’); ?>
  2. <?php }?>

<?php}?> ?

进行查询,如输入car ,结果如下?

连接solr查询成功,再进行些美工上的优化,就能有像百度一样的查询界面了


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Solr 学习(六) ―- Solr的PHP客户端

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址