1,建立一个文件夹apache_solr,将下载好的solr解压,将5个文件夹拷贝到里面。
2,SolrPhpClient下载解压,将SolrPhpClient文件夹考到里面,此时apache_solr里面看起来像是这样。
3,安装jdk,命令行进入cd example,执行start.jar,如图,
回车,启动solr内置服务器,默认端口是8983,访问http://localhost:8983/solr/admin/进入实例图。
,说明服务器启动成功!此时搜索热和内容都看不到任何值。
4.如图,提交配置文件.xml,即可搜索到内容
5.http://localhost:8983/solr/browse,例子。第四步执行完毕后,可搜索。
6.在根目录下建立search.php文件。
<?php
require_once('SolrPhpClient/Apache/Solr/Service.php');
$solr = new Apache_Solr_Service( 'localhost', '8983', '/solr' );
if ( ! $solr->ping() ) {
echo 'Solr service not responding.';
exit;
}
如果没报错,证明php调用成功!
代码
<?php
/**
* solr 测试
*/
require_once(dirname(__FILE__).'SolrPhpClient/Apache/Solr/Service.php');
require_once(dirname(__FILE__).'SolrPhpClient/Apache/Solr/HttpTransport/Curl.php');
//测试使用基本连接 solr
defined('SOLR_HOST') || define('SOLR_HOST','localhost');
defined('SOLR_PORT') || define('SOLR_PORT','8983');
defined('SOLR_PATH') || define('SOLR_PATH','/solr/');
$solr = new Apache_Solr_Service(SOLR_HOST, SOLR_PORT, SOLR_PATH);
$newTransport = new Apache_Solr_HttpTransport_Curl();
$solr->setHttpTransport($newTransport);
($solr->ping()) || die('not connect solr');
echo 'solr runing';
if(!empty($_POST['do']) && 'add' == $_POST['do']){
// 开测构建document (需要被索引的文档, 这个可以来源于xml ,也可以是字符串,也可以来自数据库(DataImportHandler)
// 这里我使用字符串进行测试(刚开始学 :D)
$document = new Apache_Solr_Document();
$document->id = uniqid();
$document->title = $_POST['title'];
$document->name = $_POST['name'];
$document->manu = $_POST['manu'];
$document->cat = $_POST['cat'];
//$document->content = 'Some content for learn solr ,haha ,wo e le...';
$solr->addDocument($document);
$solr->commit();
}
$limit = 50;
// 显示内容
$query = !empty($_GET['q']) ? $_GET['q'] : '*:*';
if (get_magic_quotes_gpc() == 1)
{
$query = stripslashes($query);
}
try
{
$results = $solr->search($query, 0, $limit);
}
catch (Exception $e)
{
die("<html><head><title>查询失 败;</title><body><pre>{$e->__toString()}</pre></body></html>");
}
// display results
if ($results)
{
$total = (int) $results->response->numFound;
$start = min(1, $total);
$end = min($limit, $total);
}
?><!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Solr</title>
</head>
<body>
<form accept-charset="utf-8" method="get">
<label for="q">查询:</label>
<input id="q" name="q" type="text" value="<?php echo htmlspecialchars($query, ENT_QUOTES, 'utf-8'); ?>"/>
<input type="submit"/>
</form>
<div>返回: 从 <?php echo $start; ?> 到 <?php echo $end;?> 个结果, 共:<?php echo $total; ?>:</div>
<?php
// iterate result documents
foreach ($results->response->docs as $doc)
{
?>
<table style="border: 1px solid black; text-align: left;float: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>
<?php
}
?><div style="border:1px;clear:both;">
<form accept-charset="utf-8" method="post">
<h2>添加数据</h2>
<input type="hidden" value="add" name="do"></input>
<label for="title">标题:</label><input name="title" type="text" value=""/><p>
<label for="name">姓名:</label><input name="name" type="text" value=""/><p>
<label for="manu">manu:</label><input name="manu" type="text" value=""/><p>
<label for="cat">猫:</label><input name="cat" type="text" value=""/><p>
<input type="submit"/>
</form>
</div>
</body>
</html>