Sphinx.conf 配置文档
<!-- [if !supportLists]-->一. <!-- [endif]-->创建 sphinx.conf 文件
其结构组成主要如下 :
Source 源名称 1{ // 指定数据源
一些配置
}
Index 索引名称 1{
Source= 源名称 1
}
Source 源名称 2{
一些配置
}
Index 索引名称 2{
Source= 源名称 2
}
Indexer{
mem_limit = 32M
}
Searchd{ // 配置 searchd 守护程序本身
}
<!-- [if !supportLists]-->二. <!-- [endif]-->Source 源名称 相关配置说明
Type= 数据库类型 (Mysql 或 SQL);
Sql_host= 数据库主机地址 ( 如果是外网 , 请确保防火墙允许链接 )
Sql_user= 数据库用户名
Sql_pass= 数据库密码
Sql_db= 数据库名称
Sql_port= 数据库端口
Sql_query_pre= 执行 SQL 前设置的编码 (SET NAMES UTF8/GBK)
Sql_query= 全文检索要显示的内容 , 据官方说法 : 尽可能不要使用 WHERE 或 GROUPBY , 将其交给 SPHINX 效率会更高 ;select 出来的字段必须包含至少一个唯一主键 , 以及全文检索的字段
Sql_query_info=
SELECT * FROM Inventory WHERE id=$id 来查找匹配记录 在查询中,$id
被替换为 searchd 返回的每个主键
Strip_html= 0/1 是否去掉 HTML 标签
Sql_attr_uint= 无符号整数属性 , 可以设置多个 , 设置数据库字段 , 设置哪个能显示出哪个字段数据的整形来 .
<!-- [if !supportLists]-->三. <!-- [endif]-->Index 中配置说明
Source= 数据源名称
Path = 索引记录存放目录 , 注 : d:/sphinx/data/cg 这个的意思是 在 data 目录下创建一个名为 cg 的文件
min_word_len= 索引的词的最小长度 设为 1 既可以搜索单个字节搜索 , 越小 索引越精确 , 但建立索引花费的时间越长
charset_type= utf-8/gbk 设置数据编码
charset_table= 字符表和大小写转换规则 . 频繁应用于 sphinx 的分词过程
min_prefix_len = 最小前缀 (0)
min_infix_len = 最小中缀 (1)
ngram_len = 对于非字母型数据的长度切 (1)
<!-- [if !supportLists]-->四. <!-- [endif]-->searchd 配置说明
port= sphinx 的端口 (9312)
log= 服务进程日志存放路径,一旦 sphinx 出现异常,基本上可以从这里查询有效信息
query_log= 客户端查询日志 尤为有用 它将在运行时显示每次搜索并显示结果
read_timeout= 请求超时 (5)
max_children= 同时可执行的最大 searchd 进程数 (30)
pid_file= 进程 ID 文件
max_matches= 查询结果的最大返回数
seamless_rotate= 是否支持无缝切换,做增量索引时通常需要 (0/1)
至此 spninx.conf 配置文件结束 ; 注意 : 如果有换行 必须用反斜杠 / 链接
<!-- [if !supportLists]-->五. <!-- [endif]-->sphinx 建立所以 及监听
切换到 DOS sphinx/bin 目录下
<!-- [if !supportLists]-->1. <!-- [endif]-->建立索引
Indexer –c sphinx.conf 索引名称 /--all (--all 参数是建立所有索引 )
完成后提示如下 :
total 535600 docs, 10707632 bytes
total 34.323 sec, 311958 bytes/sec, 15604.27 docs/sec
total 5 reads, 0.282 sec, 45592.6 kb/call avg, 56.4 msec/call avg
total 547 writes, 12.172 sec, 1017.0 kb/call avg, 22.2 msec/call avg
<!-- [if !supportLists]-->2. <!-- [endif]-->建立完成后
可以执行 search [–c] sphinx.conf 搜索内容 (-c 参数 : 是否允许模糊搜索 )
<!-- [if !supportLists]-->3. <!-- [endif]-->监听端口
Searchd
运行后 提示
listening on all interfaces, port=9312
accepting connections
监听成功后 既可以运行 PHP 程序 进行 搜索
PHPCODE:
require("sphinxapi.php"); // 加载类文件
$cl=new SphinxClient(); // 实例化类
$cl->SetServer('localhost',9312); // 设置服务
$cl->SetArrayResult(true); // 设置 显示结果集方式
$cl->SetLimits(0,10); // 同 sql 语句中的 LIMIT
$cl->SetSortMode(“mode”); // 排序模式 SPH_SORT_ATTR_DESC 和 SPH_SORT_ATTR_ASC
$result=$cl->Query('ff',[ 索引名称可选 ]); // 执行搜索
Var_dump($result); // 输出
注意 : 每次修改 sphinx.conf 后要重建索引 .