本文將講述:sphinx在windows下的安裝配置;sphinx與PHP的即時索引使用;sphinx服務加入windows服務。
參考手冊:http://www.coreseek.cn/docs/coreseek_4.1-sphinx_2.0.1-beta.html
一、sphinx在windows下的安裝配置
1.:
http://sphinxsearch.com/downloads/ 盡量下高一點的版本,我用的Sphinx 2.0.6-release,
下載後解壓到,f:/sphinx(根據你的情況安裝).
將sphinx.conf.in拷貝重名到/bin/sphinx.conf
建立:f:/sphinx/data 檔案夾
建立:f:/sphinx/log 檔案夾
2.修改設定檔(詳細配置見http://blog.csdn.net/design321/article/details/8895608)
## Minimal Sphinx configuration sample (clean, simple, functional)#source src1{#資料庫資訊 type = mysql sql_host = localhost sql_user = root sql_pass = 123456 sql_db = test sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content FROM documents sql_attr_uint = group_id sql_attr_timestamp = date_added sql_query_info = SELECT * FROM documents WHERE id=$id}index test2{ source = src1 path = f:/sphinx/data/test1 //建立的data目錄 docinfo = extern charset_type = utf-8 min_prefix_len = 0 min_infix_len = 0 min_word_len = 1 ngram_len = 1 ngram_chars = U+3000..U+2FA1F charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F}indexer{ mem_limit = 32M}searchd{ port = 3312 log =f:/sphinx/log/searchd.log #建立的log目錄 #query_log = F:/sphinx/log/query.log #建立的log目錄 read_timeout = 5 max_children = 30 pid_file = f:/sphinx/log/searchd.pid #建立的log目錄 max_matches = 1000 preopen_indexes = 0 unlink_old = 1 seamless_rotate = 0}# eof
配置完成
3.匯入測試資料到mysql
將example.sql檔案在test資料庫執行。
注意,匯入的資料非utf8格式,需要自已去資料庫改一下字條集。
4.建立索引
F:\sphinx\bin>indexer.exe --config f:/sphinx/bin/sphinx.conf --all
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file 'f:/sphinx/bin/sphinx.conf'...
indexing index 'test2'...
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 138 bytes
total 0.011 sec, 12402 bytes/sec, 269.61 docs/sec
total 2 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
5.尋找
F:\sphinx\bin>search.exe test
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
index 'test2': query 'test ': returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=2, weight=2252, group_id=1, date_added=Mon May 06 18:43:15 2013
id=2
group_id=1
group_id2=6
date_added=2013-05-06 18:43:15
title=test two
content=?? this is my test document number two
2. document=3, weight=1319, group_id=2, date_added=Mon May 06 18:43:15 2013
id=3
group_id=2
group_id2=7
date_added=2013-05-06 18:43:15
title=another doc
content=this is another group ?? test
3. document=4, weight=1319, group_id=2, date_added=Mon May 06 18:43:15 2013
id=4
group_id=2
group_id2=8
date_added=2013-05-06 18:43:15
title=doc number four
content=this is to test groups ??
words:
1. 'test': 3 documents, 4 hits
得到以上內容,安裝成功。
二、讓PHP 能使用sphinx
1.啟動sphinx服務
F:\sphinx\bin>searchd.exe
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file './sphinx.conf'...
WARNING: compat_sphinxql_magics=1 is deprecated; please update your application
and config
listening on all interfaces, port=3312
precaching index 'test2'
precached 1 indexes in 0.002 sec
binlog: replaying log ./binlog.001
2,將F:\sphinx\api\sphinxapi.php拷貝到apach下的網站目錄,以供調用.
3.利用現有索引的簡單樣本
<?phprequire 'sphinxapi.php';$s = new SphinxClient();$s->SetServer('127.0.0.1',3312); //伺服器名,sphinx連接埠號碼$result = $s->Query('test','test2'); //關鍵詞,索引名(與設定檔裡一致,為*時表示全部)echo '<pre>';var_dump($result);echo '<pre>';?>
4.建立即時索引的樣本
<?php require 'sphinxapi.php'; $keyword='中文'; $sphinx=new SphinxClient(); $sphinx->SetServer("localhost",3312); $sphinx->SetMatchMode(SPH_MATCH_ANY); //$sphinx->setLimits(0,0); //位移量 $result=$sphinx->query("$keyword","test2"); //echo "<pre>"; //print_r($result); //echo "</pre>"; $ids=join(",",array_keys($result['matches'])); mysql_connect("localhost","root","123456"); mysql_select_db("test"); $sql="select * from documents where id in({$ids})"; mysql_query("set names utf8"); $rst=mysql_query($sql); $opts=array( "before_match"=>"<button style='font-weight:bold;color:#f00'>", "after_match"=>"</button>" ); while($row=mysql_fetch_assoc($rst)){ $rst2=$sphinx->buildExcerpts($row,"main",$keyword,$opts);echo '<pre>'; var_dump($row);echo ''; /* echo "第{$rst2[0]}篇貼子<br>"; echo "標題: {$rst2[1]}<br>"; echo "內容: {$rst2[2]}<br>"; echo "<hr>"; */ }
三、將sphinx服務添加到windows服務
F:\sphinx\bin>searchd.exe --install -c f:\sphinx\bin\sphinx.conf --servicename s
phinx
Sphinx 2.0.6-release (r3473)
Copyright (c) 2001-2012, Andrew Aksyonoff
Copyright (c) 2008-2012, Sphinx Technologies Inc (http://sphinxsearch.com)
Installing service...
Service 'sphinx' installed successfully.
F:\sphinx\bin>net start sphinx
sphinx 服務正在啟動 .
sphinx 服務已經啟動成功。
F:\sphinx\bin>net stop sphinx
sphinx 服務正在停止..
sphinx 服務已成功停止。
F:\sphinx\bin>sc delete sphinx
[SC] DeleteService SUCCESS
end.