设为首页 加入收藏

TOP

coreseek (sphinx)+ Mysql + Thinkphp搭建中文搜索引擎详解(三)
2017-10-10 12:01:20 】 浏览:4433
Tags:coreseek sphinx Mysql Thinkphp 搭建 中文 搜索引擎 详解
ngle-0-query--'); #[coreseek]关键字,[mysql]数据源source $err = $s->GetLastError(); echo '<pre>'; var_dump($res); var_dump($res['matches']); var_export($err); echo '</pre>';

php5 test.php

运行结果:matches为匹配后的结果集

 

 五, Coreseek在Thinkphp中的使用   

 1,Sphinx扩展安装安装

Coreseek官方教程中建议php使用直接include一个php文件进行操作,事实上php有独立的sphinx模块可以直接操作coreseek(coreseek就是sphinx!)已经进入了php的官方函数库,而且效率更高!但php模块依赖于libsphinxclient包。我是按照以下这篇文章的步骤安装了Sphinx扩展。

感谢:http://blog.csdn.net/e421083458/article/details/21529969

[第一步] 安装依赖libsphinxclient

# cd /var/install/coreseek-4.1-beta/csft-4.1/api/libsphinxclient/
# ./configure  --prefix=/usr/local/sphinxclient

configure: creating ./config.status
config.status: creating Makefile
config.status: error: cannot find input file: Makefile.in   #报错configure失败    

//处理configure报错
编译过程中报了一个config.status: error: cannot find input file: src/Makefile.in这个的错误,然后运行下列指令再次编译就能通过了:
# aclocal
# libtoolize --force
# automake --add-missing
# autoconf
# autoheader
# make clean

//从新configure编译
# ./configure

# make && make install

[第二步] 安装sphinx的PHP扩展

http://pecl.php.net/package/sphinx
# wget http://pecl.php.net/get/sphinx-1.3.0.tgz
# tar zxvf sphinx-1.3.0.tgz
# cd sphinx-1.3.0
# phpize
# ./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient
# make && make install
# cd /etc/php.d/
# cp gd.ini  sphinx.ini
# vi sphinx.ini

extension=sphinx.so

# service php-fpm restart

 安装完PHP的Sphinx扩展后,就可以直接使用$coreseek = new SphinxClient();而无需引入源文件了。

简单说一下我在TP里使用coreseek查询,并高亮关键词的思路:

1,通过sphinx查出id,uid的集合
2,然后$sql = "select * from post where id in($ids)";$res = mysql_query($sql);获取到数据库的真实数据
3,用BuildExcerpts将title和data的关键字高亮,然后分页展示

关键代码:

     $where = array();
        $where['uid']=$uid;
        if(!empty($search)){    //有需要查找的内容,则去 coreseek 忠查出对应的id
            $coreseek = new \SphinxClient();
            $coreseek->setServer("127.0.0.1", 9312);
            //SPH_MATCH_ALL, 匹配所有查询词(默认模式); SPH_MATCH_ANY, 匹配查询词中的任意一个; SPH_MATCH_EXTENDED2, 支持特殊运算符查询
            $coreseek->setMatchMode(SPH_MATCH_ALL);
            $coreseek->setMaxQueryTime(30);                        //设置最大搜索时间
            $coreseek->SetArrayResult(false);                    //是否将Matches的key用ID代替
            $coreseek->SetSelect ( "*" );                        //设置返回信息的内容,等同于SQL
            $coreseek->SetLimits ( 0, 30, 1000, 0 );            //设置结果集偏移量  SetLimits
            $res = $coreseek->query($search,'mysql','--single-0-query--');
            $key = array_keys($res['matches']);
            $where['id']=array('in',$key);
            $coreseek->close();
        }else{
            
        }
        //获取总数据条数
        $total=$mod->where($where)->count();

高亮的关键代码:

     if(!empty($search)){
            $page->parameter['search']=$search;
            //代码高亮
            $opt = array("before_match"=>"<font style='font-weight:bold;color:#f00'>","after_match"=>"</font>");
            $coreseek1 = new \SphinxClient();
            $coreseek1->setServer("127.0.0.1", 9312);
            $coreseek1->SetMatchMode(SPH_MATCH_ALL);
            $i=0;
            $tags_title=array();
            foreach($info as $key=>$row){
                $tags_title[]=$row['title'];
            }
            $replace_title=$coreseek1->BuildExcerpts($tags_title,'mysql',$search,$opt);
            foreach($info as $key=>
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHP图形操作之生成图像验证码 下一篇Netbeans 8.2关于PHP的新特性

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目