sphinx从安装到使用

 

Sphinx中重要的三个命令,splinx安装的bin目录下

Indexer创建索引命令,searchd启动进程命令,Search命令搜索命令(新版本中已不存在)

 

下载sphinx并安装

./configure--prefix=/usr/local/sphinx –with-mysql=/usr/local/mysql

make && make install

sphinx.conf 配置文件配置

 

#主数据源修改

source main{

    sql_query_pre            = SET NAMES utf8

    sql_query_pre            = SET SESSIONquery_cache_type=OFF

    sql_query_pre            = replace into sph_counterselect 1,max(id) from post

    sql_query                = select id,title,content from post where id<= 
                               (select max_doc_id from sph_ocunter where counter_id = 1)

}

 

#增量数据源

source delta : main

{

       sql_query_pre = set names utf8

       sql_query= select id,title,content from post where id > 
            (selectmax_doc_id from sph_counter where counter_id = 1)

}

 

#主索引:

index main

{

       source                   = main

       path                    =/usr/local/coreseek/var/data/main

       charset_type             = zh_cn.utf-8

       charset_dictpath          = /usr/local/mmseg/etc/

}

#增量索引

index delta : main

{

       source                  = delta

       path                    =/usr/local/coreseek/var/data/delta

}

 

 

 

把Sphinx集成到php程序中,有两种方式

 

1.Sphinx php模块

2.使用sphinxapi类

我们要使用sphinx需要做以下几件事情

1.      首先得有数据

2.      建立Sphinx配置文件

3.      生成索引

4.      启动Searchd服务进程,开启端口9312

5.      用php客户程序去连接Sphinx服务

 

Php加载sphinx模块

cd sphinx-1.3.3

phpize



cd csft-3.2.14/api/libsphinxclient/

./configure

make && make install



cd /root/sphinx-1.3.3/

./configure--with-php-config=/usr/bin/php-config --with-sphinx



/usr/lib64/php/modules/

#将sphinx.so加入到php.ini中



#php加载sphinx完成

 

 

<?php 

   //include('api/sphinxapi.php');

   $sphinx = new SphinxClient();

         //sphinx的主机名和端口

   $sphinx->setServer('127.0.0.1', 9312);

         //SPH_MATCH_ALL(同义and)

         $sphinx->SetMatchMode(SPH_MATCH_ANY);   

         //设置返回结果集为php数组格式

         $sphinx->SetArrayResult( true );

         //匹配结果的偏移量,参数的意义依次为:起始位置,返回结果条数,最大匹配条数

         $sphinx->SetLimits(0,20, 1000);

         //最大搜索时间

         $sphinx->SetMaxQueryTime(10);

         //执行简单的搜索,这个搜索将会查询所有字段的信息

         $query= $_GET['query'];

   $res = $sphinx->query ($query,'*');

         $ids= array();

     foreach($res['matches'] as $row){

               $ids[]=$row['id'];

     }

         $id_str= implode(',',$ids);

         //$ids= join(',',array_keys($res['matches']));

         mysql_connect('localhost','root','root');

         mysql_select_db('test');

         $sql= "select * from post where id in ({$id_str})";



         mysql_query("setnames utf8");

         $rst= mysql_query($sql);

        

         $opts=array(

                   'before_match'=>'<fontstyle="font-weight:bold;color:red;">',

                   'after_match'=>'</font>'

         );

         while($row= mysql_fetch_assoc($rst)){

                   /*echo"<pre>";

                   print_r($row);

                   echo"</pre>";*/

                   $r= $sphinx->buildExcerpts($row,"main",$query,$opts);

                   echo"第{$r[0]}篇帖子<br/>";

                  echo "标题:{$r[1]}<br/>";

                   echo"内容:{$r[2]}<br/>";

         }

 

 

Sphinx实时索引(上述的sphinx并没有实现实时)

主索引生成(一般在网站的访问量比较少的时候进行)

Main.sh:

/usr/local/coreseek/bin/indexer main--rotate >>/usr/local/coreseek/var/log/main.log

 

增量索引生成(每过5分钟生成一次)

delta.sh:

/usr/local/coreseek/bin/indexer delta--rotate >>/usr/local/coreseek/var/log/delta.log

 

#进入定时器编辑界面

Crontab –e

#每过5分钟生成一次

*/5 * * * */usr/local/coreseek/init/delta.sh

#每天凌晨三点生成一次

00 03 * * */usr/local/coreseek/init/main.sh

#退出编辑器后添加执行的权限

Chmod a+x *

#启动任务计划

Crontab –l

 

Sphinx主要讲解了包括Sphinx、中文分词、高亮、摘要、匹配、增量索引、实时索引和分布式索引方面的知识。

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值