這次演示的環境是:win7系統,64位,php5.4.x,apachephp
sphinx,斯芬克斯(英語很差的同窗能夠直接讀這個音),意獅身人面像html
特色:建立索引速度快,3分鐘左右能建立100萬條記錄的索引;檢索速度快,1000萬的記錄檢索速度在毫秒級上;爲不少腳本語言設計了檢索API;專門爲php設計了存儲引擎插件。mysql
1. 去 sphinx官網 下載一個版本,在 http://sphinxsearch.com/downloads/archive/ 頁面下載之前的版本。我下載是 Win64 binaries w/MySQL+id64 support。(注意:要選擇適合本身本地環境的版本)sql
(建議初學者別下載最新的版本,由於最新的版本資料少,出了問題初學者本身還解決不了,容易打擊學習的信心)。數據庫
2. 直接解壓到一個目錄裏,如 D:\sphinx ,新建兩個文件夾,data和log。目錄結構以下圖:apache
3. 移動配置:將"sphinx-min.conf.in"複製到bin目錄下,並更名爲 "sphinx.conf"。(sphinx-min.conf.in和sphinx.conf.in的區別在於前者將後者存在的註釋全去掉了,在我看來,這樣更直觀簡單,註釋太多的話,壓力太大)api
4. 修改配置:打開"sphinx.conf",根據實際狀況修改配置。less
配置文件總共分爲1個source,名爲src1,也就是數據源;2個index,test1和testrt,表示索引;1個indexer,猜想是indexer命令的配置項;1個searchd,猜想是searchd命令的配置項。ide
a. 數據源配置。學習
就是配置數據庫host,user,pass等等之類的,相信你們已經很熟了。sql_query就是一句select語句。sql_attr_xxx表示數據表的字段的屬性,主要用於過濾和排序。
若是數據庫是utf8,在sql_port下加一行"sql_query_pre = SET NAMES utf8 "
另外將配置文件全部的 "@CONFDIR@" (表示配置目錄)替換爲 "D:/sphinx"。
下面是修改後的配置文件
1 # 2 # Minimal Sphinx configuration sample (clean, simple, functional) 3 # 4 5 source src1 6 { 7 type = mysql 8 9 sql_host = localhost 10 sql_user = root 11 sql_pass = root 12 sql_db = test 13 sql_port = 3306 14 15 #若是數據庫是utf編碼 16 sql_query_pre = SET NAMES utf8 17 18 sql_query = \ 19 SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \ 20 FROM documents 21 22 sql_attr_uint = group_id 23 sql_attr_timestamp = date_added 24 } 25 26 27 index test1 28 { 29 source = src1 30 path = D:/sphinx/data/test1 31 } 32 33 34 index testrt 35 { 36 type = rt 37 rt_mem_limit = 128M 38 39 path = D:/sphinx/data/testrt 40 41 rt_field = title 42 rt_field = content 43 rt_attr_uint = gid 44 } 45 46 47 indexer 48 { 49 mem_limit = 128M 50 } 51 52 53 searchd 54 { 55 listen = 9312 56 listen = 9306:mysql41 57 log = D:/sphinx/log/searchd.log 58 query_log = D:/sphinx/log/query.log 59 read_timeout = 5 60 max_children = 30 61 pid_file = D:/sphinx/log/searchd.pid 62 seamless_rotate = 1 63 preopen_indexes = 1 64 unlink_old = 1 65 workers = threads # for RT to work 66 binlog_path = D:/sphinx/data 67 }
5. 導入 "example.sql" 到數據庫。(注意:該數據庫就是上面配置中的數據庫)
6. 打開CMD窗口:進入bin目錄,shift+鼠標右鍵,選擇"在此處打開命令窗口",進入cmd窗口。
7. 創建索引:在cmd命令行中輸入 indexer.exe --all
8. 開啓searchd服務:在cmd命令行下輸入 searchd.exe
9. 成功後測試:
a. 在D:\sphinx\api下寫一個foo.php;
1 <?php 2 3 require_once 'sphinxapi.php'; 4 5 $s = new SphinxClient(); 6 $s->setServer('localhost', 9312); 7 $result = $s->Query('number'); //搜索"number"字符串 8 9 print_r($result);
b. 執行這個foo.php;
在cmd命令輸入 php D:\sphinx\api\foo.php ,結果以下
1 Array 2 ( 3 [error] => 4 [warning] => 5 [status] => 0 6 [fields] => Array 7 ( 8 [0] => title 9 [1] => content 10 ) 11 12 [attrs] => Array 13 ( 14 [group_id] => 1 15 [date_added] => 2 16 ) 17 18 [matches] => Array 19 ( 20 [1] => Array 21 ( 22 [weight] => 1442 23 [attrs] => Array 24 ( 25 [group_id] => 1 26 [date_added] => 1476926888 27 ) 28 29 ) 30 31 [2] => Array 32 ( 33 [weight] => 1442 34 [attrs] => Array 35 ( 36 [group_id] => 1 37 [date_added] => 1476926888 38 ) 39 40 ) 41 42 [4] => Array 43 ( 44 [weight] => 1442 45 [attrs] => Array 46 ( 47 [group_id] => 2 48 [date_added] => 1476926888 49 ) 50 51 ) 52 53 ) 54 55 [total] => 3 56 [total_found] => 3 57 [time] => 0.000 58 [words] => Array 59 ( 60 [number] => Array 61 ( 62 [docs] => 3 63 [hits] => 3 64 ) 65 66 ) 67 68 )