zoukankan      html  css  js  c++  java
  • Sphinx(coreseek) 安装使用以及词库的扩展

    1、Sphinx(coreseek) 是啥

      一般而言,Sphinx是一个独立的全文搜索引擎;而Coreseek是一个支持中文的全文搜索引擎,意图为其他应用提供高速、低空间占用、高结果相关度的中文全文搜索能力。Sphinx/Coreseek可以非常容易的与SQL数据库和脚本语言集成。

          也就是说Coreseek可以单独使用,不需要再安装Sphinx


    2、从哪里获取

        Sphinx原始版本可以从Sphinx官方网站 http://www.sphinxsearch.com/,Coreseek可以从Coreseek官方网站 http://www.coreseek.cn/下载.


    3、主要特性

    目前,Sphinx/Coreseek的发布包包括如下软件:

    • indexer: 用于创建全文索引;
    • search: 一个简单的命令行(CLI) 的测试程序,用于测试全文索引;
    • searchd: 一个守护进程,其他软件可以通过这个守护进程进行全文检索;
    • sphinxapi: 一系列searchd 的客户端API 库,用于流行的Web脚本开发语言(PHP, Python, Perl, Ruby, Java).
    • spelldump: 一个简单的命令行工具,用于从 ispell 或 MySpell (OpenOffice内置绑定) 格式的字典中提取词条。当使用 wordforms 时可用这些词条对索引进行定制.
    • indextool: 工具程序,用来转储关于索引的多项调试信息。 此工具是从版本Coreseek 3.1(Sphinx 0.9.9-rc2)开始加入的。
    • mmseg: 工具程序和库,Coreseek用于提供中文分词和词典处理。

    4、安装Coreseek

    1)将你下载的tar包解压,并进入coreseek 子目录:      

    $ tar xzvf coreseek-3.2.14.tar.gz
    $ cd coreseek

    2)首先安装MMSeg:

    $ cd mmseg
    
    $ ./configure --prefix=/usr/local/mmseg
    
    $ make
    
    $ make install
    
    $ cd ..

    3)运行配置程序:

    $ ./configure

    configure程序有很多运行选项。完整的列表可以通过使用 --help 开关得到。最重要的如下:

    • --prefix, 定义将Coreseek安装到何处;比如 --prefix=/usr/local/coreseek (以下全部示例都假定Coreseek安装在这个位置)
    • --with-mysql, 当自动检测失败时,指出在那里能找到MySQL 头文件和库文件;
    • --with-pgsql, 指出在那里能找到PostgreSQL头文件和库文件.
    • --with-mmseg, 启用基于MMSeg的中文分词法,并指出在那里能找到MMSeg头文件和库文件.
    • --with-python, 启用Python数据源支持. 需要预先安装Python2.6.

    4)编译源代码生成二进制程序:

    $ make
    
    $ make install

    5、Coreseek快速配置

    1)创建配置文件.

    缺省的配置文件名为 csft.conf. 全部的Sphinx/Coreseek提供的程序默认都在当前工作的目录下寻找该文件.

    configure 程序生成的示例配置文件sphinx.conf.dist 中包括全部选项的注释,复制并编辑这个文件使之适用于你的具体情况: (请确认 Sphinx/Coreseek 安装在 /usr/local/coreseek/)

    $ cd /usr/local/coreseek/etc
    $ cp sphinx.conf.dist csft.conf
    $ vi csft.conf

     

    coreseek配置文件解析:

    • source:数据源,数据是从什么地方来的。
    • index:索引,当有数据源之后,从数据源处构建索引。索引实际上就是相当于一个字典检索。有了整本字典内容以后,才会有字典检索。
    • searchd:提供搜索查询服务。它一般是以deamon的形式运行在后台的。
    • indexer:构建索引的服务。当要重新构建索引的时候,就是调用indexer这个命令。
    • attr:属性,属性是存在索引中的,它不进行全文索引,但是可以用于过滤和排序。

    具体配置源信息,请见官方文档:http://www.coreseek.com/products-install/datasource/

    #源定义
    source xml
    {
        type                    = xmlpipe2
        xmlpipe_command = bincat var/test/test.xml     #此处也可使用其他可执行程序输出xml数据
    }
    
    #index定义
    index xml
    {
        source            = xml             #对应的source名称
        path            = var/data/xml
        docinfo            = extern
        mlock            = 0
        morphology        = none
        min_word_len        = 1
        html_strip                = 0
        #charset_dictpath = /usr/local/mmseg3/etc/    #BSD、Linux环境下设置,/符号结尾
        charset_dictpath = etc/                        #Windows环境下设置,/符号结尾
        charset_type        = zh_cn.utf-8
    }

    2)运行indexer 为你的数据创建全文索引:

    $ cd /usr/local/coreseek/etc
    $ /usr/local/coreseek/bin/indexer --all

    3)测试索引:

    $ cd /usr/local/coreseek/etc
    $ /usr/local/coreseek/bin/search test

    或者

    /usr/local/coreseek/bin/search -c /usr/local/coreseek/etc/csft.conf  测试索引

    6、通过PHP代码调用索引

    1)运行守护进程searchd,PHP脚本需要连接到searchd上进行检索:

    $ cd /usr/local/coreseek/etc
    $ /usr/local/coreseek/bin/searchd

    2)运行PHP API 附带的test 脚本(运行之前请确认searchd守护进程已启动):

    $ cd /源代码目录/coreseek/api
    $ php test.php test

    当然,这是直接包含coreseek的api接口文件的方式来调用索引,还可以通过安装 sphinx的php扩展来调用(不过客户端版本和服务端版本要保持一致

    测试代码为:

    $sphinx = new SphinxClient();
    $sphinx->SetServer ( 'x.x.x.x', 9312 );
    $sphinx->SetArrayResult ( true );//设置返回结果集为php数组格式
    // $sphinx->SetLimits(0, 20, 1000);//匹配结果的偏移量,参数的意义依次为:起始位置,返回结果条数,最大匹配条数
    
    //最大搜索时间
    // $sphinx->SetMaxQueryTime(10);
    
    //索引源是配置文件中的 index 类,如果有多个索引源可使用,号隔开:'email,diary' 或者使用'*'号代表全部索引源
    $index = 'index'; 
    $sphinx->setMatchMode(SPH_MATCH_ALL);  
    // $sphinx->setMaxQueryTime(30);                             //设置最大搜索时间    
    // $sphinx->SetArrayResult(false);                           //是否将Matches的key用ID代替    
    // $sphinx->SetSelect ( "*" );                               //设置返回信息的内容,等同于SQL    
    // $sphinx->SetRankingMode(SPH_RANK_BM25);                   //设置评分模式,SPH_RANK_BM25可能使包含多个词的查询的结果质量下降。  
    
    
    $result = $sphinx->query("测试", $index); 
    $err = $sphinx->GetLastError();   
    
    
    echo '<pre>';
    print_r($result);
    print_r($err);
    echo '</pre>';

    6、扩展词库

    虽然coreseek自带了中文词库,但是毕竟只是基础词库,如果我们需要一些行业词库,则可以自己扩展词库。

    1)首先要到搜狗下载词库:http://pinyin.sogou.com/dict/

    2)转换词库为文本文件

         网上找一个搜狗转 google的小工具,用它把你下载的全部词库转成文本文件, 生成为一个文件命名为words.txt。文件要用utf8编码保存。

         如果你想自己转换请参考官网上的方法:http://www.coreseek.cn/opensource/mmseg/

    3)转换文本文件为 词库格式

         现在我们有了一个初步的词库,但这个词库还不能直接使用,要再整理并转换coreseek使用的格式才行,通过PHP代码来转换

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    date_default_timezone_set ('Asia/Shanghai');
    set_time_limit(0);
    
    $buffer    = ini_get('output_buffering');
    if($buffer){
        ob_end_flush();
    }
    
    echo '处理新词库...'.PHP_EOL;
    flush();
    $filename     = "words.txt";
    $handle     = fopen ($filename, "r");
    $content     = fread ($handle, filesize ($filename));
    fclose ($handle);
    
    $content    =    trim($content);
    $arr1         =     explode( "
    " ,$content );
    $arr1        =    array_flip(array_flip($arr1));
    
    foreach($arr1 as $key=>$value){
        
        $value  =   dealchinese($value);
        if(!empty($value)){
            $arr1[$key] = $value;
        }else{
            unset($arr1[$key]);
        }
    }
    
    echo '处理原来词库...'.PHP_EOL;
    flush();
    $filename2     = "unigram.txt";
    $handle2     = fopen ($filename2, "r");
    $content2     = fread ($handle2, filesize ($filename2));
    fclose ($handle2);
    $content2    = dealchinese($content2,"
    ");
    $arr2         = explode( "
    " ,$content2 );
    echo '删除相同词条...'.PHP_EOL;
    flush();
    $array_diff    = array_diff($arr1,$arr2);
    
    echo '格式化词库...'.PHP_EOL;
    flush();
    $words='';
    foreach($array_diff as $k => $word){
        $words .= $word."	1".PHP_EOL."x:1".PHP_EOL;
    }
    //echo $words;
    file_put_contents('words_new.txt',$words,FILE_APPEND);
    echo 'done!';
    
    function dealChinese($str, $join=''){
        preg_match_all('/[x{4e00}-x{9fff}]+/u', $str, $matches); //将中文字符全部匹配出来
        $str = join($join, $matches[0]); //从匹配结果中重新组合
        return $str;
    }

    4)生成新的词库

    运行完后会在相同目录下生产 words_new.txt把这个文件加到原unigram.txt的后面,保存备用.

    把上面得到的文件unigram.txt复制到/usr/local/mmseg-3.2.14/etc/

    cd  /usr/local/mmseg-3.2.14/etc/
    /usr/local/mmseg-3.2.14/bin/mmseg -u unigram.txt 

    该命令执行后,将会在unigram.txt所在目录中产生一个名为unigram.txt.uni的文件,将该文件改名为uni.lib,完成词典的构造。

    5)测试新的词库

    新建测试文件 test.txt,里面写入你要测试的文本

    /usr/local/mmseg-3.2.14/bin/mmseg -d  /usr/local/mmseg-3.2.14/etc/ test.txt

    也就是

    mmseg -d <dict_dir> test.txt

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

    7、coreseek常用命令

    启动
    /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf
    停止
    /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf --stop
    建立索引
    /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all
    重建索引
    /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all --rotate
  • 相关阅读:
    linux 重启网卡的方法
    linux find命令详解
    linux grep命令
    linux ls命令详解
    linux ping命令详解
    FTP命令使用详解
    linux du命令详解
    linux scp命令详解
    linux curl用法详解
    Linux top命令的图解使用
  • 原文地址:https://www.cnblogs.com/whoamme/p/6144361.html
Copyright © 2011-2022 走看看