[centos]配置coreseek

[1] 复制配置文件到coreseek的etc

cp /data/src/coreseek-3.2.14/testpack/etc/csft.conf /usr/local/coreseek/etc/csft.conf

 

[2] 修改配置文件

source src1{
        type 		= mysql
        sql_host 	= localhost
        sql_user 	= root
        sql_pass 	= 
        sql_db 		= test
        sql_port 	= 3306
        sql_query_pre 		= SET NAMES utf8
        sql_query 			= SELECT id,title,content,create_time FROM ps_news
        sql_attr_uint 		= id
        sql_attr_timestamp  = create_time
        sql_query_info_pre  = SET NAMES utf8
}

index src1{
        source = src1
        path 			= /usr/local/coreseek/var/data/src1/
        docinfo 		= extern
        mlock 			= 0
        morphology 		= none
        min_word_len 	= 1
        html_strip 		= 0

        charset_type 		= zh_cn.utf-8
        charset_dictpath 	= /usr/local/mmseg3/etc/
        #charset-table 		= 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
        #ngram_len 			= 1
        #ngram_chars 		= U+3000..U+2FA1F
}

#global indexer
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			= /usr/local/coreseek/var/log/searchd_rtindex.pid
    log 				= /usr/local/coreseek/var/log/searchd_rindex.log       
    query_log 			= /usr/local/coreseek/var/log/query_rtindex.log
}

     a. source是配置数据源,按照提示输入MYSQL的主机、帐号、密码和数据库即可,我的MYSQL就安装在本机上(MYSQL的安装可自行百度)

  b. sql_query_pre是在执行查询之前执行的SQL语句。(注意:在coreseek只能识别utf8字符集编码,所以我们要执行转换一下)

  c. sql_query是要查询进行索引的SQL语句,sql_attr_unit和sql_attr_timestamp是设置属性的,属性在全文检索中可以用来设置过滤和排序。

  d.  index和source应该是成对出现,index就是配置索引的功能(我们还可以配置多个索引 主索引+增量索引的功能)

  e. searchd是常驻进程的全文检索服务,默认监控本机的9312端口

  f. charset_type和charset_dictpath是中文分词配置

4. 创建索引和测试全文检索

  上面我们配置了MYSQL数据源,我们要做到是test库中的documents表进行全文检索,为了测试全文检索的效率,我批量插入了该表30W的数据,以下我们就对该表进行索引测试和全文检索测试。

[3] 测试indexer索引

$ cd /usr/local/coreseek/
$  /usr/local/coreseek/bin/indexer -c etc/csft.conf
##以下为正常情况下的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
    total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

 

[4] 创建sphinx索引文件

$  /usr/local/coreseek/bin/indexer -c etc/csft.conf --all
##以下为正常索引全部数据时的提示信息:(csft-4.0版类似)
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    indexing index 'xml'...
    collected 3 docs, 0.0 MB
    sorted 0.0 Mhits, 100.0% done
    total 3 docs, 7585 bytes
    total 0.075 sec, 101043 bytes/sec, 39.96 docs/sec
    total 2 reads, 0.000 sec, 5.6 kb/call avg, 0.0 msec/call avg
    total 7 writes, 0.000 sec, 3.9 kb/call avg, 0.0 msec/call avg

   a.如果出现这样的错误

ERROR: index 'src1': sql_connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (DSN=mysql://root:***@localhost:3306/test).

   b.解决方法

ERROR: index 'src1': sql_connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (DSN=mysql://root:***@localhost:3306/test).

#注:如果不存在/var/lib/mysql/mysql.sock文件,则可以用:
#ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock即可

 

[5]使用search程序测试全文检索(注意是search不是searchd)

 

[root@kewen coreseek]# /usr/local/coreseek/bin/search -c etc/csft.conf 
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

 using config file 'etc/csft.conf'...
index 'src1': query '': returned 1000 matches of 1245184 total in 0.046 sec

displaying matches:
1. document=1, weight=1, create_time=Fri Jan  3 23:36:17 2014
2. document=2, weight=1, create_time=Fri Jan  3 23:36:19 2014

....

[root@kewen coreseek]# /usr/local/coreseek/bin/search -c etc/csft.conf -a title1
Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

 using config file 'etc/csft.conf'...
index 'src1': query 'title1 ': returned 1000 matches of 1245184 total in 0.161 sec

displaying matches:
1. document=1, weight=1, create_time=Fri Jan  3 23:36:17 2014
2. document=2, weight=1, create_time=Fri Jan  3 23:36:19 2014
3. document=3, weight=1, create_time=Fri Jan  3 23:36:37 2014
4. document=4, weight=1, create_time=Fri Jan  3 23:36:38 2014
5. document=5, weight=1, create_time=Fri Jan  3 23:36:38 2014
6. document=6, weight=1, create_time=Fri Jan  3 23:36:38 2014
7. document=7, weight=1, create_time=Fri Jan  3 23:36:38 2014
8. document=8, weight=1, create_time=Fri Jan  3 23:36:38 2014
9. document=9, weight=1, create_time=Fri Jan  3 23:36:39 2014

...

 

 [6] 开启searchd服务

 

开启searchd服务,使能用API程序进行调用(注意:你需要配置你的防火墙或者关闭,和关闭selinux)

- service iptables stop #暂时关闭防火墙
- setenforce 0  # 暂时关闭selinux

$  /usr/local/coreseek/bin/searchd -c etc/csft.conf
##以下为正常开启搜索服务时的提示信息:(csft-4.0版类似)
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

    using config file 'etc/csft.conf'...
    listening on all interfaces, port=9312
##如要停止搜索服务,请使用/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop
##如要已启动服务,要更新索引,请使用/usr/local/coreseek/bin/indexer -c etc/csft.conf --all --rotate

 

[7] 测试代码

 

<?php
error_reporting(E_ALL ^ E_NOTICE);
header("Content-type: text/html; charset=utf-8"); 

require ( "sphinxapi.php" );
define(INDEX_SRC1, 'src1');

$cl = new SphinxClient();
$cl->SetServer("localhost",9312);
$cl->SetArrayResult(true);

$keyword = "标题";
$result = $cl->Query($keyword, INDEX_SRC1);

echo '<pre>';
print_r($result);

//处理$result
$matches = isset($result['matches']) ? $result['matches'] : '';
if(is_array($matches)){
    foreach ($matches as $v){
        $ids[] = $v['id'];
    }
}else{
    print("<pre>");    
    print $cl->GetLastError();
    print $cl->GetLastWarning();
    //print "没找到了亲~";
    print("<pre>");
    return;
}
$ids = implode(',', $ids);


//拿着id ,拉库
$link = mysql_connect('localhost','root','') or die('mysql link fail!');
mysql_select_db('test',$link);
mysql_query("SET NAMES UTF8");

$sql = "select * from ps_news where id in({$ids})";
$result = mysql_query($sql,$link);

$data = array();
while ($row = mysql_fetch_assoc($result))
{
       $data[] = $row;
}
mysql_close($link);


// 关键字高亮
$p_titles = array();
$p_contents = array();

$build_opts= array(//查询结果集设置
    'before_match'=> '<font color="red">',
    'after_match'=> '</font>',
    'limit'=> '130',
    //'exact_phrase'=> true,
    'single_passage'=> true,
    'chunk_separator' => '',
);

foreach($data as $key=>$value)
{
    $p_titles[$key] = $value['title'];
    $p_contents[$key] = $value['content'];
}

$p_titles   = $cl->BuildExcerpts($p_titles, INDEX_SRC1, $keyword, $build_opts);
$p_contents = $cl->BuildExcerpts($p_contents, INDEX_SRC1, $keyword, $build_opts);

foreach($data as $key=>$post)
{
    $data[$key]['title']  = $p_titles[$key];
    $data[$key]['content'] = $p_contents[$key];
}

var_dump($data);
?>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值