Sphinx

首先我们要从 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 


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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值