国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

sphinx學(xué)習(xí)技巧:億萬級項(xiàng)目都在用的sphinx

2019-11-14 12:15:44
字體:
供稿:網(wǎng)友

前言

年輕的時候總以為很多app或者網(wǎng)站的搜索功能是基于cache+sql的模式進(jìn)行查詢的,也未曾想過數(shù)據(jù)是億萬級別,用戶也是億萬級別時候,cache和sql的入門級模式是否能應(yīng)對。答案是肯定不能hold住的,現(xiàn)在年長了些,隨著項(xiàng)目的發(fā)展有幸接觸到相關(guān)解決方案,所以想記下來,備忘。 那么老規(guī)則本文主要解決三個問題: 1.如何解決與設(shè)計數(shù)據(jù)和用戶都是億萬級別的搜索的思路。 2.sphinx的簡介與特性 3.sphinx的安裝與運(yùn)行 4.sphinx在億級項(xiàng)目中的使用場景

正文開始

1.如何解決與設(shè)計數(shù)據(jù)和用戶都是億萬級別的搜索的思路。

首先當(dāng)數(shù)據(jù)量和用戶基數(shù)很大時候,意味著三個問題需要解決: 1.查詢的次數(shù)會很多并且需要快速返回; 2.查詢并發(fā)數(shù)會很高,如何正確的分流分壓; 3.數(shù)據(jù)的增長會很快,這部分增長的數(shù)據(jù)如何有效的處理才能實(shí)時搜索到;

MySQL自身的全文索引搜索慢,定制化程度低,自然無法滿足解決上述問題,那么就需要更高性能的自定義的搜索,sphinx出現(xiàn)了,它提供了針對上述三大問題都有相應(yīng)的解決方案。sphinx是以以俄國全文檢索引擎,提供了高速、低空間占用、高結(jié)果相關(guān)度的全文搜索功能。主要方式是提供符合條件的數(shù)據(jù)源給sphinx,sphinx生成索引,依賴索引對外提供服務(wù)。更重要的是sphinx內(nèi)置mysql數(shù)據(jù)庫數(shù)據(jù)源的支持,使用起來非常簡單,和使用mysql很大程度相似。

2.sphinx的簡介與特性

我的理解中的sphinx

1.sphinx的機(jī)制兩部分構(gòu)成:生成索引+search索引 2.sphinx索引類型:普通索引+rt實(shí)時索引+分布式索引

特性(最新版sphinx性能某些方面更高于下面描述)

1.高速的建立索引(在當(dāng)代CPU上,峰值性能可達(dá)到10 MB/秒); 2.高性能的搜索(在2 – 4GB 的文本數(shù)據(jù)上,平均每次檢索響應(yīng)時間小于0.1秒); 3.可處理海量數(shù)據(jù)(目前已知可以處理超過100 GB的文本數(shù)據(jù), 在單一CPU的系統(tǒng)上可 處理100 M 文檔); 4.提供了優(yōu)秀的相關(guān)度算法,基于短語相似度和統(tǒng)計(BM25)的復(fù)合Ranking方法; 5.支持分布式搜索; 6.可作為MySQL的存儲引擎提供搜索服務(wù); 7.支持布爾、短語、詞語相似度等多種檢索模式; 8.文檔支持多個全文檢索字段(最大不超過32個); 9.文檔支持多個額外的屬性信息(例如:分組信息,時間戳等); 10.支持單一字節(jié)編碼和UTF-8編碼; 11.原生的MySQL支持(同時支持MyISAM 和InnoDB ); 12.原生的PostgreSQL 支持.

反正就是很牛逼就是了。

3.sphinx的安裝與運(yùn)行(此部分轉(zhuǎn)載的)

1.需要安裝的軟件 coreseek的mmseg包 mysql安裝包 sphinx-0.9.8版 sphinx中文分詞補(bǔ)丁1 sphinx中文分詞補(bǔ)丁2

2.安裝libmmseg

tar -zxvf mmseg-0.7.3.tar.gz cd mmseg-0.7.3 ./configure --PRefix=/usr/local/mmseg make make install 1234512345

有問題嘗試執(zhí)行下面命令

echo '/usr/local/mmseg/lib' >> /etc/ld.so.conf ldconfig -v ln -s /usr/local/mmseg/bin/mmseg /bin/mmseg123123

3.重新編譯mysql 安裝sphinx之前先裝兩個補(bǔ)丁。

tar -zxvf sphinx-0.9.8-rc2.tar.gz cd sphinx-0.9.8 patch -p1 < ../sphinx-0.98rc2.zhcn-support.patch patch -p1 < ../fix-crash-in-excerpts.patch12341234

4.安裝sphinx

cd /root/lemp/sphinx-0.9.8-rc2 ./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql / --with-mysql-includes=/opt/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql / --with-mmseg-includes=/usr/local/mmseg/include --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg make1234512345tokenizer_zhcn.cpp:1:30: SegmenterManager.h: 沒有那個文件或目錄 tokenizer_zhcn.cpp:2:23: Segmenter.h: 沒有那個文件或目錄1212make clean ./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql / --with-mysql-includes=/usr/local/mysql/include/mysql --with-mysql-libs=/opt/mysql/lib/mysql / --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg/root/sphinx/sphinx-0.9.8-rc2/src/tokenizer_zhcn.cpp:34: undefined reference to `libiconv_close' collect2: ld returned 1 exit status123456123456官網(wǎng)解決辦法:In the meantime I've change the configuration file and set#define USE_LIBICONV 0 in line 8179.修改configure 文件把 #define USE_LIBICONV 0 最后的數(shù)值由1改為0重新編譯。1234512345
make clean ./configure --prefix=/usr/local/sphinx --with-mysql=/opt/mysql / --with-mysql-includes=/usr/local/mysql/include/mysql --with-mysql-libs=/usr/local/mysql/lib/mysql / --with-mmseg-includes=/usr/local/mmseg/include/mmseg --with-mmseg-libs=/usr/local/mmseg/lib --with-mmseg12341234
vi configure輸入/define USE_LIBICONV 找到目標(biāo)行按i鍵后將1改成0,按esc,輸入:wq保存退出123123
make make installcd /usr/local/sphinx/etc cp sphinx.conf.dist sphinx.conf12341234

5.配置sphinx

vim /usr/local/sphinx/etc/sphinx.conftype = mysql # some straightforward parameters for SQL source types sql_host = localhost sql_user = root sql_pass = sql_db = test sql_port = 3306 # optional, default is 3306address = 127.0.0.1 #安全點(diǎn)可以只監(jiān)聽本機(jī)12345678910111234567891011

6.索引建立 裝好sphinx后在sphinx的目錄中有三個目錄 分別為bin etc var bin中 存有sphinx用到的一些執(zhí)行文件 包括 indexer 索引建立 search 查詢工具 searchd 查詢服務(wù)器。備注:最新版已經(jīng)沒有search 查詢工具了

usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf test1  建立索引期間可能由于不同版本的數(shù)據(jù)庫導(dǎo)致indexer找不到共享庫libmysqlclient.so.16需要把/opt/mysql/lib/mysql/libmysqlclient.so.16.0.0 這個文件復(fù)制到/usr/lib下 或者作軟連接即可12341234

7.查詢服務(wù)器 /usr/local/sphinx/bin/searchd –config /usr/local/sphinx/etc/sphinx.conf 為開啟

/usr/local/sphinx/bin/searchd –config /usr/local/sphinx/etc/sphinx.conf –stop 為關(guān)閉

sphinx的查詢 可以大致分為三種

7.1 數(shù)據(jù)庫引擎中的查詢7.2 通過search工具查詢(最新版已不提供這個工具)    /usr/local/sphinx/bin/search --config     /usr/local/sphinx/etc/sphinx.conf test7.3 通過php的接口查詢 詳見sphinxapi.php

8.創(chuàng)建sphinx啟動腳本與配置

#!/bin/sh # sphinx: Startup script for Sphinx search # # chkconfig: 345 86 14 # description:  This is a daemon for high performance full text / #               search of MySQL and PostgreSQL databases. / #               See http://www.sphinxsearch.com/ for more info. # # processname: searchd # pidfile: $sphinxlocation/var/log/searchd.pid # Source function library. . /etc/rc.d/init.d/functions processname=searchd servicename=sphinx username=sphinx sphinxlocation=/usr/local/sphinx pidfile=$sphinxlocation/var/log/searchd.pid searchd=$sphinxlocation/bin/searchd RETVAL=0 PATH=$PATH:$sphinxlocation/bin start() {     echo -n $"Starting Sphinx daemon: "     daemon --user=$username --check $servicename $processname     RETVAL=$?     echo     [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename } stop() {     echo -n $"Stopping Sphinx daemon: "     $searchd --stop     #killproc -p $pidfile $servicename -TERM     RETVAL=$?     echo     if [ $RETVAL -eq 0 ]; then         rm -f /var/lock/subsys/$servicename         rm -f $pidfile     fi } # See how we were called. case "$1" in     start)         start         ;;     stop)         stop         ;;     status)         status $processname         RETVAL=$?         ;;     restart)         stop sleep 3         start         ;;     condrestart)         if [ -f /var/lock/subsys/$servicename ]; then             stop     sleep 3             start         fi         ;;     *)         echo $"Usage: $0 {start|stop|status|restart|condrestart}"         ;; esac exit $RETVAL123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
chmod 755 /etc/init.d/sphinx chkconfig --add sphinx chkconfig --level 345 sphinx on chkconfig --list|grep sphinx #檢查下service sphinx start #運(yùn)行 service sphinx stop  #停止,官方的腳本在我的as4上有點(diǎn)問題,所以粗魯?shù)母牧讼?service sphinx restart #重啟 service sphinx status #查看是否運(yùn)行#檢查下已用sphinx用戶運(yùn)行ps aux |grep searchd sphinx   24612  0.0  0.3 11376 6256 pts/1    S    14:07   0:00 searchd123456789101112123456789101112

4.sphinx在億級項(xiàng)目中的使用場景

不管是網(wǎng)站還是app很多產(chǎn)品的設(shè)計思路和產(chǎn)品功能多多少少都有相似之處,那么這邊主要講以下幾個場景

描述、話題的搜索

主要的實(shí)現(xiàn)思路是全量索引+增量索引方式,可設(shè)定時任務(wù)定點(diǎn)跑索引

用戶昵稱的搜索

主要是實(shí)現(xiàn)思路是實(shí)時索引+分布式索引的方式,用戶由于過多,故使用實(shí)時索引的方法進(jìn)行增加,舊數(shù)據(jù)通過跑腳本重新讀取后再寫入。

搜索框聯(lián)想詞的提示

主要實(shí)現(xiàn)思路是分布式索引的方式,自動聯(lián)想其他人曾經(jīng)輸入過的詞語。


tip: morphology = stem_en會啟用英文單詞的提取。搜索英文時候就不會一個一個字母搜了,會提高sphinx搜索英文單詞的時候的效率。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘肃省| 平利县| 南汇区| 万年县| 恩平市| 竹山县| 蒙山县| 治县。| 涟源市| 正蓝旗| 盈江县| 高台县| 南通市| 衡东县| 耒阳市| 隆回县| 泽普县| 洱源县| 延津县| 汝城县| 卢湾区| 石家庄市| 海林市| 德清县| 普安县| 招远市| 太仆寺旗| 黄大仙区| 临漳县| 剑阁县| 新平| 烟台市| 炎陵县| 桑植县| 城口县| 丰都县| 江阴市| 晴隆县| 洪泽县| 西充县| 阿拉善盟|