php--tp3.2引入sphinx搜索

1.首先我们把coreseek下载好,命名为coreseek,我们找到coreseek/etc中的csft_mysql.conf修改这个配置文件

#源定义
source lemai
{
type                    = mysql

sql_host                = localhost
sql_user                = root
sql_pass                = 123
sql_db                    = lemai
sql_port                = 3306
sql_query_pre            = SET NAMES utf8

sql_query                = SELECT id, title,content,name FROM shop
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
sql_attr_uint            = sid           #从SQL读取到的值必须为整数
sql_attr_timestamp        = time #从SQL读取到的值必须为整数,作为时间属性

sql_query_info_pre      = SET NAMES utf8                                        #命令行查询时,设置正确的字符集
sql_query_info            = SELECT * FROM shop WHERE name=$id #命令行查询时,从数据库读取原始数据信息
}

#index定义
index lemai
{
source            = lemai             #对应的source名称
path            = var/data/mysql #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
docinfo            = extern
mlock            = 0
morphology        = none
min_word_len        = 1
html_strip                = 0

min_infix_len = 1     #搜索英文、数字

#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
#charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
charset_dictpath = C:/AppServ/www/thinkphp/ThinkPHP/Extend/Vendor/Coreseek/etc/                             #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type        = zh_cn.utf-8
#charset_table      =
ngram_len            = 0
}

#全局index定义
indexer
{
mem_limit            = 128M
}

#searchd服务定义
searchd
{
listen                  =   9312
read_timeout        = 5
max_children        = 30
max_matches            = 1000
seamless_rotate        = 0
preopen_indexes        = 0
unlink_old            = 1
pid_file = var/log/searchd_mysql.pid  #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
log = var/log/searchd_mysql.log        #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
query_log = var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...
}

2.然后把coreseek拷贝一份到thinkphp的核心文件Vendor/coreseek下.

3.在cmd命令行创建sphinx索引

indexer.exe -c 配置文件 --all(参数--all 表示创建全部索引

bin\indexer.exe -c etc\csft_mysql.conf --all

启动searchd 服务  searchd.exe -c 配置文件

bin\searchd.exe -c etc\csft_mysql.conf

4.php代码实现

   Vendor("Coreseek.api.sphinxapi"); //其中同样包含 tp3.2中如何引入第三方类
        $s = new \SphinxClient();
        $s->SetServer("127.0.0.1", 9312);
        $s->SetMatchMode(SPH_MATCH_ALL);
        $rs = $s->Query('饭锅',"*");
        echo "<pre>";
        print_r($rs);

 

posted @ 2016-05-13 16:05  不解凡忧  Views(485)  Comments(0Edit  收藏  举报