Sphinx的安裝和使用


sphinx的使用兩種方式:

1、使用sphinx的API來操作sphinx,PHP中將API編譯到PHP中作為擴展

2、使用mysql的sphinx的存儲引擎

sphinx是英文的全文檢索引擎,coreseek是支持中文詞庫的全文檢索引擎,Lucene是用java實現的全文檢索引擎。

使用sphinx搜索引擎對數據做索引,數據一次性加載進來后保存在內存中,用戶在進行搜索的時候只需要在sphinx服務器上檢索數據即可。整個流程是:Indexer程序到數據庫里面提取數據,對數據進行分詞,然后根據生成的分詞生成單個或多個索引,並將它們傳遞給searchd程序,然后客戶端可以通過API調用進行搜索。

流程圖解釋:

Database:數據源,是sphinx做索引的數據來源。

Indexer:索引程序,從數據源中獲取數據,並將數據生成全文索引。根據需求定期運行Indexer達到定時更新索引的需求。

## sphinx使用配置文件從數據庫讀出數據之后,就將數據傳遞給Indexer程序,然后Indexer會逐條讀取記錄,根據分詞算法對每條記錄建立索引,分詞算法可以是一元分詞或mmseg分詞。

Searchd:Searchd直接與客戶端程序進行對話,並使用Indexer程序構建好的索引來快速地處理搜索查詢。

App客戶端:接收來自用戶輸入的搜索字符串,發送查詢給searchd程序並顯示返回結果。

【安裝過程】

# 到sphinx官網上下載源碼文件:http://sphinxsearch.com/files/sphinx-2.2.10-release.tar.gz
[root@localhost ~]# cd /usr/local/src
[root@localhost ~]# tar -zxvf sphinx-2.2.10-release.tar.gz
[root@localhost ~]# cd sphinx-2.2.10-release
[root@localhost sphinx-2.2.10-release]# ./configure --prefix=/usr/local/sphinx --with-mysql
[root@localhost sphinx-2.2.10-release]# make && make install

# libsphinxclient安裝(PHP模塊需要)
[root@localhost sphinx-2.2.10-release]# cd api/libsphinxclient
[root@localhost libsphinxclient]# ./configure --prefix=/usr/local/sphinx
[root@localhost libsphinxclient]# make && make install

安裝PHP的sphinx模塊

#下載sphinx擴展包:http://pecl.php.Net/package/sphinx
[root@localhost src]# tar -zxvf sphinx-1.3.3.tgz
[root@localhost src]# cd sphinx-1.3.3
[root@localhost sphinx-1.3.3]# phpize
[root@localhost sphinx-1.3.3]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinx/
[root@localhost sphinx-1.3.3]# make && make install

# 安裝成功:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

# 編輯php.ini
[root@localhost sphinx-1.3.3]# vim /usr/local/php/etc/php.ini
添加:extension=sphinx.so
# 重啟nginx服務器
[root@localhost ~]# vim /etc/ld.so.conf
# 添加如下內容:
/usr/local/mysql/lib
[root@localhost ~]# ldconfig  # 使命令生效

【Sphinx配置文件】

# sphinx可以定義多個索引與數據源,不同的索引與數據源可以應用到不同表或不同應用的全文檢索。

## 數據源 src1
source src1
{
    ## 說明數據源類型,數據源類型可以是:mysql、mssql、odbc等等
    type  =  mysql
    ## 下面是sql數據庫特有的端口、用戶名、密碼數據庫名等。
    sql_host  =  localhost
    sql_user  =  root
    sql_pass  =  root
    sql_db     =  test
    sql_port  =  3306  
    ## 執行sql前的操作,設置mysql檢索編碼
    sql_query_pre = SET NAMES UTF8
    ## 全文索引要顯示的內容(盡可能不使用where、group by,將其的內容交給sphinx)
    ## select字段中必須包含一個唯一主鍵以及要全文檢索的字段,where中要用到的字段也要select出來,sphinx使用此語句從數據庫中拉取數據。
    sql_query = SELECT id , name from tablename
    ## 以下是用來過濾或條件查詢的屬性
    ## 當數據源過大時多次查詢操作
    sql_query_range = SELECT MIN( id ) , MAX( id ) FROM documents  ## 獲取最大和最小id,根據步長來獲取數據
    sql_range_step = 1000  ## 查詢的步長
    sql_ranged_throttle = 0  ## 設置分次查詢的時間間隔,單位是毫秒
    ## 以下都是不同屬性的數據(屬性字段),屬性時存在索引中,它不進行全文索引,只可以用於過濾和排序
    ## 在where、orderby、groupby中出現的字段要分別定義一個屬性(以sql_attr_開頭),定義不同類型的字段要用不同的屬性名。
    sql_attr_uint = cat_id    ## 無符號整數類型
    sql_attr_unit = member_id
    sql_attr_timestamp = add_time  ## unix時間戳
    ## 用於命令行界面調用測試
    sql_query_info = select * from tablename where id=$id
}

## 索引
index test1
{
    source = src1    ## 聲明索引源
    path = /usr/local/sphinx/var/data/test1  ## 索引文件存放路徑及索引的文件名
    ## mmseg分詞 ##
    ##charset_dictpath =  /usr/local/mmseg3/etc  ## 指定分詞讀取詞典文件的目錄,目錄下必須有uni.lib詞典,當啟用分詞發時需要填
    ## charset_type = zh_ch.utf-8  ## 設置數據編碼 utf-8/gbk
    ## 一元分詞 ##
    #charset_type = utf-8  ## 新的sphinx不支持charset_type設置
    charset_table = ## 字符表和大小寫轉換規則
    ngram_chars = ## 要進行一元字符切分模式認可的有效字符集
    ngram_len = 1  ## 分詞長度
}

## 索引器配置
indexer
{
    mem_limit = 256  ## 內存限制
}

## sphinx服務進程
searchd
{
    listen = 9312    ## 監聽端口
    listen = 9306:mysql41
    log = /usr/local/sphinx/var/log/searchd.log  ## 服務進程日志
    query_log = /usr/local/sphinx/var/log/query.log  ## 客戶端查詢日志
    read_time = 5  ## 請求超時
    max_children = 30  ##  同時可執行的最大searchd進程數
    pid_file = /usr/local/sphinx/var/log/searchd.pid  ## 進程id文件
    max_matches = 1000  ## 查詢結果的最大返回數
    seamless_rotate = 1  ## 啟動無縫輪轉
}

【生成索引】

調用indexer程序生成全部索引:

[root@localhost ~]# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all

指定某個數據源生成索引:

[root@localhost ~]# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf 索引名(配置文件中所定義的)

如果此時searchd守護進程已經啟動,需要加上--rotate參數:

[root@localhost ~]# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all --rotate

【啟動sphinx】

[root@localhost ~]# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf

【使用sphinx】

1、new SphinxClient ();    ## 創建sphinx的客戶端接口對象

2、SetServer( host , port );    ## 設置連接sphinx主機與端口

3、SetMatchMode( mode );    ## 設置全文查詢的匹配模式,mode為搜索模式

4、SetFilter( string $attribute , array $values [ , bool $exclude = false ] )  ## 增加整數型過濾器

string $attribute 屬性名稱

array $values 整數值數組

bool $exclude 匹配該過濾規則的文檔是否會被排除在結果之外

5、SetSortMode( int mode [ , string $sortby ] )    ## 設置匹配排序模式

6、SetLimits( int $offset , int $limit )    ##  設置返回結果集偏移量和數目

7、Query( string $query [ , string $index='*'  ] )    ## 執行搜索查詢

string $query 查詢的字符串

string $index 索引名稱,可以是多個,用逗號分割或者為'*'表示全部索引

返回的數據結構:


           
值說明
"matches" 存儲文檔id以及其對應的另一個包含文檔權重和屬性值得hash表
"total" 此查詢在服務器檢索所得到的匹配文檔總數(即服務器端結果集的大小,且與相關設置有關)
"total_found" 索引中匹配文檔的總數
"words" 將查詢關鍵詞(關鍵詞經過大小寫轉換,取詞干和其他處理)映射到一個包含關於關鍵字的統計數據。'docs'在多少文檔中出現,'hits'一共出現了多少次。
"error" searchd報告的錯誤信息
"warning" searchd報告的警告信息

8、buildExcerpts( array $docs , string $index , string $words [ , array $opts ] )   ## 高亮關鍵字文本片段,可以用於實現摘要的功能

array $docs 文檔內容字符串數組

string $index 檢索名稱

string $words 要高亮的關鍵詞

array $opts 關聯數組的附加突出選項

【sphinx增量索引更新】

索引建立構成:1、固定不變的主索引。2、增量索引重建。3、索引數據的合並。

在實際操作中,需要為增量索引的建立創建輔助表,這樣才可以記住最后建立索引的記錄id來做實際的增量部分的索引建立。

1)創建輔助表:CREATE TABLE `sph_counter` (`counter_id` int(11) NOT NULL COMMENT `標識不同的數據表`,`max_doc_id` int(11) NOT NULL COMMENT `每個索引表的最大ID,會實時更新`,PRIMARY KEY (`counter_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

2)在主索引的數據源中,在sql_query的查詢語句中,增加where條件語句(WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id = 1 ))

3)在增量索引的數據源中,繼承主索引數據源,在sql_query的查詢語句中,增加where條件語句,獲取主索引中沒有的數據(WHERE id > ( SELECT max_doc_id FROM sph_counter WHERE counter_id = 1 ))

4)分別配置主索引和增量索引的index定義配置。

生成主索引,可添加crontab,定時重建主索引:

/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --rotate test1
# 生成增量索引並且合並,可添加到crontab任務中每隔一段時間執行一次:
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --rotate delta
/usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --merge test1 delta --rotate

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM