一、使用sphinx的API来操做sphinx,PHP中将API编译到PHP中做为扩展php
二、使用mysql的sphinx的存储引擎html
sphinx是英文的全文检索引擎,coreseek是支持中文词库的全文检索引擎,Lucene是用java实现的全文检索引擎。java
使用sphinx搜索引擎对数据作索引,数据一次性加载进来后保存在内存中,用户在进行搜索的时候只须要在sphinx服务器上检索数据便可。整个流程是:Indexer程序到数据库里面提取数据,对数据进行分词,而后根据生成的分词生成单个或多个索引,并将它们传递给searchd程序,而后客户端能够经过API调用进行搜索。mysql
流程图解释:nginx
Database:数据源,是sphinx作索引的数据来源。算法
Indexer:索引程序,从数据源中获取数据,并将数据生成全文索引。根据需求按期运行Indexer达到定时更新索引的需求。sql
## sphinx使用配置文件从数据库读出数据以后,就将数据传递给Indexer程序,而后Indexer会逐条读取记录,根据分词算法对每条记录创建索引,分词算法能够是一元分词或mmseg分词。数据库
Searchd:Searchd直接与客户端程序进行对话,并使用Indexer程序构建好的索引来快速地处理搜索查询。vim
App客户端:接收来自用户输入的搜索字符串,发送查询给searchd程序并显示返回结果。api
# 到sphinx官网上下载源码文件:http://sphinxsearch.com/files/sphinx-2.2.10-release.tar.gz
[root@localhost ~]
`# cd /usr/local/src`
[root@localhost ~]
`# tar -zxvf sphinx-2.2.10-release.tar.gz`
[root@localhost ~]
`# cd sphinx-2.2.10-release`
[root@localhost sphinx-2.2.10-release]
`# ./configure --prefix=/usr/local/sphinx --with-mysql`
[root@localhost sphinx-2.2.10-release]
`# make && make install`
# libsphinxclient安装(PHP模块须要)
[root@localhost sphinx-2.2.10-release]
`# cd api/libsphinxclient`
[root@localhost libsphinxclient]
`# ./configure --prefix=/usr/local/sphinx`
[root@localhost libsphinxclient]
`# make && make install`
安装PHP的sphinx模块
#下载sphinx扩展包:http://pecl.php.Net/package/sphinx
[root@localhost src]
`# tar -zxvf sphinx-1.3.3.tgz`
[root@localhost src]
`# cd sphinx-1.3.3`
[root@localhost sphinx-1.3.3]
`# phpize`
[root@localhost sphinx-1.3.3]
`# ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinx/`
[root@localhost sphinx-1.3.3]
`# make && make install`
# 安装成功:
Installing shared extensions:
`/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/`
# 编辑php.ini
[root@localhost sphinx-1.3.3]
`# vim /usr/local/php/etc/php.ini`
添加:extension=sphinx.so
# 重启nginx服务器
[root@localhost ~]
`# vim /etc/ld.so.conf`
# 添加以下内容:
/usr/local/mysql/lib
[root@localhost ~]
`# ldconfig # 使命令生效`
# sphinx能够定义多个索引与数据源,不一样的索引与数据源能够应用到不一样表或不一样应用的全文检索。
## 数据源 src1
source
`src1`
{
## 说明数据源类型,数据源类型能够是:mysql、mssql、odbc等等
type
= mysql
## 下面是sql数据库特有的端口、用户名、密码数据库名等。
sql_host = localhost
sql_user = root
sql_pass = root
sql_db =
`test`
sql_port = 3306
## 执行sql前的操做,设置mysql检索编码
sql_query_pre = SET NAMES UTF8
## 全文索引要显示的内容(尽量不使用where、group by,将其的内容交给sphinx)
## select字段中必须包含一个惟一主键以及要全文检索的字段,where中要用到的字段也要select出来,sphinx使用此语句从数据库中拉取数据。
sql_query = SELECT
`id`, name from tablename
## 如下是用来过滤或条件查询的属性
## 当数据源过大时屡次查询操做
sql_query_range = SELECT MIN(
`id) , MAX(
id) FROM documents
## 获取最大和最小id,根据步长来获取数据`
sql_range_step = 1000
`## 查询的步长`
sql_ranged_throttle = 0
`## 设置分次查询的时间间隔,单位是毫秒`
## 如下都是不一样属性的数据(属性字段),属性时存在索引中,它不进行全文索引,只能够用于过滤和排序
## 在where、orderby、groupby中出现的字段要分别定义一个属性(以sql_attr_开头),定义不一样类型的字段要用不一样的属性名。
sql_attr_uint = cat_id
`## 无符号整数类型`
sql_attr_unit = member_id
sql_attr_timestamp = add_time
`## unix时间戳`
## 用于命令行界面调用测试
sql_query_info =
`select* from tablename where
id=$
id`
}
## 索引
index test1
{
source
`= src1 `## 声明索引源
path =
`/usr/local/sphinx/var/data/test1
## 索引文件存放路径及索引的文件名`
## mmseg分词 ##
##charset_dictpath = /usr/local/mmseg3/etc ## 指定分词读取词典文件的目录,目录下必须有uni.lib词典,当启用分词发时须要填
## charset_type = zh_ch.utf-8 ## 设置数据编码 utf-8/gbk
## 一元分词 ##
#charset_type = utf-8 ## 新的sphinx不支持charset_type设置
charset_table =
`## 字符表和大小写转换规则`
ngram_chars =
`## 要进行一元字符切分模式承认的有效字符集`
ngram_len = 1
`## 分词长度`
}
## 索引器配置
indexer
{
mem_limit = 256
`## 内存限制`
}
## sphinx服务进程
searchd
{
listen = 9312
`## 监听端口`
listen = 9306:mysql41
log =
`/usr/local/sphinx/var/log/searchd.log
## 服务进程日志`
query_log =
`/usr/local/sphinx/var/log/query.log
## 客户端查询日志`
read_time = 5
`## 请求超时`
max_children = 30
`## 同时可执行的最大searchd进程数`
pid_file =
`/usr/local/sphinx/var/log/searchd.pid
## 进程id文件`
max_matches = 1000
`## 查询结果的最大返回数`
seamless_rotate = 1
`## 启动无缝轮转`
}
调用indexer程序生成所有索引:
[root@localhost ~]
`# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all`
指定某个数据源生成索引:
[root@localhost ~]
`# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf 索引名(配置文件中所定义的)`
若是此时searchd守护进程已经启动,须要加上--rotate参数:
[root@localhost ~]
`# /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all --rotate`
[root@localhost ~]
`# /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf`
一、new SphinxClient (); ## 建立sphinx的客户端接口对象
二、SetServer( host , port ); ## 设置链接sphinx主机与端口
三、SetMatchMode( mode ); ## 设置全文查询的匹配模式,mode为搜索模式
四、SetFilter( string $attribute , array $values [ , bool $exclude = false ] ) ## 增长整数型过滤器
string $attribute 属性名称
array $values 整数值数组
bool $exclude 匹配该过滤规则的文档是否会被排除在结果以外
五、SetSortMode( int mode [ , string $sortby ] ) ## 设置匹配排序模式
六、SetLimits( int $offset , int $limit ) ## 设置返回结果集偏移量和数目
七、Query( string $query \[ , string $index='*' ] ) ## 执行搜索查询
string $query 查询的字符串
string $index 索引名称,能够是多个,用逗号分割或者为'*'表示所有索引
返回的数据结构:
键
值说明
"matches"
存储文档id以及其对应的另外一个包含文档权重和属性值得hash表
"total"
此查询在服务器检索所获得的匹配文档总数(即服务器端结果集的大小,且与相关设置有关)
"total_found"
索引中匹配文档的总数
"words"
将查询关键词(关键词通过大小写转换,取词干和其余处理)映射到一个包含关于关键字的统计数据。'docs'在多少文档中出现,'hits'一共出现了多少次。
"error"
searchd报告的错误信息
"warning"
searchd报告的警告信息
八、buildExcerpts( array $docs , string $index , string $words \[ , array $opts ] ) ## 高亮关键字文本片断,能够用于实现摘要的功能
array $docs 文档内容字符串数组
string $index 检索名称
string $words 要高亮的关键词
array $opts 关联数组的附加突出选项
索引创建构成:一、固定不变的主索引。二、增量索引重建。三、索引数据的合并。
在实际操做中,须要为增量索引的创建建立辅助表,这样才能够记住最后创建索引的记录id来作实际的增量部分的索引创建。
1)建立辅助表:CREATE TABLE `sph_counter` (`counter_id` int(11) NOT NULL COMMENT `标识不一样的数据表`,`max_doc_id` int(11) NOT NULL COMMENT `每一个索引表的最大ID,会实时更新`,PRIMARY KEY (`counter_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8
2)在主索引的数据源中,在sql_query的查询语句中,增长where条件语句(WHERE id<=( SELECT max_doc_id FROM sph_counter WHERE counter_id = 1 ))
3)在增量索引的数据源中,继承主索引数据源,在sql_query的查询语句中,增长where条件语句,获取主索引中没有的数据(WHERE id > ( SELECT max_doc_id FROM sph_counter WHERE counter_id = 1 ))
4)分别配置主索引和增量索引的index定义配置。
生成主索引,可添加crontab,定时重建主索引:
/usr/local/sphinx/bin/indexer
`--config/usr/local/sphinx/etc/sphinx
.conf --rotate test1`
# 生成增量索引而且合并,可添加到crontab任务中每隔一段时间执行一次:
/usr/local/sphinx/bin/indexer
`--config/usr/local/sphinx/etc/sphinx
.conf --rotate delta`
/usr/local/sphinx/bin/indexer
`--config/usr/local/sphinx/etc/sphinx
.conf --merge test1 delta --rotate`