智能文章系统实战-文章全文搜索应用(5)
发布于:2018-6-20 18:02 作者:admin 浏览:28291.配置索引文件
[root@localhost app]# cat article.ini project.name = article project.default_charset = utf-8 server.index = 8383 server.search = 8384 [id] type = id [title] type = title [content] type = body [times] type = numeric [root@localhost app]#
2.索引数据库
<?php
require_once('./inc.php');
require_once('/usr/local/soft/xunsearch/sdk/php/lib/XS.php');
$xs = new XS('article'); //article为项目名称,配置文件是 /usr/local/soft/xunsearch/sdk/app/article.ini
$index = $xs->index; //获取索引对象
//读取 position.ini 文件中上次索引ID的位置 的文件名 (单机服务器)
$fileName='position.ini';
//清空索引
if($_GET['clean']==1)
{
$index->clean();
file_put_contents($fileName,0);
exit();
}
//读取 position.ini 文件中上次索引ID的位置 (单机服务器)
$position=0;
if(file_exists($fileName))
{
$position=intval(file_get_contents($fileName));
}
else
{
file_put_contents($fileName,0);
}
//读取数据库的文章,进行索引。
//每5分钟更新一次说索引,每次更新100条。
$sql="SELECT * FROM article WHERE id>".$position." ORDER BY id ASC LIMIT 100";
$result=$db->query($sql);
if($result)
{
while($info=$result->fetch_array())
{
//把MYSQL数据库的数据转化到索引数据库
$data = array(
'id' => $info['id'], // 此字段为主键,必须指定
'title' => $info['title'],
'content' => strip_tags($info['content']),
'times' => $info['times']
);
//创建文档对象
$doc = new XSDocument;
$doc->setFields($data);
//添加到索引数据库中
$index->add($doc);
//记录最后ID的位置
$position=$info['id'];
}
}
//把最后ID的位置写入position.ini
file_put_contents($fileName,$position);
echo "Complete!";
?>
3.全文搜索
<?php
error_reporting(0);
require_once('/usr/local/soft/xunsearch/sdk/php/lib/XS.php');
$xs = new XS('article'); //demo为项目名称,配置文件是 /usr/local/soft/xunsearch/sdk/app/article.ini
$search = $xs->search; //获取搜索对象
//获取参数
$keyword=trim($_REQUEST['keyword']);
$page=intval($_REQUEST['page']);
//关键词搜索
if($keyword)
{
$search->setQuery($keyword); //搜索 测试
}
//获取结果总数
$total=$search->count();
$pageSize=10;
$maxPage=ceil($total/$pageSize);
if($page>$maxPage)
{
$page=$maxPage;
}
if($page<1)
{
$page=1;
}
$offset=($page-1)*$pageSize;
//设置搜索分页
$search->setLimit($pageSize,$offset);
//搜索到的文档
$docs = $search->search();
//搜索结束
//开始输出页面数据
$html = <<<HTML_BEGIN
<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="apple-mobile-web-app-title" content="">
<title>全文搜索结果</title>
<style>
.design a{line-height:25px;}
body{background:#FFF;}
em{color:#F00;}
</style>
</head>
<body>
HTML_BEGIN;
echo $html;
echo "<ul>";
foreach ($docs as $doc) {
$title = $search->highlight($doc->title); // 高亮处理标题
echo "<li><a href='#".$doc->id."'>".$title."</a></li>";
}
echo "</ul>";
echo '<a href="?keyword='.$keyword.'&page='.($page-1).'">上一页</a> <b>'.$page.'/'.$maxPage.'</b> <a href="?keyword='.$keyword.'&page='.($page+1).'">下一页</a>';
$html = <<<HTML_END
</body>
</html>
HTML_END;
echo $html;
?>
4.全文搜索结果(全部数据)
5.全文搜索结果(关键词搜索) BAT架构 (http://演示域名/search.php?keyword=BAT架构&page=1)
智能文章系统实战-文章全文搜索安装和测试(4)
发布于:2018-6-19 20:05 作者:admin 浏览:30181.运行下面指令下载、解压安装包
wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 tar -xjf xunsearch-full-latest.tar.bz2 mv xunsearch-full-latest xunsearch cd xunsearch sh setup.sh bin/xs-ctl.sh restart php ./sdk/php/util/RequiredCheck.php
2.测试数据
cat ./sdk/php/app/demo.ini ./sdk/php/util/Indexer.php --source=csv --clean demo #输入测试数据 1,关于 xunsearch 的 DEMO 项目测试,项目测试是一个很有意思的行为!,1314336158 2,测试第二篇,这里是第二篇文章的内容,1314336160 3,项目测试第三篇,俗话说,无三不成礼,所以就有了第三篇,1314336168 #测试搜索结果 ./sdk/php/util/Quest.php demo 项目
3.PHP搜索
<?php
require_once('/usr/local/soft/xunsearch/sdk/php/lib/XS.php');
$xs = new XS('demo'); //demo为项目名称,配置文件是 /usr/local/soft/xunsearch/sdk/app/demo.ini
//$index = $xs->index; //获取索引对象
$search = $xs->search; //获取搜索对象
$search->setLimit(20);
$keyword='测试';
$docs = $search->setQuery($keyword)->search(); //搜索 测试
//var_dump($docs);
echo "<ul>";
foreach ($docs as $doc) {
$subject = $search->highlight($doc->subject); // 高亮处理标题
echo "<li>".$subject."</li>";
}
echo "</ul>";
?>

