下面講講關(guān)于sphinx結(jié)合scws對(duì)MySQL實(shí)現(xiàn)全文檢索,文字的奧妙在于貼近主題相關(guān)。所以,閑話就不談了,我們直接看下文吧,相信看完sphinx結(jié)合scws對(duì)mysql實(shí)現(xiàn)全文檢索這篇文章你一定會(huì)有所受益。
創(chuàng)新互聯(lián)主營(yíng)若羌網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),若羌h5微信小程序開發(fā)搭建,若羌網(wǎng)站營(yíng)銷推廣歡迎若羌等地區(qū)企業(yè)咨詢
系統(tǒng)環(huán)境
主機(jī)名 | IP地址 | 相關(guān)服務(wù) | 版本 |
SQL | 172.169.18.128 | mysql5.6(主) | |
Sphinx | 172.169.18.210 | mysql5.6(從)php5.6 Apache2.4 | sphinx版本:2.2.10 sphinx插件:1.3.3 scws分詞版本:1.2.3 |
一、簡(jiǎn)介
1.1、 Sphinx是什么
參考地址:http://www.sphinxsearch.org/sphinx-tutorial
Sphinx是由俄羅斯人Andrew Aksyonoff開發(fā)的一個(gè)全文檢索引擎。意圖為其他應(yīng)用提供高速、低空間占用、高結(jié)果 相關(guān)度的全文搜索功能。Sphinx可以非常容易的與SQL數(shù)據(jù)庫(kù)和腳本語(yǔ)言集成。當(dāng)前系統(tǒng)內(nèi)置MySQL和PostgreSQL 數(shù)據(jù)庫(kù)數(shù)據(jù)源的支持,也支持從標(biāo)準(zhǔn)輸入讀取特定格式 的XML數(shù)據(jù)。
Sphinx創(chuàng)建索引的速度為:創(chuàng)建100萬(wàn)條記錄的索引只需3~4分鐘,創(chuàng)建1000萬(wàn)條記錄的索引可以在50分鐘內(nèi)完成,而只包含最新10萬(wàn)條記錄的增量索引,重建一次只需幾十秒。
1.2、 Sphinx的特性如下:
(1)高速的建立索引(在當(dāng)代CPU上,峰值性能可達(dá)到10 MB/秒);
(2)高性能的搜索(在2 – 4GB 的文本數(shù)據(jù)上,平均每次檢索響應(yīng)時(shí)間小于0.1秒);
(3)可處理海量數(shù)據(jù)(目前已知可以處理超過(guò)100 GB的文本數(shù)據(jù), 在單一CPU的系統(tǒng)上可處理100 M 文檔);
(4)提供了優(yōu)秀的相關(guān)度算法,基于短語(yǔ)相似度和統(tǒng)計(jì)(BM25)的復(fù)合Ranking方法;
(5) 支持分布式搜索;
(6)支持短語(yǔ)搜索
(7)提供文檔摘要生成
(8)可作為MySQL的存儲(chǔ)引擎提供搜索服務(wù);
(9)支持布爾、短語(yǔ)、詞語(yǔ)相似度等多種檢索模式;
(10)文檔支持多個(gè)全文檢索字段(最大不超過(guò)32個(gè));
(11)文檔支持多個(gè)額外的屬性信息(例如:分組信息,時(shí)間戳等);
(12)支持?jǐn)嘣~;
1.3、總結(jié)
優(yōu)點(diǎn):效率較高,具有較高的擴(kuò)展性
缺點(diǎn):不負(fù)責(zé)數(shù)據(jù)存儲(chǔ)
使用Sphinx搜索引擎對(duì)數(shù)據(jù)做索引,數(shù)據(jù)一次性加載進(jìn)來(lái),然后做了所以之后保存在內(nèi)存。這樣用戶進(jìn)行搜索的時(shí)候就只需要在Sphinx云服務(wù)器上檢索數(shù)據(jù)即可。而且,Sphinx沒有MySQL的伴隨機(jī)磁盤I/O的缺陷,性能更佳。
2.1、SCWS 是 Simple Chinese Word Segmentation 的首字母縮寫(即:簡(jiǎn)易中文分詞系統(tǒng))。
參考地址:http://www.xunsearch.com/scws/index.php
這是一套基于詞頻詞典的機(jī)械式中文分詞引擎,它能將一整段的中文文本基本正確地切分成詞。 詞是中文的最小語(yǔ)素單位,但在書寫時(shí)并不像英語(yǔ)會(huì)在詞之間用空格分開, 所以如何準(zhǔn)確并快速分詞一直是中文分詞的攻關(guān)難點(diǎn)。
2.2、特性
SCWS 采用純 C 語(yǔ)言開發(fā),不依賴任何外部庫(kù)函數(shù),可直接使用動(dòng)態(tài)鏈接庫(kù)嵌入應(yīng)用程序, 支持的中文編碼包括 GBK、UTF-8 等。此外還提供了 PHP 擴(kuò)展模塊, 可在 PHP 中快速而方便地使用分詞功能。
分詞算法上并無(wú)太多創(chuàng)新成分,采用的是自己采集的詞頻詞典,并輔以一定的專有名稱,人名,地名, 數(shù)字年代等規(guī)則識(shí)別來(lái)達(dá)到基本分詞,經(jīng)小范圍測(cè)試準(zhǔn)確率在 90% ~ 95% 之間, 基本上能滿足一些小型搜索引擎、關(guān)鍵字提取等場(chǎng)合運(yùn)用。首次雛形版本發(fā)布于 2005 年底。
二、環(huán)境準(zhǔn)備
1、暫時(shí)關(guān)閉防火墻
2、關(guān)閉seliunx
3、系統(tǒng)環(huán)境
centos7.4 mysql5.6 php5.6
三、搭建Sphinx服務(wù)
1、安裝依賴包
# yum -y install make gcc gcc-c++ libtool autoconf automake mysql-devel
2、安裝Sphinx
# yum install expat expat-devel
# wget -c http://sphinxsearch.com/files/sphinx-2.2.10-release.tar.gz
# tar -zxvf sphinx-2.2.10-release.tar.gz
# cd sphinx-2.2.10-release/
# ./configure --prefix=/usr/local/sphinx --with-mysql --with-libexpat --enable-id64
# make && make install
3、安裝libsphinxclient,php擴(kuò)展用到
# cd api/libsphinxclient/
# ./configure --prefix=/usr/local/sphinx/libsphinxclient
# make && make install
安裝完畢后查看一下/usr/local/sphinx下是否有 三個(gè)目錄 bin etc var,如有,則安裝無(wú)誤!
4、安裝Sphinx的PHP擴(kuò)展:我的是5.6需裝sphinx-1.3.3.tgz,如果是php5.4以下可sphinx-1.3.0.tgz,如果php7.0需要安裝sphinx-339e123.tar.gz(http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=339e123acb0ce7beb2d9d4f9094d6f8bcf15fb54;sf=tgz)
# wget -c http://pecl.php.net/get/sphinx-1.3.3.tgz
# tar zxvf sphinx-1.3.3.tgz
# cd sphinx-1.3.3/
# phpize
# ./configure --with-sphinx=/usr/local/sphinx/libsphinxclient/ --with-php-config=/usr/bin/php-config
# make && make install
# 成功后會(huì)提示:
Installing shared extensions: /usr/lib64/php/modules/
#修改php配置
# echo "[Sphinx]" >> /etc/php.ini
# echo "extension = sphinx.so" >> /etc/php.ini
#重啟httpd服務(wù)
# systemctl restart httpd.service
5、創(chuàng)建測(cè)試數(shù)據(jù)進(jìn)行測(cè)試(db:jiangjj)
CREATE TABLE IF NOT EXISTS `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='全文檢索測(cè)試的數(shù)據(jù)表' AUTO_INCREMENT=11 ;
INSERT INTO `items` (`id`, `title`, `content`, `created`) VALUES
(1, 'linux mysql集群安裝', 'MySQL Cluster 是MySQL 適合于分布式計(jì)算環(huán)境的高實(shí)用、可拓展、高性能、高冗余版本', '2016-09-07 00:00:00'),
(2, 'mysql主從復(fù)制', 'mysql主從備份(復(fù)制)的基本原理 mysql支持單向、異步復(fù)制,復(fù)制過(guò)程中一個(gè)云服務(wù)器充當(dāng)主云服務(wù)器,而一個(gè)或多個(gè)其它云服務(wù)器充當(dāng)從云服務(wù)器', '2016-09-06 00:00:00'),
(3, 'hello', 'can you search me?', '2016-09-05 00:00:00'),
(4, 'mysql', 'mysql is the best database?', '2016-09-03 00:00:00'),
(5, 'mysql索引', '關(guān)于MySQL索引的好處,如果正確合理設(shè)計(jì)并且使用索引的MySQL是一輛蘭博基尼的話,那么沒有設(shè)計(jì)和使用索引的MySQL就是一個(gè)人力三輪車', '2016-09-01 00:00:00'),
(6, '集群', '關(guān)于MySQL索引的好處,如果正確合理設(shè)計(jì)并且使用索引的MySQL是一輛蘭博基尼的話,那么沒有設(shè)計(jì)和使用索引的MySQL就是一個(gè)人力三輪車', '0000-00-00 00:00:00'),
(9, '復(fù)制原理', 'redis也有復(fù)制', '0000-00-00 00:00:00'),
(10, 'redis集群', '集群技術(shù)是構(gòu)建高性能網(wǎng)站架構(gòu)的重要手段,試想在網(wǎng)站承受高并發(fā)訪問(wèn)壓力的同時(shí),還需要從海量數(shù)據(jù)中查詢出滿足條件的數(shù)據(jù),并快速響應(yīng),我們必然想到的是將數(shù)據(jù)進(jìn)行切片,把數(shù)據(jù)根據(jù)某種規(guī)則放入多個(gè)不同的云服務(wù)器節(jié)點(diǎn),來(lái)降低單節(jié)點(diǎn)云服務(wù)器的壓力', '0000-00-00 00:00:00');
CREATE TABLE IF NOT EXISTS `sph_counter` (
`counter_id` int(11) NOT NULL,
`max_doc_id` int(11) NOT NULL,
PRIMARY KEY (`counter_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='增量索引標(biāo)示的計(jì)數(shù)表';
6、Sphinx配置:注意修改數(shù)據(jù)源配置信息。
配置地址:http://www.cnblogs.com/yjf512/p/3598332.html
以下采用"Main + Delta" ("主索引"+"增量索引")的索引策略,使用Sphinx自帶的一元分詞。
# vim /usr/local/sphinx/etc/sphinx.conf
source items {
type = mysql
sql_host = 172.169.18.128
sql_user = jiangjj
sql_pass = 123456
sql_db = jiangjj
sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type = OFF
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM items
sql_query_range = SELECT MIN(id), MAX(id) FROM items \
WHERE id<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1)
sql_range_step = 1000
sql_ranged_throttle = 1000
sql_query = SELECT id, title, content, created, 0 as deleted FROM items \
WHERE id<=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1) \
AND id >= $start AND id <= $end
sql_attr_timestamp = created
sql_attr_bool = deleted
}
source items_delta : items {
sql_query_pre = SET NAMES utf8
sql_query_range = SELECT MIN(id), MAX(id) FROM items \
WHERE id > (SELECT max_doc_id FROM sph_counter WHERE counter_id=1)
sql_query = SELECT id, title, content, created, 0 as deleted FROM items \
WHERE id>( SELECT max_doc_id FROM sph_counter WHERE counter_id=1 ) \
AND id >= $start AND id <= $end
sql_query_post_index = set @max_doc_id :=(SELECT max_doc_id FROM sph_counter WHERE counter_id=1)
sql_query_post_index = REPLACE INTO sph_counter SELECT 2, IF($maxid, $maxid, @max_doc_id)
}
#主索引
index items {
source = items
path = /usr/local/sphinx/var/data/items
docinfo = extern
morphology = none
min_word_len = 1
min_prefix_len = 0
html_strip = 1
html_remove_elements = style, script
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
charset_type = utf-8
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
preopen = 1
min_infix_len = 1
}
#增量索引
index items_delta : items {
source = items_delta
path = /usr/local/sphinx/var/data/items_delta
}
#分布式索引
index master {
type = distributed
local = items
local = items_delta
}
indexer {
mem_limit = 256M
}
searchd {
listen = 9312
listen = 9306:mysql41
log = /usr/local/sphinx/var/log/searchd.log
query_log = /usr/local/sphinx/var/log/query.log
# compat_sphinxql_magics = 0
attr_flush_period = 600
mva_updates_pool = 16M
read_timeout = 5
max_children = 0
dist_threads = 2
pid_file = /usr/local/sphinx/var/log/searchd.pid
# max_marches = 1000
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
workers = threads
binlog_path = /usr/local/sphinx/var/data
}
7、Sphinx創(chuàng)建索引
# cd /usr/local/sphinx/bin/
#第一次重建索引
# ./indexer -c /usr/local/sphinx/etc/sphinx.conf --all
#啟動(dòng)sphinx
# ./searchd -c /usr/local/sphinx/etc/sphinx.conf
#查看進(jìn)程
# ps -ef | grep searchd
#查看狀態(tài)
# ./searchd -c /usr/local/sphinx/etc/sphinx.conf --status
#關(guān)閉sphinx
# ./searchd -c /usr/local/sphinx/etc/sphinx.conf --stop
8、索引更新及使用說(shuō)明
"增量索引"每N分鐘更新一次.通常在每天晚上低負(fù)載的時(shí)進(jìn)行一次索引合并,同時(shí)重新建立"增量索引"。當(dāng)然"主索引"數(shù)據(jù)不多的話,也可以直接重新建立"主索引"。
API搜索的時(shí),同時(shí)使用"主索引"和"增量索引",這樣可以獲得準(zhǔn)實(shí)時(shí)的搜索數(shù)據(jù).本文的Sphinx配置將"主索引"和"增量索引"放到分布式索引master中,因此只需查詢分布式索引"master"即可獲得全部匹配數(shù)據(jù)(包括最新數(shù)據(jù))。
索引的更新與合并的操作可以放到cron job完成:
8.1、編輯
# crontab -e
*/1 * * * * /usr/local/sphinx/shell/index_update.sh
0 3 * * * /usr/local/sphinx/shell/merge_index.sh
//查看
# crontab -l
8.2、腳本如下
#更新腳本
# vim /usr/local/sphinx/shell/index_update.sh
#!/bin/sh
/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate items_delta > /dev/null 2>&1
#合并腳本
[root@Sphinx shell]# vim merge_index.sh
#!/bin/bash
indexer=/usr/local/sphinx/bin/indexer
mysql=`which mysql`
#host=172.169.18.128
#mysql_user=jiangjj
#mysql_password=123456
QUERY="use jiangjj;select max_doc_id from sph_counter where counter_id = 2 limit 1;"
index_counter=$($mysql -h272.169.18.128 -ujiangjj -p123456 -sN -e "$QUERY")
#merge "main + delta" indexes
$indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate --merge items items_delta --merge-dst-range deleted 0 0 >> /usr/local/sphinx/var/index_merge.log 2>&1
if [ "$?" -eq 0 ]; then
##update sphinx counter
if [ ! -z $index_counter ]; then
$mysql -h272.169.18.128 -ujiangjj -p123456 -Djiangjj -e "REPLACE INTO sph_counter VALUES (1, '$index_counter')"
fi
##rebuild delta index to avoid confusion with main index
$indexer -c /usr/local/sphinx/etc/sphinx.conf --rotate items_delta >> /usr/local/sphinx/var/rebuild_deltaindex.log 2>&1
fi
#授權(quán)
# chmod u+x *.sh
測(cè)試沒問(wèn)題后繼續(xù)下一步操作
四、搭建scws(中文分詞)服務(wù)
下載地址:http://www.xunsearch.com/scws/download.php
1、scws下載安裝:注意擴(kuò)展的版本和php的版本
# wget -c http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
# tar jxvf scws-1.2.3.tar.bz2
# cd scws-1.2.3/
# ./configure --prefix=/usr/local/scws
# make && make install
2、scws的PHP擴(kuò)展安裝
# cd ./phpext/
# phpize
# ./configure
# make && make install
編譯完成狀態(tài)如下:
#修改php配置文件
[root@Sphinx ~]# echo "[scws]" >> /etc/php.ini
[root@Sphinx ~]# echo "extension = scws.so" >> /etc/php.ini
[root@Sphinx ~]# echo "scws.default.charset = utf-8" >> /etc/php.ini
[root@Sphinx ~]# echo "scws.default.fpath = /usr/local/scws/etc/" >> /etc/php.ini
3、詞庫(kù)安裝
下載地址:http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2
# wgethttp://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2
# tar jxvf scws-dict-chs-utf8.tar.bz2 -C /usr/local/sphinx/etc/
# chown -R apache:apache /usr/local/sphinx/etc/dict.utf8.xdb
五、php使用Sphinx+scws測(cè)試
1、在Sphinx源碼API中,有好幾種語(yǔ)言的API調(diào)用.其中有一個(gè)是sphinxapi.php。
新建一個(gè)Search.php文件,一個(gè)前端頁(yè)面index.php
代碼省略
訪問(wèn):
2、SphinxQL測(cè)試
要使用SphinxQL需要在Searchd的配置里面增加相應(yīng)的監(jiān)聽端口(參考上文配置)。
# mysql -h227.0.0.1 -P9306 -ujiangjj -p
mysql> show global variables;
mysql> desc items;
mysql> select * from master where match('mysql*') limit 10;
mysql> show meta;
對(duì)于以上sphinx結(jié)合scws對(duì)mysql實(shí)現(xiàn)全文檢索相關(guān)內(nèi)容,大家還有什么不明白的地方嗎?或者想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。
新聞標(biāo)題:sphinx結(jié)合scws對(duì)mysql實(shí)現(xiàn)全文檢索
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article10/gcisgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、響應(yīng)式網(wǎng)站、電子商務(wù)、ChatGPT、外貿(mào)建站、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)