solr php大小写,Solr 学习(6) —- Solr的PHP客户端

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

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

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

// 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('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("

SEARCH EXCEPTION
{$e->__toString()}
");

}

}

?>

PHP Solr Client Example

Search:

// display results

if ($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; ?>:

// iterate result documents

foreach ($results->response->docs as $doc)

{

?>

// iterate document fields / values

foreach ($doc as $field => $value)

{

?>

<?php echo htmlspecialchars($field, ENT_NOQUOTES, 'utf-8'); ?><?php echo htmlspecialchars($value, ENT_NOQUOTES, 'utf-8'); ?>

}

?>

}

?>

}

?>

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

762df17e442b61f2abc805a0997503f0.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值