Skip to content

Commit cdbb190

Browse files
committed
全文索引管理
1 parent c78a062 commit cdbb190

File tree

11 files changed

+349
-28
lines changed

11 files changed

+349
-28
lines changed

script/build_answer_index.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
require dirname(__DIR__).'/server/config.php';
3+
4+
Swoole::$php->db->debug = true;
5+
6+
$question_table = table('aws_question');
7+
$question_table->primary = 'question_id';
8+
$question_table->select = 'question_content';
9+
10+
$table = table('aws_answer');
11+
$table->primary = 'answer_id';
12+
$table->select = 'answer_id, question_id, answer_content, add_time';
13+
14+
$pages = $table->all();
15+
echo "count=".count($pages)."\n";
16+
17+
$index = new App\Indexer('answer');
18+
$index->beginRebuild();
19+
20+
foreach($pages as $v)
21+
{
22+
$q = $question_table->get($v['question_id'])->get();
23+
if (empty($q))
24+
{
25+
echo "no question [{$v['question_id']}], skip.\n";
26+
}
27+
$data = array(
28+
'pid' => $v['answer_id'],
29+
'question_id' => $v['question_id'],
30+
'subject' => $q['question_content'],
31+
'message' => $v['answer_content'],
32+
'add_time' => $v['add_time'],
33+
);
34+
$ret = $index->add($data);
35+
echo "index #{$v['answer_id']} ok\n";
36+
}
37+
38+
$index->endRebuild();

script/build_group_index.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@
22
require dirname(__DIR__).'/server/config.php';
33
require APPSPATH.'/classes/xunsearch/lib/XS.php';
44

5-
$wikis = table('wiki_tree')->gets(array('project_id' => 1));
6-
$table = table('wiki_content');
5+
Swoole::$php->db->debug = true;
76

8-
echo "count=".count($wikis)."\n";
7+
$table = table('aws_question');
8+
$table->primary = 'question_id';
9+
$table->select = 'question_id, question_content, question_detail, update_time';
910

10-
$xs = new XS(WEBPATH.'/search.ini');
11+
$pages = $table->all();
12+
echo "count=".count($pages)."\n";
13+
14+
$xs = new XS(APPSPATH.'/configs/search/question.ini');
1115

1216
$index = $xs->index;
1317
$index->beginRebuild();
1418

15-
foreach($wikis as $v)
19+
foreach($pages as $v)
1620
{
17-
$wiki = $table->get($v['id'])->get();
21+
$page = $table->get($v['question_id'])->get();
1822
$data = array(
19-
'pid' => $v['id'],
20-
'subject' => $v['text'],
21-
'message' => $wiki['content'],
22-
'chrono' => time(),
23+
'pid' => $v['question_id'],
24+
'subject' => $v['question_content'],
25+
'message' => $v['question_detail'],
26+
'chrono' => $v['update_time'],
2327
);
24-
2528
$doc = new XSDocument;
2629
$doc->setFields($data);
27-
2830
$ret = $index->add($doc);
29-
echo "index #{$v['id']} ok\n";
31+
echo "index #{$v['question_id']} ok\n";
3032
}
3133

3234
$index->endRebuild();

script/build_index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
echo "count=".count($wikis)."\n";
99

10-
$xs = new XS(WEBPATH.'/search.ini');
10+
$xs = new XS(APPSPATH.'/configs/search/wiki.ini');
1111

1212
$index = $xs->index;
1313
$index->beginRebuild();

server/apps/classes/Indexer.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace App;
3+
4+
use Swoole;
5+
6+
require_once dirname(__DIR__) . '/classes/xunsearch/lib/XS.php';
7+
8+
class Indexer
9+
{
10+
static $types = [
11+
'wiki' => [],
12+
'answer' => [],
13+
'question' => [],
14+
];
15+
16+
protected $xs;
17+
18+
function __construct($type)
19+
{
20+
$iniFile = \Swoole::$app_path . '/configs/search/' . $type . '.ini';
21+
if (!is_file($iniFile))
22+
{
23+
throw new Swoole\Exception\NotFound("type[$type] not found.");
24+
}
25+
$this->xs = new \XS($iniFile);
26+
}
27+
28+
function add($data)
29+
{
30+
$doc = new \XSDocument;
31+
$doc->setFields($data);
32+
return $this->xs->getIndex()->add($doc);
33+
}
34+
35+
function update($data)
36+
{
37+
$doc = new \XSDocument;
38+
$doc->setFields($data);
39+
return $this->xs->getIndex()->update($doc);
40+
}
41+
42+
function del($id)
43+
{
44+
return $this->xs->getIndex()->del($id);
45+
}
46+
47+
function beginRebuild()
48+
{
49+
return $this->xs->getIndex()->beginRebuild();
50+
}
51+
52+
function endRebuild()
53+
{
54+
return $this->xs->getIndex()->endRebuild();
55+
}
56+
}

server/apps/configs/search/answer.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
project.name = swoole_group_answer
2+
project.default_charset = utf-8
3+
server.index = 127.0.0.1:8383
4+
server.search = 127.0.0.1:8384
5+
6+
[pid]
7+
type = id
8+
9+
[subject]
10+
type = title
11+
12+
[message]
13+
type = body
14+
15+
[add_time]
16+
type = numeric
17+
18+
[question_id]
19+
type = numeric
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
project.name = swoole_group_question
2+
project.default_charset = utf-8
3+
server.index = 127.0.0.1:8383
4+
server.search = 127.0.0.1:8384
5+
6+
[pid]
7+
type = id
8+
9+
[subject]
10+
type = title
11+
12+
[message]
13+
type = body
14+
15+
[chrono]
16+
type = numeric
File renamed without changes.

server/apps/controllers/Wiki.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,37 @@ function search()
6868
return "请输入搜索的关键词";
6969
}
7070

71-
$this->getProjectInfo();
72-
$this->getProjectLinks();
73-
$this->getTreeData();
74-
$_GET['id'] = $this->project['home_id'];
75-
7671
$pagesize = 10;
7772
$page = empty($_GET['page']) ? 1: intval($_GET['page']);
7873

74+
/**
75+
* 搜索类型
76+
*/
77+
$type = empty($_GET['type']) ? 'wiki' : trim($_GET['type']);
78+
$iniFile = Swoole::$app_path . '/configs/search/' . $type . '.ini';
79+
if (!is_file($iniFile))
80+
{
81+
$this->http->status(403);
82+
return "bad request";
83+
}
84+
85+
$link_tpl = '';
86+
$page_tpl = "/wiki/search/?type={$type}&q=" . urlencode($_GET['q']) . '&page={page}';
7987
$s = microtime(true);
80-
$xs = new \XS(WEBPATH.'/search.ini');
88+
if ($type == 'wiki')
89+
{
90+
$link_tpl = '/wiki/page/{id}.html';
91+
}
92+
elseif ($type == 'question')
93+
{
94+
$link_tpl = 'http://group.swoole.com/question/{id}';
95+
}
96+
elseif ($type == 'answer')
97+
{
98+
$link_tpl = 'http://group.swoole.com/question/{question_id}#answer_list_{id}';
99+
}
100+
101+
$xs = new \XS($iniFile);
81102
$search = $xs->getSearch();
82103
$q = trim($_GET['q']);
83104
$search->setQuery($q);
@@ -95,14 +116,19 @@ function search()
95116
$li['id'] = $doc->pid;
96117
$li['title'] = self::highlight($search, $doc->subject);
97118
$li['desc'] = self::highlight($search, $doc->message);
119+
if ($type == 'answer')
120+
{
121+
$li['question_id'] = $doc->question_id;
122+
}
98123
$list[] = $li;
99124
}
100-
$pager->page_tpl = "/wiki/search/?q=".urlencode($_GET['q']).'&page={page}';
101-
$this->tpl->assign('list', $list);
102-
$this->tpl->assign('cost_time', round(microtime(true) - $s, 3));
103-
$this->tpl->assign('count', $total);
104-
$this->tpl->assign('pager', $pager->render());
105-
$this->tpl->display("wiki/noframe/search.html");
125+
$pager->page_tpl = $page_tpl;
126+
$this->assign('list', $list);
127+
$this->assign('cost_time', round(microtime(true) - $s, 3));
128+
$this->assign('count', $total);
129+
$this->assign('link_tpl', $link_tpl);
130+
$this->assign('pager', $pager->render());
131+
$this->display("wiki/noframe/search.php");
106132
}
107133

108134
function main()
@@ -313,6 +339,17 @@ function edit()
313339
'title' => $cont->title,
314340
'version' => intval($cont->version),
315341
));
342+
//更新索引
343+
if ($this->project_id == 1)
344+
{
345+
$index = new App\Indexer('wiki');
346+
$index->update([
347+
'pid' => $node->id,
348+
'subject' => $cont->title,
349+
'message' => $cont->content,
350+
'chrono' => time()
351+
]);
352+
}
316353
//增加版本号
317354
$cont->version = intval($cont->version) + 1;
318355
}

server/apps/controllers/Wiki_admin.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ function revert()
288288
$node->update_uid = $this->uid;
289289
$node->save();
290290
$cont->save();
291+
if ($this->project_id == 1)
292+
{
293+
$index = new App\Indexer('wiki');
294+
$index->update([
295+
'pid' => $node->id,
296+
'subject' => $cont->title,
297+
'message' => $cont->content,
298+
'chrono' => $cont->uptime,
299+
]);
300+
}
291301
App\Content::clearCache($wiki_id);
292302
$this->http->redirect('/wiki_admin/main/?id='.$wiki_id);
293303
}
@@ -538,7 +548,7 @@ function create()
538548
//同级页面
539549
else
540550
{
541-
$in['pid'] = empty($cnode)?0:$cnode['pid'];
551+
$in['pid'] = empty($cnode) ? 0 : $cnode['pid'];
542552
}
543553
if(!empty($_POST['link']))
544554
{
@@ -566,6 +576,17 @@ function create()
566576
$in2['close_edit'] = intval($_POST['close_edit']);
567577
$_cont->put($in2);
568578

579+
if ($in['project_id'] == 1)
580+
{
581+
$index = new App\Indexer('wiki');
582+
$index->add([
583+
'pid' => $node_id,
584+
'subject' => $in2['title'],
585+
'message' => $in2['content'],
586+
'chrono' => time()
587+
]);
588+
}
589+
569590
//写入历史记录
570591
$_historyTable = table('wiki_history');
571592
$_historyTable->put(array(
@@ -657,12 +678,20 @@ private function ifDeny()
657678

658679
function delete()
659680
{
660-
if(empty($_GET['id'])) return "error: requirer miki_page id";
681+
if (empty($_GET['id']))
682+
{
683+
return "error: requirer miki_page id";
684+
}
661685
$_cont = model('WikiContent');
662686
$_tree = model('WikiTree');
663687
$id = (int)$_GET['id'];
664688
$_cont->del($id);
665689
$_tree->del($id);
690+
if ($this->project_id == 1)
691+
{
692+
$index = new App\Indexer('wiki');
693+
$index->del($id);
694+
}
666695
$this->reflushPage('删除成功');
667696
}
668697

@@ -729,6 +758,16 @@ function modify()
729758
//更新内容和标题
730759
if (!($_POST['content'] === $cont->content and trim($_POST['title']) == $cont->title))
731760
{
761+
if ($this->project_id == 1)
762+
{
763+
$index = new App\Indexer('wiki');
764+
$index->update([
765+
'pid' => $node->id,
766+
'subject' => $cont->title,
767+
'message' => $cont->content,
768+
'chrono' => time()
769+
]);
770+
}
732771
//写入历史记录
733772
$_historyTable = table('wiki_history');
734773
$_historyTable->put(array(

server/apps/templates/wiki/noframe/search.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
<script src="/static/js/jquery.js"></script>
2121
<script src="/static/js/dtree.js"></script>
2222
<title>搜索结果页-{{$project.name}}-Swoole文档中心</title>
23+
<style>
24+
#search_result h2 {
25+
font-size: 18px;
26+
}
27+
#search_result div {
28+
font-size: 14px;
29+
}
30+
</style>
2331
</head>
2432
<body>
2533

0 commit comments

Comments
 (0)