---
title: "提高coreseek的搜索精度"
author: "phpseyo"
date: "2012-11-19T11:28:22Z"
tags: []
categories: ["行业应用"]
slug: "coreseek"
draft: false
lastmod: "2012-11-19T11:28:22Z"
id: 1729376
url: "https://phpseyo.iteye.com/blog/1729376"
---

由于coreseek带的mmseg默认词库只有1w多个字词，搜索出来的结果准确度不是很理想。花了半天找了下搜索引擎相关的资料，现在记录下一些提高搜索精度的方法和步骤：

1\. 基础词库要足够丰富，因为只有基础词库丰富了，才能谈权重设置。

2\. 增加相关业务领域的词汇，进一步提升词库量，如果不是做综合搜索，一般都只需要提高自身业务领域的词汇。

3\. 记录与收集用户搜索，把搜索记录高的作为新增词汇加入到词库中(前提是词库中不存在这个词)。

所以第一件事就是丰富基础词库，词库可以去搜狗拼音，QQ拼音官网下载。下下来后，统一把词汇进行处理，添加到一个词汇数据表 unigram，这个表只需包含id, word两列，id为自增字段，word为key字段，以保证这个词是独一无二的。

然后写个php脚本，把这个 unigram 表的数据转成 unigram.txt(mmseg要求的字典格式)，下面是这个脚本的简单步骤

&lt;?php

set\_time\_limit(0);

define(IN\_SITE, true);

require 'config.php';

require 'db\_class.php';

 

$db = new dbstuff();

$db-&gt;connect($dbhost, $dbuser, $dbpw, $dbname);

$max\_id = $db-&gt;result\_first('SELECT MAX(id) FROM unigram2');

 

$limit = 2000;

$start\_page = isset($\_GET\['id']) ? (int)$\_GET\['id'] : 0;

$start\_id = $limit * $start\_page;

if ($start\_id &gt; $max\_id) exit('ok');

$sql = "SELECT word FROM unigram2 limit {$start\_id}, {$limit}";

$query = $db-&gt;query($sql);

while ($arr = $db-&gt;fetch\_array($query)) {

$str .= $arr\['word'] . "\\t1\\r\\nx:1\\r\\n";

}

 

$file = './unigram3.txt';

$h = fopen($file, 'a');

fwrite($h, $str);

 

$start\_page++;

$url\_forward = 'import.php?id='.$start\_page;

echo '&lt;script&gt;window.location.href = "'.$url\_forward.';;&lt;/script&gt;';

?&gt;

 

得到新增的txt后，把这个txt里的所有内容复制到默认词典的底部。合并原有的的词库。

然后是用mmseg合并词典

 

cd /usr/local/mmseg3/

bin/mmseg -u etc/unigram.txt

 

执行上面的命令后，会生成 unigram.uni 文件，把它重命名为uni.lib(原来也有一个，可以对旧的改名)。

此时词库生成，但还需要重新生成一次索引，

 

cd /usr/local/coreseek

bin/indexer -c etc/xxx.conf --all

 

重启searchd

 

bin/searchd -c etc/xxx.conf --stop

bin/searchd -c etc/xxx.conf

 

至此，扩充词库并应用到索引的过程就完成了！