眾所周知,在MYSQL數據庫中,如果你在百萬級別數據庫中使用 like 的話那你一定在那罵娘,coreseek是一個針對於中文檢索方案的一種全文檢索技術,基於sphinx開發的。但是在coreseek中不但支持了mysql數據源,還支持了python、xml、mssql、odbc。而且提供了很多語言PHP、C#、JAVA、python等豐富API接口。在中文全文搜索引擎中,基本沒有什么能有coreseek匹敵的(是我太深入了嘛-^-),在千萬條數據測試下,coreseek生成索引后全文檢索的時間不會超過0.5s,這個速度是非常可觀的。
在論壇如discuz(discuz后台可配置coreseek)、電商網站(shopex)都是支持配置全文檢索的。下面我們就來全程安裝一下coreseek全文檢索引擎。我使用的系統是 centos5.5。
1. 安裝必要的編譯工作支持
安裝coreseek之前需要安裝這些工具,當然使用yum安裝你的機子需要先保證已經聯網
yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel
2. 下載coreseek和編譯安裝
$ wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz $ tar xzvf coreseek-3.2.14.tar.gz $ cd coreseek-3.2.14 ##安裝mmseg中文分詞 $ cd mmseg-3.2.14 $ ./bootstrap #輸出的warning信息可以忽略,如果出現error則需要解決 $ ./configure --prefix=/usr/local/mmseg3 $ make && make install $ cd .. ##安裝coreseek $ cd csft-3.2.14 $ sh buildconf.sh #輸出的warning信息可以忽略,如果出現error則需要解決 $ ./configure --prefix=/usr/local/coreseek --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql ##如果提示mysql問題,可以查看MySQL數據源安裝說明 $ make && make install $ cd ..
3. 配置MYSQL數據源
vi /usr/local/coreseek/etc/csft.conf
摘錄我的MYSQL數據源配置文件
source src1{ type = mysql sql_host = localhost sql_user = root sql_pass = 123456 sql_db = test sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT id,group_id,author_id,UNIX_TIMESTAMP(date_added) as date_added,title,content FROM documents sql_attr_uint = author_id sql_attr_uint = group_id sql_attr_timestamp = date_added sql_query_info_pre = SET NAMES utf8 } index src1{ source = src1 path = /usr/local/coreseek/var/data/src1/ docinfo = extern mlock =0 morphology = none min_word_len =1 html_strip =0 charset_type = zh_cn.utf-8 charset_dictpath = /usr/local/mmseg3/etc/ #charset-table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F #ngram_len = 1 #ngram_chars = U+3000..U+2FA1F } searchd{ listen = 9312 read_timeout =5 max_children = 30 max_matches = 1000 seamless_rotate = 0 preopen_indexes = 0 unlink_old = 1 pid_file = /usr/local/coreseek/var/log/searchd_rtindex.pid log = /usr/local/coreseek/var/log/searchd_rindex.log query_log = /usr/local/coreseek/var/log/query_rtindex.log }
a. source是配置數據源,按照提示輸入MYSQL的主機、帳號、密碼和數據庫即可,我的MYSQL就安裝在本機上(MYSQL的安裝可自行百度)
b. sql_query_pre是在執行查詢之前執行的SQL語句。(注意:在coreseek只能識別utf8字符集編碼,所以我們要執行轉換一下)
c. sql_query是要查詢進行索引的SQL語句,sql_attr_unit和sql_attr_timestamp是設置屬性的,屬性在全文檢索中可以用來設置過濾和排序。
d. index和source應該是成對出現,index就是配置索引的功能(我們還可以配置多個索引 主索引+增量索引的功能)
e. searchd是常駐進程的全文檢索服務,默認監控本機的9312端口
f. charset_type和charset_dictpath是中文分詞配置
4. 創建索引和測試全文檢索
上面我們配置了MYSQL數據源,我們要做到是test庫中的documents表進行全文檢索,為了測試全文檢索的效率,我批量插入了該表30W的數據,以下我們就對該表進行索引測試和全文檢索測試。
a. 測試indexer索引
$ cd /usr/local/coreseek/ $ /usr/local/coreseek/bin/indexer -c etc/csft.conf ##以下為正常情況下的提示信息: Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2010, Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'... total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
b. 創建sphinx索引文件(30W的數據我創建花了2分鍾左右,索引文件大概200MB)
$ /usr/local/coreseek/bin/indexer -c etc/csft.conf --all ##以下為正常索引全部數據時的提示信息:(csft-4.0版類似) Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2010, Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'... indexing index 'xml'... collected 3 docs, 0.0 MB sorted 0.0 Mhits, 100.0% done total 3 docs, 7585 bytes total 0.075 sec, 101043 bytes/sec, 39.96 docs/sec total 2 reads, 0.000 sec, 5.6 kb/call avg, 0.0 msec/call avg total 7 writes, 0.000 sec, 3.9 kb/call avg, 0.0 msec/call avg
c. 使用search程序測試全文檢索(注意是search不是searchd)
$ /usr/local/coreseek/bin/search -c etc/csft.conf ##以下為正常測試搜索時的提示信息:(csft-4.0版類似) Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2010, Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'... index 'xml': query '': returned 3 matches of 3 total in 0.093 sec displaying matches: 1. document=1, weight=1, published=Thu Apr 1 22:20:07 2010, author_id=1 2. document=2, weight=1, published=Thu Apr 1 23:25:48 2010, author_id=1 3. document=3, weight=1, published=Thu Apr 1 12:01:00 2010, author_id=2 words: $ /usr/local/coreseek/bin/search -c etc/csft.conf -a Twittter和Opera都提供了搜索服務 ##以下為正常測試搜索關鍵詞時的提示信息:(csft-4.0版類似) Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2010, Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'... index 'xml': query 'Twittter和Opera都提供了搜索服務 ': returned 3 matches of 3 total in 0.038 sec displaying matches: 1. document=3, weight=24, published=Thu Apr 1 12:01:00 2010, author_id=2 2. document=1, weight=4, published=Thu Apr 1 22:20:07 2010, author_id=1 3. document=2, weight=3, published=Thu Apr 1 23:25:48 2010, author_id=1 words: 1. 'twittter': 1 documents, 3 hits 2. '和': 3 documents, 15 hits 3. 'opera': 1 documents, 25 hits 4. '都': 2 documents, 4 hits 5. '提供': 0 documents, 0 hits 6. '了': 3 documents, 18 hits 7. '搜索': 2 documents, 5 hits 8. '服務': 1 documents, 1 hits
d. 開啟searchd服務
開啟searchd服務,使能用API程序進行調用(注意:你需要配置你的防火牆或者關閉,和關閉selinux)
- service iptables stop #暫時關閉防火牆
- setenforce 0 # 暫時關閉selinux
$ /usr/local/coreseek/bin/searchd -c etc/csft.conf ##以下為正常開啟搜索服務時的提示信息:(csft-4.0版類似) Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)] Copyright (c) 2007-2010, Beijing Choice Software Technologies Inc (http://www.coreseek.com) using config file 'etc/csft.conf'... listening on all interfaces, port=9312 ##如要停止搜索服務,請使用/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop ##如要已啟動服務,要更新索引,請使用/usr/local/coreseek/bin/indexer -c etc/csft.conf --all --rotate
5. 使用PHP API調用coreseek
復制api/sphinxapi.php文件到你的項目,以下是我的一個PHP測試腳本程序:
<?php error_reporting(E_ALL ^ E_NOTICE); header("Content-type: text/html; charset=utf-8"); require ( "sphinxapi.php" ); define(INDEX_SRC1, 'src1'); $cl = new SphinxClient(); $cl->SetServer("10.2.4.15",9312); $cl->SetArrayResult(true); $keyword = "媒體"; $result = $cl->Query($keyword, INDEX_SRC1); //處理$result $matches = isset($result['matches']) ? $result['matches'] : ''; if(is_array($matches)){ foreach ($matches as $v){ $ids[] = $v['id']; } }else{ print("<pre>"); print $cl->GetLastError(); print $cl->GetLastWarning(); //print "沒找到了親~"; print("<pre>"); return; } $ids = implode(',', $ids); //拿着id ,拉庫 $link = mysql_connect('localhost','root','123456') or die('mysql link fail!'); mysql_select_db('test',$link); mysql_query("SET NAMES UTF8"); $sql = "select * from documents where id in({$ids})"; $result = mysql_query($sql,$link); $data = array(); while ($row = mysql_fetch_assoc($result)) { $data[] = $row; } mysql_close($link); // 關鍵字高亮 $p_titles = array(); $p_contents = array(); $build_opts= array(//查詢結果集設置 'before_match'=> '<font color="red">', 'after_match'=> '</font>', 'limit'=> '130', //'exact_phrase'=> true, 'single_passage'=> true, 'chunk_separator' => '', ); foreach($data as $key=>$value) { $p_titles[$key] = $value['title']; $p_contents[$key] = $value['content']; } $p_titles = $cl->BuildExcerpts($p_titles, INDEX_SRC1, $keyword, $build_opts); $p_contents = $cl->BuildExcerpts($p_contents, INDEX_SRC1, $keyword, $build_opts); foreach($data as $key=>$post) { $data[$key]['title'] = $p_titles[$key]; $data[$key]['content'] = $p_contents[$key]; } var_dump($data); ?>
6. 設置coreseek自動啟動
在/etc/rc.d/rc.local文件中加入以下行
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf
程序啟動你的服務器,使用命令 ps -aux查看是否已經啟動成功了。
7. 中文分詞mmseg的一些用法
官方文檔:http://www.coreseek.cn/opensource/mmseg/#mmseg_ini
LibMMSeg 是Coreseek.com為 Sphinx 全文搜索引擎設計的中文分詞軟件包,其在GPL協議下發行的中文分詞法,采用Chih-Hao Tsai的MMSEG算法
詞典的構造
mmseg -u unigram.txt
該命令執行后,將會在unigram.txt所在目錄中產生一個名為unigram.txt.uni的文件,將該文件改名為uni.lib,完成詞典的構造。需要注意的是,unigram.txt需要預先准備,並且編碼格式必須為UTF-8編碼
mmseg -d <dict_dir> tobe_segment.txt
其中,命令使用‘-d’開關指定詞庫文件所在的位置,參數dict_dir為詞庫文件(uni.lib )所在的目錄;tobe_segment.txt 為待切分的文本文件,必須為UTF-8編碼。如果一切正確,mmseg會將切分結果以及所花費的時間顯示到標准輸出上。
對特殊短語的支持
由於LibMMSeg是為Sphinx全文搜索引擎設計的,因此其內置了部分搜索引擎切分算法的特性,主要表現在對特殊短語的支持上。
在搜索引擎中,需要處理C++時,如果分詞器中沒有詞組C++,則將被切分為C/x +/x +/x,在進一步的檢索中,可能每個詞會由於出現的過於頻繁而被過濾掉,導致搜索的結果與 C++相關度不高不說,也嚴重影響的全文搜索的速度。在LibMMSeg中,內置對特殊短語的支持。
其輸入文件格式如下
// test commit
.net => dotnet
c# => csharp
c++ => cplusplus
其中左側是待支持的特殊短語,右側是左側的特殊短語需要被轉換為的短語。這一轉換在分詞前進行。
可以在行的開頭加入'//'作為注釋符號,發現符號'//'后,整行將被忽略。
特殊短語詞庫構造命令:
mmseg -b exceptions.txt
其中, 開關'-b'指示mmseg是要構造特殊短語詞庫;exceptions.txt是用戶編輯的特殊短語轉換規則。
該命令執行后,將在當前目錄下產生一個名為"synonyms.dat"的文件,將該文件放在"uni.lib"同一目錄下,分詞系統將自動啟動特殊短語轉換功能。
分詞
mmseg -d tobe_segment.txt
其中,命令使用‘-d’開關指定詞庫文件所在的位置,參數dict_dir為詞庫文件(uni.lib )所在的目錄;tobe_segment.txt 為待切分的文本文件,必須為UTF-8編碼。如果一切正確, mmseg會將切分結果以及所花費的時間顯示到標准輸出上。
注意:分詞中的相似詞組,比如要在詞庫里有存在的配置才會有效!
總結:當然以上都是一些安裝的菜鳥級別應用,coreseek是一個強大系統官方也有很全的文檔,還有查詢表達式、增量索引、實時索引、中文詞庫、分布式索引等高級的應用。