Sphinx是開源的搜索引擎,它支持英文的全文檢索。所以如果單獨搭建Sphinx,你就已經可以使用全文索引了。但是往往我們要求的是中文索引,怎麼做呢?國人提供了一個可供企業使用的,基於Sphinx的中文全文檢索引擎。也就是說Coreseek實際上的內核還是Sphinx。
sphinx可以通過設置爲“一元切分模式”來支持搜索中文
在實際使用中,搜索非中文的話,sphinx比coreseek要快;搜索短中文字符串的話,開啓了“一元切分模式”的sphinx比coreseek要快;只有在搜索長中文字串時,coreseek的分詞優勢才能顯現,比sphinx要快
根據你的應用場景來選擇用哪個,如果是索引英文、數字、字符較多的數據,就用源生sphinx;如果是索引中文非常多非常長的數據,還是用coreseek吧。Coreseek是基於Sphinx開發的一款軟件,對Sphinx做了一些改動,在中文方面支持得比Sphinx好
Sphinx 詳細介紹可參考sphinx 中文站
本文以 wget http://files.opstool.com/man/coreseek-4.1-beta.tar.gz 爲安裝版本
遇到Makefile.in錯誤執行以下操作
aclocal
libtoolize –force
automake –add-missing
autoconf
autoheader
make clean
第一步:安裝升級autoconf
因爲coreseek需要autoconf 2.64以上版本,因此需要升級autoconf,不然會報錯
安裝方法如下:
yum -y install glibc-common libtool autoconf automake mysql-devel expat-devel
- 1
第二步:安裝mmseg(coreseek所使用的詞典)安裝的過程一定要注意是否是在當前目錄
tar -zxvf coreseek-4.1-beta.tar.gz
cd coreseek-4.1-beta
cd mmseg-4.1-beta/
./bootstrap #輸出的warning信息可以忽略,如果出現error則需要解決
./configure --prefix=/usr/local/mmseg3
make
make install
# 檢測是否安裝完成
mmseg
提示:-bash: mmseg: command not found
# 問題解決方法
ln -s /usr/local/mmseg3/bin/mmseg /bin/mmseg
# 再測試下
mmseg
# 顯示以下內容表示安裝成功
Coreseek COS(tm) MM Segment 1.0
Copyright By Coreseek.com All Right Reserved.
Usage: mmseg <option> <file>
-u <unidict> Unigram Dictionary
-r Combine with -u, used a plain text build Unigram Dictionary, default Off
-b <Synonyms> Synonyms Dictionary
-t <thesaurus> Thesaurus Dictionary
-h print this help and exit
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
第三步:安裝csft-4.1-beta
cd csft-4.1-beta/
sh buildconf.sh #輸出的warning信息可以忽略,如果出現error則需要解決
./configure \
--prefix=/usr/local/sphinx \
--without-unixodbc \
--with-mmseg \
--with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ \
--with-mmseg-libs=/usr/local/mmseg3/lib/ \
--with-mysql=/usr/local/mysql/ \
make
make install
測試mmseg分詞和coreseek搜索
cd testpack
cat var/test/test.xml #此時應該正確顯示中文
/usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml
/usr/local/sphinx/bin/indexer -c etc/csft.conf --all
/usr/local/sphinx/bin/search -c etc/csft.conf 網絡搜索
#此時正確的應該返回
words:
1. '網絡': 1 documents, 1 hits
2. '搜索': 2 documents, 5 hits
第四步:配置mmseg中文分詞
目前這裏不用操作,安裝時已經幫我配置好了
cd /usr/local/mmseg3/
# 生成unigram.txt.uni
./bin/mmseg -u /usr/local/mmseg/etc/unigram.txt
vim etc/mmseg.ini
[mmseg]
merge_number_and_ascii=0; ;合併英文和數字 abc123/x
number_and_ascii_joint=; ;定義可以連接英文和數字的字符
compress_space=1; ;暫不支持
seperate_number_ascii=0; ;將字母和數字打散
# 複製到sphinx/dict目錄
cp etc/mmseg.ini /usr/local/sphinx/dict/mmseg.ini
# 複製到sphinx/dict目錄
cp etc/unigram.txt /usr/local/sphinx/dict/uni.lib
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
第五步:配置sphinx
# 創建配置文件
mkdir -p /usr/local/sphinx/etc/conf.d/
vim /usr/local/sphinx/etc/conf.d/sphinx.conf
- 1
- 2
- 3
- 4
sphinx.conf 官方最簡配置文件內容
配置的數據庫文件:/usr/local/sphinx/etc/example.sql
source src1
{
type = mysql
sql_host = localhost
sql_user = test
sql_pass =
sql_db = test
sql_port = 3306 # optional, default is 3306
sql_query = \
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
FROM documents
sql_attr_uint = group_id #搜索後顯示的int字段
sql_attr_timestamp = date_added #搜索後顯示的時間戳字段
sql_query_info = SELECT * FROM documents WHERE id=$id
}
index test1
{
source = src1
path = /usr/local/sphinx/var/data/test1
docinfo = extern
charset_type = sbcs
}
index testrt # 索引的配置
{
type = rt
rt_mem_limit = 32M
path = /usr/local/sphinx/var/data/testrt
charset_type = utf-8
rt_field = title
rt_field = content
rt_attr_uint = gid
}
indexer 索引的內存
{
mem_limit = 32M
}
searchd # 守護進程
{
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
read_timeout = 5
max_children = 30
pid_file = /usr/local/sphinx/var/log/searchd.pid
max_matches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads # for RT to work
}
建立sphinx 索引(若日常更新sphinx數據,只需執行以下1、2、3步即可,可寫入sh文件執行)
# 結束所有索引
#pkill -9 search
1、#停止正在運行的searchd
/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf --stop
2、# 創建索引
# /usr/local/sphinx/bin/indexer --c --config /usr/local/sphinx/etc/conf.d/sphinx.conf --all
#如果只想對某個數據源進行索引,則可以這樣:
#/usr/local/sphinx/bin/indexer --c --config /usr/local/sphinx/etc/conf.d/sphinx.conf 索引名稱
3、# 啓動索引
# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf
4、# 合併索引跟增量索引
# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/conf.d/sphinx.conf --merge baby_idx baby_merge --rotate
# 錯誤解決
# 解決:複製Mysql客戶端文件到所有用戶可以放的目錄去
cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
# 64位系統需要在創建一個軟連接
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
服務器所用MySQL在當時編譯時並沒有編譯Sphinx擴展,而重新編譯mysql並加入Sphinx暫時又無法實現(項目用到了多臺服務器,在不影響現有業務的情況下不可能去重新編譯MySQL的),所以採用的是程序通過API來外部調用Sphinx.Sphinx自帶的API有PHP,Python,Ruby,Java等衆多版本,所以基本也夠用了,本人使用的編程語言是php所以下文的條用示例採用的是PHP版的API.
- 1
以下步驟不需要執行,原因如上:
第六步:安裝sphinxclient
php模塊依賴於libsphinxclient包
# 進入sphinx 源碼包的api目錄
cd coreseek-4.1/csft-4.1/api/libsphinxclient
./configure --prefix=/usr/local/libsphinxclient
#若報一下錯誤
config.status: error: cannot find input file: Makefile.in #報錯configure失敗
#處理configure報錯,運行下列指令再次編譯:
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
//重新configure編譯
./configure --prefix=/usr/local/libsphinxclient
make
make install
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
第七步:安裝php的Sphinx擴展
# PHP7以前的版本使用下面擴展
wget http://pecl.php.net/get/sphinx-1.3.0.tgz
# PHP7 使用下面的擴展
wget http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=9a3d08c67af0cad216aa0d38d39be71362667738;sf=tgz
tar -zxvf sphinx-1.3.0.tgz
cd sphinx-1.3.0
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient/
make && make install
# 安裝好後,在安裝目錄下etc目錄下,有份測試數據和配置的樣本
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
第八步:配置PHP支持Sphinx
編輯php.ini文件
vim /usr/local/php/etc/php.ini
# 修改以下內容
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"
# 增加擴展到php
extension = sphinx.so
# 重新啓動php-fpm
/etc/init.d/php-fpm restart
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
查看Sphinx是否安裝成功
新建PHP文件
vim index.php
# 寫入一下內容:
<?php
echo phpinfo();
- 1
- 2
- 3
- 4
- 5
瀏覽器輸入地址訪問
看到sphinx表示擴展安裝成功
第九步(簡易版):php使用sphinx
$s = new SphinxClient;
$a = $s->setServer("127.0.0.1", 9312);
$res = $s->query("體驗版搶先試用",'test1');
查詢結果:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
)
[attrs] => Array
(
[lv] => 1
[fansnum] => 1
[visible] => 1
[lon] => 5
[lat] => 5
[show_reg_client] => 1073741825
)
[total] => 0
[total_found] => 0
[time] => 0.002
[words] => Array
(
[體驗] => Array
(
[docs] => 114
[hits] => 114
)
[版] => Array
(
[docs] => 1279
[hits] => 1280
)
[搶先] => Array
(
[docs] => 0
[hits] => 0
)
[試用] => Array
(
[docs] => 4
[hits] => 4
)
)
)
Matches中就是查詢的結果了,但是彷彿不是我們想要的數據,比如titile,content字段的內容就沒有查詢出來,根據官方的說明是Sphinx並沒有連接到MySQL去取數據,只是根據它自己的索引內容進行計算,因此如果想用Sphinx提供的API去取得我們想要的數據,還必須以查詢的結果爲依據,再次查詢MySQL從而得到我們想要的數據。
參考:
sphinx中文站
博客參考