sphinx

目录

安装

#安装的这个版本不是原版的,是一个针对中文的hack版本
#下载安装
wget http://www.sphinx-search.com/downloads/sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
tar xvf sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
cd sphinx-for-chinese-2.2.1-dev-r4311

./configure --prefix=/usr/local/webserver/sphinx \
--with-mysql=/usr/local/webserver/mysql
make
make install

#安装字典
/usr/local/webserver/sphinx/bin/mkdict xdict_1.1.txt xdict 
cp xdict /usr/local/webserver/sphinx/etc/


配置

vim /usr/local/webserver/sphinx/etc/sphinx.conf

source Title
{
#####数据源类型
type = mysql

######mysql主机
sql_host = 127.0.0.1

########mysql用户名
sql_user = root

############mysql密码
sql_pass =xxxxxxxxxx

#########mysql数据库名
sql_db = xxxxxxxxx

###########mysql端口
sql_port= 3306

###mysql检索编码,特别要注意这点,很多人中文检索不到是数据库的编码是GBK或其他非UTF8
sql_query_pre = SET NAMES UTF8
sql_query_pre = SET SESSION query_cache_type=OFF
sql_query_pre = REPLACE INTO sph_counter SELECT 1, MAX(id) FROM dede_archives

####### 获取数据的sql
sql_query =SELECT arc.id ,arc.typeid as typeid,arc.typeid2,arc.flag,arc.title,arc.keywords,arc.description,arcadd.body,tp.typedir,tp.typename,tp.isdefault,tp.defaultname,tp.namerule,tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath   FROM `dede_archives` arc INNER JOIN `dede_addonarticle` as arcadd ON arc.id=arcadd.aid LEFT JOIN `dede_arctype` tp  ON arc.typeid=tp.id WHERE arc.arcrank>=0 AND FIND_IN_SET('s',arc.flag) AND arc.id>=$start AND arc.id<=$end
sql_attr_uint = typeid
sql_attr_multi=uint nid from query; SELECT id as nid ,title  FROM dede_archives  where arcrank>=0 AND FIND_IN_SET('s',flag)
sql_attr_multi=uint tid from query; SELECT typeid as tid ,title  FROM dede_archives  where arcrank>=0 AND FIND_IN_SET('s',flag)
sql_query_range= SELECT 1,max_doc_id FROM sph_counter WHERE counter_id=1
sql_range_step                  = 2000
sql_ranged_throttle             = 0


############ UNIX时间戳属性
#sql_attr_timestamp = created 

######### 用于命令界面端(CLI)调用的测试
#sql_query_info = select * from sphinx_article where id=$id 
}

### 索引 ###
index title
{
####声明索引源
source = Title

#######索引文件存放路径及索引的文件名
path = /data/data/sphinx/title

##### 文档信息存储方式
docinfo = extern


###缓存数据内存锁定
mlock = 0

#### 形态学(对中文无效)
morphology = none

#### 索引的词最小长度
min_word_len = 1
#####数据编码
charset_type = utf-8

   ##中文字典
   chinese_dictionary = /usr/local/webserver/sphinx/etc/xdict
html_strip=0
}

source Title_new : Title
{
   sql_query_pre = SET NAMES UTF8
   sql_query_pre = SET SESSION query_cache_type=OFF
   sql_query_range = SELECT sph.max_doc_id+1,(SELECT MAX(id) FROM dede_archives) FROM sph_counter AS sph WHERE sph.counter_id=1;
}

index title_new : title
{
        source                  = Title_new
        path = /data/data/sphinx/title_new
}

######### 索引器配置 #####
indexer
{
   mem_limit = 256M ####### 内存限制
}

############ sphinx 服务进程 ########
searchd
{
#listen = 9312 ### 监听端口,在此版本开始,官方已在IANA获得正式授权的9312端口,以前版本默认的是3312

#### 服务进程日志 ,一旦sphinx出现异常,基本上可以从这里查询有效信息,轮换(rotate)出的问题一般可在此寻到答案
#log = /usr/local/sphinx/var/log/searchd.log 

### 客户端查询日志,笔者注:若欲对一些关键词进行统计,可以分析此日志文件
#query_log = /usr/local/sphinx/var/log/query.log 

## 请求超时
read_timeout = 5

### 同时可执行的最大searchd 进程数
max_children = 30

#######进程ID文件
pid_file = /usr/local/webserver/sphinx/var/log/searchd.pid

### 查询结果的最大返回数
max_matches = 1000

### 是否支持无缝切换,做增量索引时通常需要
seamless_rotate = 1

}

生成索引

/usr/local/webserver/sphinx/bin/indexer --rotate --config /usr/local/webserver/sphinx/etc/sphinx.conf title

启动服务

/usr/local/webserver/sphinx/bin/searchd -c /usr/local/webserver/sphinx/etc/sphinx.conf

测试

/usr/local/webserver/sphinx/bin/search -c /usr/local/webserver/sphinx/etc/sphinx.conf --index title 凡人修仙传

目录