一、使用sphinx的API來操做sphinx,PHP中將API編譯到PHP中做爲擴展php
二、使用mysql的sphinx的存儲引擎html
sphinx是英文的全文檢索引擎,coreseek是支持中文詞庫的全文檢索引擎,Lucene是用java實現的全文檢索引擎。java
使用sphinx搜索引擎對數據作索引,數據一次性加載進來後保存在內存中,用戶在進行搜索的時候只須要在sphinx服務器上檢索數據便可。整個流程是:Indexer程序到數據庫裏面提取數據,對數據進行分詞,而後根據生成的分詞生成單個或多個索引,並將它們傳遞給searchd程序,而後客戶端能夠經過API調用進行搜索。mysql
流程圖解釋:nginx
Database:數據源,是sphinx作索引的數據來源。算法
Indexer:索引程序,從數據源中獲取數據,並將數據生成全文索引。根據需求按期運行Indexer達到定時更新索引的需求。sql
## sphinx使用配置文件從數據庫讀出數據以後,就將數據傳遞給Indexer程序,而後Indexer會逐條讀取記錄,根據分詞算法對每條記錄創建索引,分詞算法能夠是一元分詞或mmseg分詞。數據庫
Searchd:Searchd直接與客戶端程序進行對話,並使用Indexer程序構建好的索引來快速地處理搜索查詢。vim
App客戶端:接收來自用戶輸入的搜索字符串,發送查詢給searchd程序並顯示返回結果。api
# 到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能夠定義多個索引與數據源,不一樣的索引與數據源能夠應用到不一樣表或不一樣應用的全文檢索。
## 數據源 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`
[root@localhost ~]
`# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf`
一、new SphinxClient (); ## 建立sphinx的客戶端接口對象
二、SetServer( host , port ); ## 設置鏈接sphinx主機與端口
三、SetMatchMode( mode ); ## 設置全文查詢的匹配模式,mode爲搜索模式
四、SetFilter( string $attribute , array $values [ , bool $exclude = false ] ) ## 增長整數型過濾器
string $attribute 屬性名稱
array $values 整數值數組
bool $exclude 匹配該過濾規則的文檔是否會被排除在結果以外
五、SetSortMode( int mode [ , string $sortby ] ) ## 設置匹配排序模式
六、SetLimits( int $offset , int $limit ) ## 設置返回結果集偏移量和數目
七、Query( string $query \[ , string $index='*' ] ) ## 執行搜索查詢
string $query 查詢的字符串
string $index 索引名稱,能夠是多個,用逗號分割或者爲'*'表示所有索引
返回的數據結構:
鍵
值說明
"matches"
存儲文檔id以及其對應的另外一個包含文檔權重和屬性值得hash表
"total"
此查詢在服務器檢索所獲得的匹配文檔總數(即服務器端結果集的大小,且與相關設置有關)
"total_found"
索引中匹配文檔的總數
"words"
將查詢關鍵詞(關鍵詞通過大小寫轉換,取詞幹和其餘處理)映射到一個包含關於關鍵字的統計數據。'docs'在多少文檔中出現,'hits'一共出現了多少次。
"error"
searchd報告的錯誤信息
"warning"
searchd報告的警告信息
八、buildExcerpts( array $docs , string $index , string $words \[ , array $opts ] ) ## 高亮關鍵字文本片斷,能夠用於實現摘要的功能
array $docs 文檔內容字符串數組
string $index 檢索名稱
string $words 要高亮的關鍵詞
array $opts 關聯數組的附加突出選項
索引創建構成:一、固定不變的主索引。二、增量索引重建。三、索引數據的合併。
在實際操做中,須要爲增量索引的創建建立輔助表,這樣才能夠記住最後創建索引的記錄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`