discuz使用xunsearch搜索 包含分类信息,不是插件不是插件!!!
使用的前提是先安装xunsearch,按官网的提示安装:
1.安装xunsearch
安装、升级 – 权威指南 – 迅搜(xunsearch) – 开源免费中文全文搜索引擎
哪个目录自己安装,不需要系统盘,如果网站大,尽量安装剩余空间更大的盘中。
2.安装完后,在/home/xunsearch/sdk/php/app这个目录是你安装的xunsearch目录。创建一个新的discuz.ini文件。是哪个编码的自己改一下洛,utf-8还是gbk
project.name = discuz // 这里可改成你需要的名字,然后后面创建的时候需要用到,记住即可。
project.default_charset = utf-8
#server.index = 192.168.0.114:8383
#server.search = 192.168.0.114:8384
[pid]
type = id
[fid]
index = self
tokenizer = full
[tid]
index = self
tokenizer = full
[first]
index = self
tokenizer = full
[position]
tokenizer = full
[author]
index = both
[authorid]
index = self
tokenizer = full
[subject]
type = title
[message]
type = body
[dateline]
type = numeric
3.将网站目录下的原来discuz带的search.php改名字或直接删掉。
4.下载附件的内容,上传到网站根目录。
5.附件中需要修改的内容:
search.php(71行),以及suggest.php(18行)
改为
$xs = new XS(‘discuz’);
如果你在第2的时候设置为discuz,就设置为discuz,其他名字就其他名字
6.在xunsearch安装目录下创建一个PHP文件,在哪里创建都行,名字随便,我的是discuz.php。我创建的是在xunsearch安装的目录下/home/xunsearch
<?php
require_once '/home/xunsearch/sdk/php/lib/XS.php';
$conn = mysqli_connect('localhost', '数据库用记名', '数据库密码', '数据库表名');
// 创建索引对象
$xs = new XS('discuz');
$index = $xs->index;
// 每页数据条数
$pageSize = 1000;
$page = 0;
while (true) {
$message1 = '';
// 查询数据库
$sql1 = "SELECT * FROM pre_forum_post LIMIT ". $page * $pageSize. ", ". $pageSize;
$re = mysqli_query($conn, $sql1);
$rows = array();
while ($row = mysqli_fetch_assoc($re)) {
$rows[$row['tid']]['pid'] = $row['pid'];
$rows[$row['tid']]['fid'] = $row['fid'];
$rows[$row['tid']]['tid'] = $row['tid'];
$rows[$row['tid']]['author'] = $row['author'];
$rows[$row['tid']]['authorid'] = $row['authorid'];
$rows[$row['tid']]['subject'] = $row['subject'];
$rows[$row['tid']]['dateline'] = $row['dateline'];
$rows[$row['tid']]['message'] = $row['message'];
$tids[] = $row['tid'];
}
if (empty($rows)) {
break;
}
$tidsStr = implode(',', $tids);
$sql2 = "SELECT * FROM pre_forum_typeoptionvar WHERE tid IN ($tidsStr)";
$re2 = mysqli_query($conn, $sql2);
$values = array();
while ($value = mysqli_fetch_assoc($re2)) {
$values[$value['tid']][$value['optionid']] = $value['value'];
}
foreach ($rows as $key => $v) {
foreach ($values as $key1 => $v2) {
$valueStr = implode(' ', $v2);
if ($key == $key1) {
$message1 = $v['message'];
$mmm = $message1. $valueStr;
$rows[$key]['message'] = $mmm;
}
}
}
// 添加数据到索引
foreach ($rows as $row) {
$itemArr = array(
'pid' => $row['pid'],
'fid' => $row['fid'],
'tid' => $row['tid'],
'author' => $row['author'],
'authorid' => $row['authorid'],
'subject' => $row['subject'],
'message' => $row['message'],
'dateline' => $row['dateline']
);
$doc = new XSDocument();
$doc->setFields($itemArr);
$index->add($doc);
/*
$search = $xs->search;
$existingDocs = $search->setQuery('pid:'. $row['pid'])->search();
if (count($existingDocs) === 0) {
$itemArr = array(
'pid' => $row['pid'],
'fid' => $row['fid'],
'tid' => $row['tid'],
'author' => $row['author'],
'authorid' => $row['authorid'],
'subject' => $row['subject'],
'message' => $row['message'],
'dateline' => $row['dateline']
);
$doc = new XSDocument();
$doc->setFields($itemArr);
$index->add($doc);
}
var_dump($itemArr);
*/
}
// 清理临时变量
$tids = array();
$rows = array();
$values = array();
$page++;
sleep(5);
}
// 优化索引
$index->flushIndex();
?>
7.运行脚本
切换到目录,我的是/home/xunsearch
nohup php discuz.php
如果你的网站需要自动添加更新,需要将tid保存到目录下,再改掉数据库查询判断,再创建一个计划让这个php自动运行。
好了,直接可以使用。展示模板效果请修改search.tpl
效果测试:朝桐光 – xb1搜索
链接: https://pan.baidu.com/s/1kaDbv8L9HLM-BF5ek3EgwA?pwd=unht 提取码: unht