首先我們要從 Sphinx 官網上 http://www.sphinxsearch.com/downloads.html 下載 mysql-5.0.45-sphinxse-0.9.8-win32.zip 和 sphinx-0.9.8.1-win32.zip,假設你已經安裝好了 MySQL

先將 mysql 服務停掉 解壓 mysql-5.0.45-sphinxse-0.9.8-win32.zip 將 bin 和 share 覆蓋掉 mysql 目錄中的 bin 和 share 解壓 sphinx-0.9.8.1-win32.zip 到獨立的目錄,如:d:/www/sphinx/中

接著開啟 mysql 服務,建立 "test" 數據庫,并導入 sql 語句,如下:

-----------------------------------------------------------

CREATE TABLE `documents` (
`id` int(11) NOT NULL auto_increment,
`group_id` int(11) NOT NULL,
`group_id2` int(11) NOT NULL,
`date_added` datetime NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5;

INSERT INTO `documents` VALUES ('1', '1', '5', '2008-09-13 21:37:47', 'test one', 'this is my test document number one. also checking search within phrases.');
INSERT INTO `documents` VALUES ('2', '1', '6', '2008-09-13 21:37:47', 'test two', 'this is my test document number two');
INSERT INTO `documents` VALUES ('3', '2', '7', '2008-09-13 21:37:47', 'another doc', 'this is another group');
INSERT INTO `documents` VALUES ('4', '2', '8', '2008-09-13 21:37:47', 'doc number four', 'this is to test groups');

-------------------------------------------實際上,這個新建立的表就是 Sphinx 中的 example.sql

我們的測試表已經建立完成,接下來我們要配置 sphinx-doc.conf 文件(重要)

先將 sphinx 下的 sphinx-min.conf 復制一份改名為 sphinx-doc.conf,接著 修改它:

----------------------------------------------------------------------

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
# type----------------------------------------數據庫類型,目前支持 mysql 與 pgsql
# strip_html--------------------------------是否去掉html 標簽
# sql_host----------------------------------數據庫主機地址
# sql_user----------------------------------數據庫用戶名
# sql_pass----------------------------------數據庫密碼
# sql_db-------------------------------------數據庫名稱
# sql_port-----------------------------------數據庫采用的端口
# sql_query_pre--------------------------執行sql前要設置的字符集,用utf8必須SET NAMES utf8
# sql_query---------------------------------全文檢索要顯示的內容,在這里盡可能不使用where或 group by,將 where 與 groupby 的內容交給 sphinx,由 sphinx 進行條件過濾與 groupby 效率會更高
# 注意: select 出來的字段必須至少包括一個唯一主鍵 (ARTICLESID) 以及要全文檢索的字段,你計劃原本在 where 中要用到的字段也要 select 出來
# 這里不用使用orderby
# sql_attr_ 開頭的表示一些屬性字段,你原計劃要用在 where, orderby, groupby 中的字段要在這里定義(# 為自己添加的注釋內容)

#source 數據源名:

source documents
{
type             = mysql
sql_host     = localhost
sql_user      = root
sql_pass     = yourpassword
sql_db         = test
sql_port       = 3306 # optional, default is 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 documents
{
source      = documents

#path   索引記錄存放目錄,如 d:/sphinx/data/cgfinal ,實際存放時會存放在 d:/sphinx/data 目錄,然后創建多個 cgfinal 名稱,不同擴展名的索引文件。
path          = d:/www/sphinx/data/doc
docinfo      = extern
enable_star     = 1

min_word_len     = 3
min_prefix_len     = 0
min_infix_len     = 3
charset_type    = sbcs

# 其他的配置如 min_word_len, charset_type, charset_table, ngrams_chars, ngram_len 這些則是支持中文檢索需要設置的內容。
# 如果檢索的不是中文,則 charset_table, ngrams_chars, min_word_len 就要設置不同的內容,具體官方網站的論壇中有很多,大家可以去搜索看看。
}

# mem_limit 索引使用內存最大限制,根據機器情況而定,默認是32M,太小的會影響索引的性能。
indexer
{
mem_limit     = 32M
}

# 搜索的守護進程配置
# 在進行全文檢索過程中,searchd要先開啟,mysql在全文檢索時才能連接到sphinx,由sphinx進行全文檢索,再將結果返回給mysql
# address 偵聽請求的地址,不設置則偵聽所有地址
# port 偵聽端口
searchd
{
port     = 3312
log       =d:/www/sphinx/logs/searched_doc.log
query_log     = d:/www/sphinx/logs/query_doc.log
read_timeout    = 5
max_children    = 30
pid_file     = d:/www/sphinx/logs/searched-doc.pid
max_matches     = 1000
seamless_rotate    = 0
preopen_indexes    = 0
unlink_old     = 1
}


----------------------------------------------------------------------


為了測試,我們的 Sphinx 配置文件已經寫好,確保我們的 Mysql 數據庫已經啟動,如果沒有啟動則在 cmd 中鍵入" net start mysql "

接下來,我們的測試正式開始:

1,生成數據索引或重建索引:

(最好再復制一個 sphinx-doc.conf 配置文件,并把它放入 bin 文件夾中,下面的舉例 假設我們已經這樣做):

在 cmd 模式下:輸入:

d:/www/sphinx/bin/indexer.exe --config d:/www/sphinx/bin/sphinx-doc.conf documents

2,運行檢索守護進程 searchd.exe:

d:/www/sphinx/bin/searchd.exe --config d:/www/sphinx/bin/sphinx-doc.conf

如過這兩步沒有報錯的話,說明我們的 Sphinx 已經正常運行了!可以通過 netstat -an 查看是否 3312 端口是否處如監聽狀態。

3,現在來用 sphinx 自帶的工具 search.exe 來測試一下:

測試:

索引關鍵字: this is m

D:\www\sphinx\bin>search.exe -c d:/www/sphinx/bin/sphinx-doc.conf this is m

結果:

Sphinx 0.9.8-release (r1371)
Copyright (c) 2001-2008, Andrew Aksyonoff

using config file 'd:/www/sphinx/bin/sphinx-doc.conf'...
WARNING: index 'documents': invalid morphology option 'extern' - IGNORED
index 'documents': query 'this is m ': returned 4 matches of 4 total in 0.000 s
c

displaying matches:
1. document=1, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008
        id=1
        group_id=1
        group_id2=5
        date_added=2008-09-13 21:37:47
        title=test one
        content=this is my test document number one. also checking search withi
phrases.
2. document=2, weight=1, group_id=1, date_added=Sat Sep 13 21:37:47 2008
        id=2
        group_id=1
        group_id2=6
        date_added=2008-09-13 21:37:47
        title=test two
        content=this is my test document number two
3. document=3, weight=1, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=3
        group_id=2
        group_id2=7
        date_added=2008-09-13 21:37:47
        title=another doc
        content=this is another group
4. document=4, weight=1, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=4
        group_id=2
        group_id2=8
        date_added=2008-09-13 21:37:47
        title=doc number four
        content=this is to test groups

words:
1. 'this': 4 documents, 4 hits

-------------------

索引關鍵字: this is another group

D:\www\sphinx\bin>search.exe -c d:/www/sphinx/bin/sphinx-doc.conf this is another group

結果:

Sphinx 0.9.8-release (r1371)
Copyright (c) 2001-2008, Andrew Aksyonoff

-------------------

到此sphinx在win上算正常運行了,sphinx-doc.conf文件配置比較靈活,根據你需要索引的數據庫進行靈活配置來達到你需要的效果

如果配置過程中出現運行參數配置問題可以查看 doc/sphinx.html文件,里面對各種參數都要詳細的說明


using config file 'd:/www/sphinx/bin/sphinx-doc.conf'...
WARNING: index 'documents': invalid morphology option 'extern' - IGNORED
index 'documents': query 'this is another group ': returned 1 matches of 1 total
in 0.000 sec

displaying matches:
1. document=3, weight=4, group_id=2, date_added=Sat Sep 13 21:37:47 2008
        id=3
        group_id=2
        group_id2=7
        date_added=2008-09-13 21:37:47
        title=another doc
        content=this is another group

words:
1. 'this': 4 documents, 4 hits
2. 'another': 1 documents, 2 hits
3. 'group': 1 documents, 1 hits