
在thinkphp下怎么使用coreseek
展开全部
首先我们把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
#中文分词配置,详情请查看: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/...
}
然后把coreseek拷贝一份到thinkphp的核心文件Extend/Vendor下.
打开cmd cd到coreseek
bin\indexer -c etc\csft_mysql.conf (mysql,等)数据库名 创建索引
创建完之后我们可以在var\data下看到一堆文件,此时说明创建成功
bin\searchd -c etc\csft_mysql.conf --console 启动进程
(检查端口9312是否有这个进程,有就OK)
命令行查询
echo 一号店 |iconv -f gbk -t utf-8 | search -c etc\csft_mysql.conf --stdin | iconv -f utf-8 -t gbk 中文索引查询
然后就可以在控制器中进行PHP连接sphinxapi进行测试了
Vendor('Coreseek.api.sphinxapi');
//加载第三方扩展包的文件 文件名不包含class
$db = new PDO('mysql:host=localhost;port=3306;dbname=lemai', 'root', '123', array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'));
//实例化PDO
$spx = new SphinxClient();
//实例化SphinxClient
$spx -> SetServer('127.0.0.1',9312);
$spx->SetConnectTimeout ( 3 );
$spx->SetArrayResult ( true );
$spx -> SetMatchMode(SPH_MATCH_ANY);
$result = $spx -> query('1','*');
$ids = join(",",array_keys($result['matches']));
$sql = "SELECT * FROM shop where id in ({$ids})";
$stmt = $db->query($sql);
$r = $stmt->FETCHALL(PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($r);
以下是我的实例代码 包括sphinx分页
class IndexAction extends Action {
public function index(){
header("Content-type:text/html;charset=utf-8");
//设置字符集
$keyword = $_GET['kw'];
Vendor('Coreseek.api.sphinxapi');
//加载第三方扩展包的文件 文件名不包含class
$db = M();
$spx = new SphinxClient();
//实例化SphinxClient
$spx -> SetServer('127.0.0.1',9312);
//设置ip和端口
$spx->SetConnectTimeout ( 3 );
//设置超时时间
$spx->SetArrayResult ( true );
if(strlen($keyword)>=18){
$spx -> SetMatchMode(SPH_MATCH_ALL);//如果用户查询字符大于=18个匹配有查询词
}else{
$spx -> SetMatchMode(SPH_MATCH_ANY);//匹配查询词中的任意一个
}
//------
$limit = 12;//每页要显示的数量
$page = $_GET['page']>1 ? $_GET['page'] : 1;
//GET值不为1 则按1算
//$spx->setLimits(0,12);
$off = ($page-1)*$limit;
$spx->SetLimits( $off, $limit);
//设置分页
$result = $spx -> query("{$keyword}",'*');
/*
* 取出matches中的id,组成字符串
*/
$str = '';
foreach($result['matches'] as $rrs){
$str.=$rrs['id'].',';
}
$ids = rtrim($str,',');
//操作数据库
$sql = "SELECT * FROM shop where id in ({$ids})";
$stmt = $db->query($sql);
$opts = array(
"before_match"=>"<span style='color:red'>",//添加样式
"after_match"=>"</span>"
);
foreach($stmt as $st){
$shop_all[] = $spx->buildExcerpts($st,'lemai',$keyword,$opts);
}
$num = $result['total'];
$count =ceil($result['total']/12);//查出sphinx搜索总数 得出该关键词分页数
$pagenum = $_GET['page']>1?$_GET['page']:1;
$this->assign('pagenum',$pagenum);
$this->assign('num',$num);
$this->assign('count',$count);
$this->assign('shop_all',$shop_all);
$this->assign('keyword',$keyword);
$this->display("seek");
}
}
#源定义
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
#中文分词配置,详情请查看: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/...
}
然后把coreseek拷贝一份到thinkphp的核心文件Extend/Vendor下.
打开cmd cd到coreseek
bin\indexer -c etc\csft_mysql.conf (mysql,等)数据库名 创建索引
创建完之后我们可以在var\data下看到一堆文件,此时说明创建成功
bin\searchd -c etc\csft_mysql.conf --console 启动进程
(检查端口9312是否有这个进程,有就OK)
命令行查询
echo 一号店 |iconv -f gbk -t utf-8 | search -c etc\csft_mysql.conf --stdin | iconv -f utf-8 -t gbk 中文索引查询
然后就可以在控制器中进行PHP连接sphinxapi进行测试了
Vendor('Coreseek.api.sphinxapi');
//加载第三方扩展包的文件 文件名不包含class
$db = new PDO('mysql:host=localhost;port=3306;dbname=lemai', 'root', '123', array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'));
//实例化PDO
$spx = new SphinxClient();
//实例化SphinxClient
$spx -> SetServer('127.0.0.1',9312);
$spx->SetConnectTimeout ( 3 );
$spx->SetArrayResult ( true );
$spx -> SetMatchMode(SPH_MATCH_ANY);
$result = $spx -> query('1','*');
$ids = join(",",array_keys($result['matches']));
$sql = "SELECT * FROM shop where id in ({$ids})";
$stmt = $db->query($sql);
$r = $stmt->FETCHALL(PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($r);
以下是我的实例代码 包括sphinx分页
class IndexAction extends Action {
public function index(){
header("Content-type:text/html;charset=utf-8");
//设置字符集
$keyword = $_GET['kw'];
Vendor('Coreseek.api.sphinxapi');
//加载第三方扩展包的文件 文件名不包含class
$db = M();
$spx = new SphinxClient();
//实例化SphinxClient
$spx -> SetServer('127.0.0.1',9312);
//设置ip和端口
$spx->SetConnectTimeout ( 3 );
//设置超时时间
$spx->SetArrayResult ( true );
if(strlen($keyword)>=18){
$spx -> SetMatchMode(SPH_MATCH_ALL);//如果用户查询字符大于=18个匹配有查询词
}else{
$spx -> SetMatchMode(SPH_MATCH_ANY);//匹配查询词中的任意一个
}
//------
$limit = 12;//每页要显示的数量
$page = $_GET['page']>1 ? $_GET['page'] : 1;
//GET值不为1 则按1算
//$spx->setLimits(0,12);
$off = ($page-1)*$limit;
$spx->SetLimits( $off, $limit);
//设置分页
$result = $spx -> query("{$keyword}",'*');
/*
* 取出matches中的id,组成字符串
*/
$str = '';
foreach($result['matches'] as $rrs){
$str.=$rrs['id'].',';
}
$ids = rtrim($str,',');
//操作数据库
$sql = "SELECT * FROM shop where id in ({$ids})";
$stmt = $db->query($sql);
$opts = array(
"before_match"=>"<span style='color:red'>",//添加样式
"after_match"=>"</span>"
);
foreach($stmt as $st){
$shop_all[] = $spx->buildExcerpts($st,'lemai',$keyword,$opts);
}
$num = $result['total'];
$count =ceil($result['total']/12);//查出sphinx搜索总数 得出该关键词分页数
$pagenum = $_GET['page']>1?$_GET['page']:1;
$this->assign('pagenum',$pagenum);
$this->assign('num',$num);
$this->assign('count',$count);
$this->assign('shop_all',$shop_all);
$this->assign('keyword',$keyword);
$this->display("seek");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询