智能文章系统实战-文章全文搜索应用(5)

admin 发布于:2018-6-20 18:02  有 1646 人浏览,获得评论 0 条 标签: 全文搜索 

1.配置索引文件

[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.全文搜索结果(全部数据)

企业微信截图_20180621094335.png

 

 

5.全文搜索结果(关键词搜索) BAT架构  (http://演示域名/search.php?keyword=BAT架构&page=1)

 

企业微信截图_20180621095338.png