zoukankan      html  css  js  c++  java
  • sphinx中文版Coreseek中文检索引擎安装和使用方法(Linux)

      众所周知,在MYSQL数据库中,如果你在百万级别数据库中使用 like 的话那你一定在那骂娘,coreseek是一个针对于中文检索方案的一种全文检索技术,基于sphinx开发的。但是在coreseek中不但支持了mysql数据源,还支持了python、xml、mssql、odbc。而且提供了很多语言PHP、C#、JAVA、python等丰富API接口。在中文全文搜索引擎中,基本没有什么能有coreseek匹敌的(是我太深入了嘛-^-),在千万条数据测试下,coreseek生成索引后全文检索的时间不会超过0.5s,这个速度是非常可观的。

      在论坛如discuz(discuz后台可配置coreseek)、电商网站(shopex)都是支持配置全文检索的。下面我们就来全程安装一下coreseek全文检索引擎。我使用的系统是 centos5.5。

    1. 安装必要的编译工作支持

      安装coreseek之前需要安装这些工具,当然使用yum安装你的机子需要先保证已经联网

    yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expat-devel

    2. 下载coreseek和编译安装

    $ wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz
    $ tar xzvf coreseek-3.2.14.tar.gz
    $ cd coreseek-3.2.14
    
    ##安装mmseg中文分词
    $ cd mmseg-3.2.14
    $ ./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决
    $ ./configure --prefix=/usr/local/mmseg3
    $ make && make install
    $ cd ..
    
    ##安装coreseek
    $ cd csft-3.2.14
    $ sh buildconf.sh    #输出的warning信息可以忽略,如果出现error则需要解决
    $ ./configure --prefix=/usr/local/coreseek  --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql    ##如果提示mysql问题,可以查看MySQL数据源安装说明
    $ make && make install
    $ cd ..

    3. 配置MYSQL数据源

    vi /usr/local/coreseek/etc/csft.conf

     摘录我的MYSQL数据源配置文件

    source src1{
            type = mysql
            sql_host = localhost
            sql_user = root
            sql_pass = 123456
            sql_db = test
            sql_port = 3306
            sql_query_pre = SET NAMES utf8
            sql_query = SELECT id,group_id,author_id,UNIX_TIMESTAMP(date_added) as date_added,title,content FROM documents
            sql_attr_uint = author_id
            sql_attr_uint = group_id
            sql_attr_timestamp = date_added
            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
    }
    
    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的数据,以下我们就对该表进行索引测试和全文检索测试。

      a. 测试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

      b. 创建sphinx索引文件(30W的数据我创建花了2分钟左右,索引文件大概200MB)

    $  /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

      c. 使用search程序测试全文检索(注意是search不是searchd)

    $  /usr/local/coreseek/bin/search -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'...
        index 'xml': query '': returned 3 matches of 3 total in 0.093 sec
    
        displaying matches:
        1. document=1, weight=1, published=Thu Apr  1 22:20:07 2010, author_id=1
        2. document=2, weight=1, published=Thu Apr  1 23:25:48 2010, author_id=1
        3. document=3, weight=1, published=Thu Apr  1 12:01:00 2010, author_id=2
    
        words:
    
    
    $  /usr/local/coreseek/bin/search -c etc/csft.conf -a Twittter和Opera都提供了搜索服务
    ##以下为正常测试搜索关键词时的提示信息:(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'...
        index 'xml': query 'Twittter和Opera都提供了搜索服务 ': returned 3 matches of 3 total in 0.038 sec
    
        displaying matches:
        1. document=3, weight=24, published=Thu Apr  1 12:01:00 2010, author_id=2
        2. document=1, weight=4, published=Thu Apr  1 22:20:07 2010, author_id=1
        3. document=2, weight=3, published=Thu Apr  1 23:25:48 2010, author_id=1
    
        words:
        1. 'twittter': 1 documents, 3 hits
        2. '和': 3 documents, 15 hits
        3. 'opera': 1 documents, 25 hits
        4. '都': 2 documents, 4 hits
        5. '提供': 0 documents, 0 hits
        6. '了': 3 documents, 18 hits
        7. '搜索': 2 documents, 5 hits
        8. '服务': 1 documents, 1 hits

      d. 开启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

    5. 使用PHP API调用coreseek

      复制api/sphinxapi.php文件到你的项目,以下是我的一个PHP测试脚本程序:

    <?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("10.2.4.15",9312);
    $cl->SetArrayResult(true);
    
    $keyword = "媒体";
    
    $result = $cl->Query($keyword, INDEX_SRC1);
    
    //处理$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','123456') or die('mysql link fail!');
    mysql_select_db('test',$link);
    mysql_query("SET NAMES UTF8");
    
    $sql = "select * from documents 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);
    ?>

    6. 设置coreseek自动启动

      在/etc/rc.d/rc.local文件中加入以下行

    /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf

      程序启动你的服务器,使用命令 ps -aux查看是否已经启动成功了。

    7. 中文分词mmseg的一些用法

       官方文档:http://www.coreseek.cn/opensource/mmseg/#mmseg_ini

      LibMMSeg 是Coreseek.com为 Sphinx 全文搜索引擎设计的中文分词软件包,其在GPL协议下发行的中文分词法,采用Chih-Hao Tsai的MMSEG算法

      词典的构造

      mmseg -u unigram.txt

      该命令执行后,将会在unigram.txt所在目录中产生一个名为unigram.txt.uni的文件,将该文件改名为uni.lib,完成词典的构造。需要注意的是,unigram.txt需要预先准备,并且编码格式必须为UTF-8编码

      mmseg -d <dict_dir> tobe_segment.txt

      其中,命令使用‘-d’开关指定词库文件所在的位置,参数dict_dir为词库文件(uni.lib )所在的目录;tobe_segment.txt 为待切分的文本文件,必须为UTF-8编码。如果一切正确,mmseg会将切分结果以及所花费的时间显示到标准输出上。

      对特殊短语的支持

        由于LibMMSeg是为Sphinx全文搜索引擎设计的,因此其内置了部分搜索引擎切分算法的特性,主要表现在对特殊短语的支持上。

      在搜索引擎中,需要处理C++时,如果分词器中没有词组C++,则将被切分为C/x +/x +/x,在进一步的检索中,可能每个词会由于出现的过于频繁而被过滤掉,导致搜索的结果与  C++相关度不高不说,也严重影响的全文搜索的速度。在LibMMSeg中,内置对特殊短语的支持。

      其输入文件格式如下

      // test commit 
      .net => dotnet
      c# => csharp
      c++ => cplusplus

      其中左侧是待支持的特殊短语,右侧是左侧的特殊短语需要被转换为的短语。这一转换在分词前进行。

      可以在行的开头加入'//'作为注释符号,发现符号'//'后,整行将被忽略。

      特殊短语词库构造命令:

      mmseg -b exceptions.txt

      其中, 开关'-b'指示mmseg是要构造特殊短语词库;exceptions.txt是用户编辑的特殊短语转换规则。

      该命令执行后,将在当前目录下产生一个名为"synonyms.dat"的文件,将该文件放在"uni.lib"同一目录下,分词系统将自动启动特殊短语转换功能

      分词

      mmseg -d tobe_segment.txt

      其中,命令使用‘-d’开关指定词库文件所在的位置,参数dict_dir为词库文件(uni.lib )所在的目录;tobe_segment.txt 为待切分的文本文件,必须为UTF-8编码。如果一切正确,  mmseg会将切分结果以及所花费的时间显示到标准输出上。

      注意:分词中的相似词组,比如要在词库里有存在的配置才会有效!

    总结:当然以上都是一些安装的菜鸟级别应用,coreseek是一个强大系统官方也有很全的文档,还有查询表达式、增量索引、实时索引、中文词库、分布式索引等高级的应用。

  • 相关阅读:
    .net类库里ListView的一个BUG
    获取lable的caption, 摘抄还未测试可用否
    (转) lua实现split的简易方法
    2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)
    1. android
    开发人员应关注的20个jQuery网站/博客
    Temp
    彩票项目开发节项
    求android ble 解决方案!
    自己开发的工作流引擎
  • 原文地址:https://www.cnblogs.com/gaoxu387/p/2803183.html
Copyright © 2011-2022 走看看