sphinx社區(qū)全文搜索平臺(tái)配置手冊(cè)_第1頁(yè)
sphinx社區(qū)全文搜索平臺(tái)配置手冊(cè)_第2頁(yè)
sphinx社區(qū)全文搜索平臺(tái)配置手冊(cè)_第3頁(yè)
sphinx社區(qū)全文搜索平臺(tái)配置手冊(cè)_第4頁(yè)
sphinx社區(qū)全文搜索平臺(tái)配置手冊(cè)_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、千萬(wàn)級(jí) Discuz! 數(shù)據(jù)全文檢索方案( Sphinx )前言:康盛創(chuàng)想的 Discuz! 從創(chuàng)立之初即以提高產(chǎn)品效率為突破口,隨著編譯模板,語(yǔ)法生成內(nèi) 核,數(shù)據(jù)緩存和自動(dòng)更新機(jī)制等獨(dú)創(chuàng)或獨(dú)有技術(shù)的應(yīng)用, 和堅(jiān)固的數(shù)據(jù)結(jié)構(gòu)及最少化數(shù)據(jù)庫(kù)查詢?cè)O(shè)計(jì), 使得 Discuz! 可以在極為繁忙的服務(wù)器環(huán)境下快速高效穩(wěn)定運(yùn)行。由于Discuz!產(chǎn)品依賴MySQL數(shù)據(jù)庫(kù)性能,在全文檢索方面如果僅僅依靠 MySQL的LIKE %關(guān) 鍵詞 % 語(yǔ)句無(wú)法取得理想的成績(jī)。本文闡述經(jīng)過(guò) Discuz! 生產(chǎn)環(huán)境考驗(yàn)的構(gòu)建在 Sphinx 基礎(chǔ)上的千 萬(wàn)級(jí) Discuz! 數(shù)據(jù)全文檢索解決方案。出自俄羅斯的開源全文搜

2、索引擎軟件 Sphinx 在單一索引達(dá)到 4 千萬(wàn)條記錄情況下的查詢速度仍 為 0.x 秒甚至 0.0x 秒級(jí)別。 Sphinx 創(chuàng)建索引的速度約五分鐘處理百萬(wàn)條記錄,對(duì)于每分鐘的增量索 引重建只需要幾十秒,每日的增量索引合并到主索引也只需一分鐘左右。此構(gòu)架基礎(chǔ)上的Discuz! 高負(fù)載站點(diǎn),已成功解決搜索速度慢、經(jīng)常鎖表、無(wú)法分布式搜索等問(wèn)題。千萬(wàn)級(jí) Discuz! 數(shù)據(jù)全文檢索方案( Sphinx )適用于繁忙的站點(diǎn)并且論壇訪問(wèn)者有大量的搜索需 求,本方案主要解決搜索緩慢問(wèn)題。本文環(huán)境以 CentOS 5 為基準(zhǔn)。初始化一次全部索引,按系統(tǒng)計(jì)劃任務(wù) crontab 定時(shí)方式,每 5 分鐘重

3、建一次增量索引 (增量索引不與主索引合并) ,每日凌晨 3:30 建立一次昨日比較的增量索引 (合 并到主索引中) 。主索引建立在磁盤目錄 /data/sphdata ,增量索引建立在內(nèi)存 /dev/shm/ 中避免大量的I/O 操作,由于帖子編輯限制,全部索引每?jī)蓚€(gè)月重建一次。目錄千萬(wàn)級(jí) Discuz! 數(shù)據(jù)全文檢索方案( Sphinx ) . 1Sphinx 快速介紹 2一、Sphinx 全文檢索方案構(gòu)架圖 3二、Sphinx 中文分詞 4三、Sphinx 安裝步驟 41. 安裝、升級(jí)所需的程序庫(kù) 42. 創(chuàng)建安裝文件夾并下載源代碼 43. 安裝 MMseg 中文分詞 44. 安裝 CSF

4、T (Sphinx 的 CoreSeek 修改版) 55. 編譯 Sphinx 過(guò)程可能出現(xiàn)的錯(cuò)誤 5a)無(wú)法找到 mysql 路徑 5b)無(wú)法找到 libiconv 5c)無(wú)法找到 libmysqlclient.so.15 5d)php 5.2.11 版本的 api BUG 5e)生成索引時(shí)容易發(fā)生磁盤空間不足寫入失敗的錯(cuò)誤 56. 安裝為 php 擴(kuò)展(可選) 67. 安裝 SphinxSE 存儲(chǔ)引擎(可選) 6四、Sphinx 配置 61. mmseg 中文分詞詞庫(kù) 6a)詞典格式 6b)詞庫(kù)生成方法 72. 創(chuàng)建 sphinx 數(shù)據(jù)目錄結(jié)構(gòu) 73. 創(chuàng)建 sphinx.conf 配置文

5、件 74. mmseg.ini 分詞配置文件 145. 建立 Sphinx 增量索引數(shù)據(jù)表 146. 書寫常用的 sphinx 控制命令到 sh 文件 147. 創(chuàng)建相應(yīng)的文件結(jié)構(gòu),初始化 Sphinx 的全部索引 178. 啟動(dòng) sphinx 179. 設(shè)置計(jì)劃任務(wù)項(xiàng) 1710. 設(shè)置開機(jī)啟動(dòng)項(xiàng) 17五、通過(guò)命令行測(cè)試搜索 17六、通過(guò) php api 調(diào)用 Sphinx搜索 18七、通過(guò) MySQL 的 SphinxSE存儲(chǔ)引擎調(diào)用 Sphinx 搜索(可選) 19八、Sphinx 數(shù)據(jù)占用量統(tǒng)計(jì) 19Sphinx 快速介紹Sphinx 是獨(dú)立的搜索服務(wù)端,不依賴 MySQL ,當(dāng) Sph

6、inx 和 MySQL 結(jié)合部署時(shí), Sphinx 的數(shù) 據(jù)來(lái)源為 MySQL 。服務(wù)器安裝 Sphinx ,由 sphinx.conf 配置文件指定 Sphinx 的數(shù)據(jù)源,如何讀取 MySQL 的數(shù)據(jù)內(nèi)容, 設(shè)置 Sphinx 對(duì) MySQL 數(shù)據(jù)庫(kù)的哪個(gè)表哪些字段建立索引, 索引的 返回?cái)?shù)據(jù)必 須是數(shù)值型 。例如一個(gè)配置段設(shè)置 Sphinx 對(duì) cdb_threads 表的 subject 和 author 字段建立索引,搜索結(jié)果可 以返回 tid 和 uid 。另一個(gè)配置段設(shè)置Sphinx 對(duì) cdb_posts 表的 subject 和 message 字段索引, 搜索結(jié)果返回 ti

7、d 和 pid 。Sphinx 建立的索引只是查詢 MySQL 數(shù)據(jù)庫(kù)獲取數(shù)據(jù)內(nèi)容,將索引存放在 Sphinx 的數(shù)據(jù)文件和 內(nèi)存中, 一般不對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行修改操作。因?yàn)?Sphinx 獨(dú)立運(yùn)行,使用 SELECT * FROM cdb_threads WHERE subject LIKE '% 關(guān)鍵詞 %' LIMIT 0, 10; 類似的 SQL 語(yǔ)句將 無(wú)法利用 Sphinx 搜索 ,即使服務(wù)器端已經(jīng)部署 Sphinx 也不會(huì)對(duì) 該查詢語(yǔ)句有任何優(yōu)化作用。正確使用 Sphinx 搜索數(shù)據(jù)的操作方式主要有三種:1 、命令行的 search 工具 : /usr/l

8、ocal/webserver/sphi nx/bi n/search- threads test2、php 的 api 接口查詢 :原理是直接用 fsockopen 連接端口,傳遞數(shù)據(jù)取得返回結(jié)果。 Sphinx 官方已經(jīng)提供 php 的 api 接口,可以 include api 查詢( 本方案以該查詢方法為主 ),也可以將其源 代碼編譯成 php 擴(kuò)展而無(wú)需 include 。3、在 mysql 中將 Sphinx 安裝為 SphinxSE 存儲(chǔ)引擎,通過(guò) SphinxSE 方式調(diào)用 Sphinx 。因 Sphinx 搜索結(jié)果只返回 INT 類型數(shù)據(jù),部署 Sphinx 搜索的核心是由搜索

9、入口( search.php ) 提交的關(guān)鍵詞到 Sphinx 中搜索, Sphinx 返回對(duì)應(yīng)的 tid 、pid 等信息, 再依據(jù) tid 、pid 到 cdb_threads 或者 cdb_posts 中搜索,得到結(jié)果集展示在頁(yè)面上。Sphinx 的搜索速度非常快,而 tid/pid 都是主鍵查詢,總體來(lái)說(shuō)雖然用了多次查詢,但是速度仍然非??臁phinx全文檢索方案構(gòu)架圖干萬(wàn)級(jí)Discuzi數(shù)據(jù)多服務(wù)器全文檢索方案(Sphinx )squidNGSklXNGIMXNG;HXPh數(shù)據(jù)伺涉IrinoDB 寫人My ISAM表讀php前端査詢?nèi)肟诮└?!匹配椅珠?詢語(yǔ)旬Ephinst匪或者P

10、HF a pi)QM!前端反向4弋理,員載均衝監(jiān)聽簫12端口計(jì)勁Jtt務(wù) 讀入數(shù)據(jù)1rsphinx詢結(jié)果聯(lián) 搽呵SQL數(shù)艇回3 / 20干萬(wàn)級(jí)Discuz!數(shù)據(jù)單服務(wù)器全文檢索方案(Sphinx )NGiMX©Sphinx監(jiān)聽3J1編口二、Sphinx中文分詞打過(guò)Sphinx官方補(bǔ)丁的Sphinx可以支持utf8、gbk等編碼的漢字,按單個(gè)漢字切分,漢語(yǔ)搜索 中應(yīng)以詞為依據(jù),獨(dú)立存在的單個(gè)漢字搜索幾乎沒(méi)有意義。本文介紹的構(gòu)架方案以CoreSeek的CSFT修改版為主,由libmmseg對(duì)搜索詞予以分詞切分,Sphinx按切分詞建立相應(yīng)索引從而建立以詞和短 語(yǔ)為基準(zhǔn)的搜索。三、Sphi

11、nx安裝步驟1. 安裝、升級(jí)所需的程序庫(kù)yum -y install gcc gcc-c+ autoconf python python-devel libiconv2. 創(chuàng)建安裝文件夾并下載源代碼mkdir -p /data/softwarecd /data/softwarewget http:/www.coreseek.en/uploads/csft/3.1/Source/mmseg-3.1.tar.gzwget http:/www.coreseek.en/uploads/csft/3.1/Source/csft-3.1.tar.gz3. 安裝MMseg中文分詞tar -xzf mmseg

12、-3.1.tar.gzcd mmseg-3.1./configure -prefix=/usr/local/webserver/mmsegmakemake installcd .4. 安裝 CSFT (Sphinx 的 CoreSeek修改版)tar -xzf csft-3.1.tar.gzcd csft-3.1./configure-prefix=/usr/local/webserver/csft-with-mmseg=/usr/local/webserver/mmseg/bin/mmseg-with-mmseg-includes=/usr/local/webserver/mmseg/inc

13、lude/mmseg/-with-mmseg-libs=/usr/local/webserver/mmseg/lib/ -with-mysql=/usr/local/webserver/mysql/makemake installcd .5. 編譯Sphinx過(guò)程可能出現(xiàn)的錯(cuò)誤a) 無(wú)法找到mysql路徑確認(rèn) -with-mysql=之后的路徑正確,是當(dāng)前 mysql所在路徑。如果 mysql是通過(guò)rpm或者yum 方式安裝,直接-with-mysql使用默認(rèn)路徑即可。失敗嘗試指定-with-mysql-i ncludes= 路徑-with-mysql-libs=路徑b) 無(wú)法找到libico

14、nvlibiconv通過(guò)yum 方式安裝的,可以直接識(shí)別。源代碼編譯安裝的,需要建立在/usr/lib和/usr/local/lib 下建立相應(yīng)的軟鏈。In -s源文件 目標(biāo)文件?;蛘邔?libiconv的目錄加入到ld.so.conf(例如 libiconv 安裝在 /usr/local/lib/ 目錄中)echo /usr/local/lib/ >> /etc/ld.so.confldconfig若仍提示libiconv無(wú)法找到,需要修改 vi src/Makefile 文件,找 LIBS =開頭的行,將LIBS = -lm -lz -lexpat -L/usr/local/l

15、ib -pthread修改成LIBS = -lm -lz -lexpat -liconv -L/usr/local/lib -lpthreadc)無(wú)法找到 libmysqlclient.so.15將 libmysqlclient.so.15建立軟鏈到 /usr/local/lib 和/usr/lib 目錄下,或者加入到ld.so.confecho /usr/local/webserver/mysql/lib/mysql/ >> /etc/ld.so.confldconfigd) php 5.2.11 版本的 api BUG當(dāng)站點(diǎn)的php版本為5.2.11 ,并且通過(guò)php api方

16、式獲取Sphinx數(shù)據(jù),數(shù)據(jù)超過(guò)1000條的情況 下有BUG,無(wú)法解析正確的數(shù)據(jù)包。遇到此情況必須重新編譯php版本,目前已知php 5.2.10是與Sphinx API結(jié)合非常穩(wěn)定的版本。e) 生成索引時(shí)容易發(fā)生磁盤空間不足寫入失敗的錯(cuò)誤Sphinx的索引需要注意配置文件中設(shè)置的Sphinx索引數(shù)據(jù)存放目錄,確保有足夠的存儲(chǔ)空間。文件占用的空間略小于數(shù)據(jù)庫(kù)數(shù)據(jù)文件本身占用的空間。6. 安裝為php擴(kuò)展(可選)cd csft-3.1/api/libsphinxclientCXXCPP="gcc -E" ./configure -prefix=/usr/local/webse

17、rver/csftmakemake installcd ././usr/local/webserver/php/bin/phpize./configure -with-sphinx=/usr/local/webserver/csftmakemake install7. 安裝SphinxSE存儲(chǔ)引擎(可選)安裝SphinxSE需要重新編譯 MySQL,簡(jiǎn)要介紹另一種搜索方法。cp -rf csft-3.1/mysqlse mysql-5.1.42/storage/sphinxcd mysql-5.1.42sh BUILD/autorun.sh./configure-prefix=/usr/loc

18、al/webserver/mysql/-enable-assembler-with-extra-charsets=complex-enable-thread-safe-client -with-big-tables -with-readline -with-ssl -with-embedded-server -enable-local-infile -with-plugins=sphinxmake && make installcd ./四、Sphinx配置1. mmseg中文分詞詞庫(kù)a)詞典格式河187x:187造假者1x:1臺(tái)北隊(duì)1x:1湖邊1其中,每條記錄分兩行。其中,第

19、一行為詞項(xiàng),其格式為:詞條t詞頻率。需要注意的是,對(duì)于單個(gè)字后面跟這個(gè)字作單字成詞的頻率,這個(gè)頻率需要在大量的預(yù)先切分好的語(yǔ)料庫(kù)中進(jìn)行統(tǒng)計(jì),7 / 20用戶增加或刪除詞時(shí),一般不需要修改這個(gè)數(shù)值;對(duì)于非單字詞,詞頻率處必須為 項(xiàng),保持一致即可。換行符使用n,字符編碼UTF-81。第二行為占位b)詞庫(kù)生成方法/usr/local/webserver/mmseg/bin/mmsegu new.txtuni.lib 文件存放于 /usr/local/webserver/csft目錄mv new.txt.uni /usr/local/webserver/csft/uni.lib整理好的文本詞庫(kù)(詞條不

20、可重復(fù))new.txt,使用mmseg生成new.txt.uni的二進(jìn)制詞庫(kù)文件。依據(jù)社區(qū)常見詞匯,針對(duì)不同行業(yè)設(shè)計(jì)相應(yīng)的高頻詞匯,分詞將更精確。uni.lib詞庫(kù)文件發(fā)生改變后,sphinx索引需全部重建。2.創(chuàng)建sphinx數(shù)據(jù)目錄結(jié)構(gòu)mkdir -p /data/sphdatamkdir -p /dev/shm/sphminute3.創(chuàng)建sphinx.conf配置文件vi /usr/local/webserver/csft/etc/sphinx.conf#數(shù)據(jù)源# Threadssource threadstype=mysql#sphinx從mysql中獲取數(shù)據(jù)源sql_host=loc

21、alhostsql_user=rootsql_pass=testsql_db=discuzsql_port=3306#sql_sock#設(shè)置mysql數(shù)據(jù)庫(kù)帳號(hào)密碼=/tmp/mysql.socksql_query_pre=SET NAMES gbksql_query_pre=SET SESSION query_cache_type=OFFsql_query_pre=REPLACE INTO cdb_sphinxcounter SELECT 1, MAX(tid) FROM cdb_threads#mysql預(yù)查詢,設(shè)置連接字符編碼等sql_query_range=SELECT MIN(tid

22、), (SELECT maxid FROM cdb_sphinxcounter WHERE indexid =1) FROM cdb_threadssql_range_step=1000#分次查詢數(shù)據(jù),不要一次將全部數(shù)據(jù)取出8 / 20SELECT tid, tid as tid, fid, iconid, typeid, sortid, authorid, dateline, displayorder, digest, special, closed, subject, 'threads' AS tablename FROM cdb_threadsWHERE tid>=

23、$start AND tid<=$end#數(shù)據(jù)內(nèi)容包含哪些字段,數(shù)據(jù)范圍是在哪些(通過(guò)cdb_sphinxcounter增量表確定數(shù)據(jù)id范圍,不用加讀表鎖)=tid=fid=iconid=typeid=sortid=authorid=dateline=displayorder=digest=special=closed=tablename必須是 >=和<=sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_timestamp sql_attr

24、_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint#設(shè)置返回的數(shù)據(jù)(返回?cái)?shù)據(jù)的字段必須是INT類型,可以在Query操作的時(shí)候設(shè)置過(guò)濾器過(guò)濾)sql_query_info=SELECT * FROM、cdb_threads' WHERE tid=$id#分鐘處理,只掃描增量,不與主索引合并source threads_minute: threadssql_query_pre=SET NAMES gbksql_query_pre=SET SESSION query_cache_type=OFFsql_query_pr

25、e=#sql_query_pre 的個(gè)數(shù)應(yīng)與主索引的個(gè)數(shù)一致1),sql_query_range=SELECT (SELECTmaxidFROM cdb_sphinxcounter WHERE indexid =MAX(tid) FROM cdb_threadssql_range_step= 1000#限制增量索引只查詢主索引中沒(méi)有的數(shù)據(jù)(當(dāng)天新增的數(shù)據(jù))sql_query= SELECT tid, tid as tid, fid, iconid, typeid, sortid, authorid, dateline, displayorder, digest, special, closed

26、, subject, 'threads_minute' AS tablename FROM cdb_threadsWHERE tid>=$start AND tid<=$end#每日處理,掃描每日增量,并且與主索引合并source threads_daily: threadssql_query_pre= SET NAMES gbksql_query_pre= SET SESSION query_cache_type=OFFsql_query_pre=sql_query_range= SELECT (SELECT maxid FROM cdb_sphinxcounte

27、r WHERE indexid = 1),MAX(tid) FROM cdb_threadssql_range_step = 1000sql_query= SELECT tid, tid as tid, fid, iconid, typeid, sortid, authorid, dateline, displayorder, digest, special, closed, subject, 'threads_daily' AS tablename FROM cdb_threadsWHERE tid>=$start AND tid<=$endsql_query_p

28、ost= REPLACE INTO cdb_sphinxcounter SELECT 1,MAX(tid) FROM cdb_threads#獲取數(shù)據(jù)后,改寫 cdb_sphinxcounter增量索引計(jì)數(shù)表中的值#threads1為存檔的,拆分的歷史數(shù)據(jù)表,如果該站點(diǎn)的Discuz!沒(méi)有分表,不需要此配置段source threads1: threadssql_query_pre= SET NAMES gbksql_query_pre= SET SESSION query_cache_type=OFFsql_query_pre=#sql_query_pre 的個(gè)數(shù)應(yīng)與主索引的個(gè)數(shù)一致sql_

29、query_range=SELECTMIN(tid), MAX(tid) FROM cdb_threads1sql_range_step=1000sql_query=SELECT tid, tid as tid, fid, iconid, typeid, sortid, authorid, dateline, displayorder, digest, special, closed, subject, 'threads1' AS tablename WHERE tid>=$start AND tid<=$end#設(shè)置數(shù)據(jù)存放信息index threadssourc

30、e=threadspath=/data/sphdata/threadsdocinfo=externmlock=0morphology=nonemin_word_len=2charset_type=zh_cn.gbkcharset_dictpath=/usr/local/webserver/csft/html_strip=0#每分鐘增量數(shù)據(jù)存放在內(nèi)存中減少I/O操作index threads_minute: threadssource path=threads_minute=/dev/shm/sphminute/threads_minuteindex threads_daily: threads

31、source=threads_dailypath=/data/sphdata/threads_daily#存檔分表的數(shù)據(jù)存放index threads1: threadssource=threads1path=/data/sphdata/threads1#全文source poststype=mysqlsql_host=localhostsql_user=rootsql_pass=testsql_db=discuzsql_port=3306sql_query_pre=SET NAMES gbksql_query_pre=SET SESSION query_cache_type=OFFsql_q

32、uery_pre=REPLACE INTO cdb_sphinxcounter SELECT 2, MAX(pid) FROM cdb_postssql_query_range=SELECT MIN(pid), (SELECT maxid FROM cdb_sphinxcounter WHERE indexid =2) FROM cdb_postssql_range_step=500sql_query= SELECT p.pid, p.pid as pid, p.tid, p.fid, p.first, p.invisible, p.authorid, p.dateline, t.iconid

33、, t.typeid, t.sortid, t.displayorder, t.digest, t.special, t.closed, p.subject, p.message, 'posts' AS tablename FROM cdb_posts p INNER JOIN cdb_threads t ON p.tid = t.tid WHERE p.pid>=$start AND p.pid<=$endsql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_u

34、int sql_attr_timestamp sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint sql_attr_uint=pid=tid=fid=first=invisible=authorid=dateline=iconid=typeid=sortid=displayorder=digest=special=closed=tablenamesql_query_info=SELECT * FROM 'cdb_posts' WHERE

35、 pid=$idsql_query_pre sql_query_pre sql_query_pre sql_query_range MAX(pid) FROM cdb_posts sql_range_step=SET NAMES gbk=SET SESSION query_cache_type=OFF=SELECT (SELECT maxid FROM cdb_sphinxcounter WHERE indexid=5002),source posts_minute: posts12 / 20sql_querySELECT p.pid, p.pid as pid, p.tid, p.fid,

36、p.first, p.invisible, p.authorid, p.dateline, t.iconid, t.typeid, t.sortid,t.displayorder, t.digest, t.special, t.closed, p.subject, p.message, 'posts_minute' AS tablename FROM cdb_posts p INNER JOIN cdb_threads t ON p.tid = t.tid WHERE p.pid>=$start AND p.pid<=$endsource posts_daily:

37、postssql_query_pre=SET NAMES gbksql_query_pre=SET SESSION query_cache_type=OFFsql_query_pre=2),sql_query_range=SELECT (SELECTmaxidFROM cdb_sphinxcounter WHERE indexidMAX(pid) FROM cdb_postssql_range_step = 500sql_query= SELECT p.pid, p.pid as pid, p.tid, p.fid, p.first, p.invisible, p.authorid, p.da

38、teline, t.iconid, t.typeid, t.sortid,t.displayorder, t.digest, t.special, t.closed, p.subject, p.message, 'posts_daily' AS tablename FROM cdb_posts p INNER JOIN cdb_threads t ON p.tid = t.tid WHERE p.pid>=$start AND p.pid<=$endsql_query_postREPLACE INTO cdb_sphinxcounter SELECT 2,MAX(p

39、id) FROM cdb_posts#帖子內(nèi)容數(shù)據(jù)分表source posts1: postssql_query_pre=SET NAMES gbksql_query_pre=SET SESSION query_cache_type=OFFsql_query_pre=sql_query_range sql_range_step=SELECT MIN(pid), MAX(pid) FROM cdb_posts1=500sql_query=SELECT p.pid, p.pid as pid, p.tid, p.fid, p.first, p.invisible, p.authorid, p.da

40、teline, t.iconid, t.typeid, t.sortid,t.displayorder, t.digest, t.special, t.closed, p.subject, p.message, 'posts1' AS tablename FROM cdb_posts p INNER JOIN cdb_threads t ON p.tid = t.tid WHERE p.pid>=$start AND p.pid<=$endindex postssource=postspath=/data/sphdata/postsdocinfo=externmlo

41、ck=0morphology=nonemin_word_len=2charset_type=zh_cn.gbkcharset_dictpath=/usr/local/webserver/csft/html_strip=0index posts_minute: posts14 / 20sourcepathindex posts_daily: postssource=posts_dailypath=/data/sphdata/posts_dailyindex postsl: postssource=postslpath=/data/sphdata/postsl# indexer settings#

42、indexermem limit=128Mmax iosize=524288#此處的mem_limit和max_iosize需要配合設(shè)置,不能太大影響服務(wù)器運(yùn)行,也不能太小導(dǎo)致sphinx生成索引緩慢。如果遇到生成索引提前退岀而沒(méi)有任何報(bào)錯(cuò)的情況,需適當(dāng)調(diào)小mem limit 設(shè)置# searchd settings#searchdlisten=3312#listen=/tmp/searchd.sock#如果監(jiān)聽為sock方式,注意設(shè)置每次啟動(dòng)時(shí)設(shè)置 sock文件的權(quán)限為777log=/usr/local/webserver/csft/var/log/searchd.logquery_log=

43、/usr/local/webserver/csft/var/log/query .logread timeout=10client timeout=10max children=30pid_file=/var/run/searchd.pid=posts_minute =/dev/shm/sphminute/posts_minute15 / 20# / 20=10000max matchesseamless rotate# / 20preopen_indexes=0unlink_old=1mva_updates_pool=1Mmax_packet_size=16Mmax_filters=256m

44、ax filter values=40964. mmseg.ini分詞配置文件rm f /usr/local/webserver/csft/mmseg.inivi /usr/local/webserver/csft/mmseg.ini mmseg merge_number_and_ascii=1; number_and_ascii_joint=-.; compress_space=0; seperate_number_ascii=O;5. 建立Sphinx增量索引數(shù)據(jù)表CREATE TABLE IF NOT EXISTS 'cdb_sphi nxcou nter' ('

45、in dexid' t inyin t(1) NOT NULL,'maxid' int(1O) NOT NULL,PRIMARY KEY('i ndexid')ENGINE=MylSAM DEFAULT CHARSET=gbk;該表為普通表,區(qū)別于 SphinxSE存儲(chǔ)引擎專用表,該表的數(shù)據(jù)僅僅是配合Sphinx的增量索引而計(jì)數(shù)的表,indexid表示索引id,值為1表示threads,值為2表示posts,對(duì)應(yīng)的maxid分別記錄 了上次sphinx增量索引合并時(shí)取得的最大 id數(shù)。6. 書寫常用的sphinx控制命令到sh文件vi /usr/loca

46、l/webserver/csft/sph.sh增加以下內(nèi)容#!/bin/sh#本shell腳本是將常用的sphinx命令寫入到shell中,減少代碼量,注意配置部分 #config start#sphinx_path=/usr/local/webserver/csft/sphinx_conf=/usr/local/webserver/csft/etc/sphinx.confminute_data_path=/dev/shm/sphminute/update_indexs=("threads" "posts")rebuildl_indexs=("

47、threads" "posts" "threadsl" "postsl" "threads2" "posts2")#config end#si=0sj=$#update_indexssk=$#rebuild indexssph_start () $sphinx_pathbin/searchd -config $sphinx_conf#chmod 777 /tmp/searchd.sock#如果監(jiān)聽為sock方式,注意啟動(dòng)命令設(shè)置sock文件權(quán)限sph_stop () $sphinx_p

48、athbin/searchd -config $sphinx_conf -stopsleep 3sph_createallindex () $sphinx_pathbin/indexer -config $sphinx_conf -allsph_rebuild () si=0while "$si" -lt "$sk"do$sphinx_pathbin/indexer -config $sphinx_conf $rebuild_indexs$si -rotatesleep 10let si=si+1donesph_minute_create () mkdi

49、r -p $minute_data_pathsi=0while "$si" -lt "$sj"do$sphinx_pathbin/indexer -config $sphinx_conf $update_indexs$si_minutesleep 10let si=si+1donesph_minute () si=0while "$si" -lt "$sj"do$sphinx_pathbin/indexer -config $sphinx_conf $update_indexs$si_minute -rotate

50、sleep 10let si=si+1donesph_daily () si=Owhile "$si" -It "$sj"&&do$sphinx_pathbin/indexer-config $sphinx_conf $update_indexs$si_daily-rotate$sphinx_pathbin/indexer -config $sphinx_conf -merge $update_indexs$si $update_indexs$si_daily -rotate sleep 10let si=si+1donecase &qu

51、ot;$1" instart)sph_startstop)sph_stoprestart)sph_stopsph_startminute)sph_minutedaily)sph_dailycreate)sph_stopsph_createallindexsph_startminute_create)sph_minute_createsph_startrebuild)sph_rebuild*)echo "Usage: $0 start|stop|restart|minute|daily|create|minute_create|rebuild"exit 1esac編

52、輯完后,設(shè)置可執(zhí)行權(quán)限chmod +x /usr/local/webserver/csft/sph.shshell使用舉例:/usr/local/webserver/csft/sph.sh start #啟動(dòng) sphinx 的 searchd 守護(hù)進(jìn)程/usr/local/webserver/csft/sph.sh stop #停止 sphinx 的 searchd 守護(hù)進(jìn)程7. 創(chuàng)建相應(yīng)的文件結(jié)構(gòu),初始化Sphinx的全部索引mkdir -p /data/sphdata/usr/local/webserver/csft/sph.sh creat此過(guò)程耗時(shí)比較長(zhǎng),可以配合screen進(jìn)入無(wú)人值

53、守模式防止中斷,適當(dāng)調(diào)整indexer配置段中的內(nèi)存和I/O占用量,避開高峰時(shí)期建立索引,以免出現(xiàn)服務(wù)器無(wú)法響應(yīng)問(wèn)題。8. 啟動(dòng) sphinx/usr/local/webserver/csft/sph.sh start9. 設(shè)置計(jì)劃任務(wù)項(xiàng)crontab -e增加以下內(nèi)容#sphinx增量索引,每5分鐘重建一次增量索引,資源消耗少(4、5點(diǎn)之間是每15分鐘重建一次增量索引)*/5 0-2 * * * /usr/local/webserver/csft/sph.sh minute*/15 4,5 * * * /usr/local/webserver/csft/sph.sh minute*/5 6-

54、23 * * * /usr/local/webserver/csft/sph.sh minute#sphinx每天03:30將增量索引與主索引合并,合并耗時(shí)約10分鐘30 3 * * * /usr/local/webserver/csft/sph.sh daily#sphinx適當(dāng)?shù)臅r(shí)機(jī)需要重新建立全部索引,耗時(shí)約1小時(shí),例如每?jī)蓚€(gè)月# /usr/local/webserver/csft/sph.sh rebuild10. 設(shè)置開機(jī)啟動(dòng)項(xiàng)vi /etc/rc.d/rc .lo cal增加以下內(nèi)容#/dev/shm是內(nèi)存信息,重啟將會(huì)導(dǎo)致文件結(jié)構(gòu)丟失,所以開機(jī)初始化一次增量數(shù)據(jù)并啟動(dòng)sphinx/usr/local/webserver/csft/sph.sh minute creat五、通過(guò)命令行測(cè)試搜索/usr/local/webserver/csft/bin/search -config /usr/local/webserver/csft/etc/sphinx.conf -i threads康盛創(chuàng)想-i表示指定使用哪個(gè)索引搜索,-config指定

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論