Coreseek-帶中文分詞的Sphinx


什么是Coreseek

Sphinx默認不支持中文索引及檢索,基於Sphinx開發了Coreseek全文檢索服務器,Coreseek應該是現在用的最多的Sphinx中文全文檢索,它提供了為Sphinx設計的中文分詞包LibMMSeg包含mmseg中文分詞。

安裝

--解壓安裝包
# tar -zxvf coreseek-3.2.14.tar.gz
# ls
csft-3.2.14  mmseg-3.2.14  README.txt  testpack

安裝中文分詞mmseg

# cd mmseg-3.2.14/
# ./configure --prefix=/usr/local/mmseg
--編譯報錯 config.status: error: cannot find input file: src/Makefile.in
--運行下面指令再次編譯就能通過了
# automake
# make && make install
--運行mmseg,輸出安裝信息則mmseg中文分詞已經安裝好了
# /usr/local/mmseg/bin/mmseg
 
Coreseek COS(tm) MM Segment 1.0
Copyright By Coreseek.com All Right Reserved.
Usage: /usr/local/mmseg/bin/mmseg <option> <file>
...

安裝csft即sphinx

# cd csft-3.2.14/
# ./configure --prefix=/usr/local/coreseek --with-mysql=/usr/local/mysql --with-mmseg=/usr/local/mmseg
--with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/ # make && make install

配置帶有中文分詞的sphinx配置文件

配置文件和配置sphinx步驟一樣,只不過是在coreseek中,有幾個地方要注意。

注意:coreseek中配置文件是csft.conf,而不是sphinx.conf

# cd /usr/local/coreseek/etc
# cp sphinx.conf.dist csft.conf
--修改配置文件
# vi csft.conf
--主數據源
source main{
        sql_host                        = localhost
        sql_user                        = root
        sql_pass                        =root
        sql_db                          = test
        sql_port                        = 3306  # optional, default is 3306
        sql_sock                                = /tmp/mysql.sock
        sql_query_pre                   = SET NAMES utf8
        sql_query_pre                   = SET SESSION query_cache_type=OFF
        sql_query=select id,title,content from post
        #sql_attr_uint                  = group_id --關閉排序
        #sql_attr_timestamp             = date_added
        sql_query_info          = SELECT * FROM post WHERE id=$id
}
--增量數據源注釋(255,257s/^/#/g)
--主數據源索引
index main{
  source                  = main
  path                    = /usr/local/coreseek/var/data/main
  #stopwords                      = G:\data\stopwords.txt --關閉停詞
  #wordforms                      = G:\data\wordforms.txt
  #exceptions             = /data/exceptions.txt
  charset_type            = zh_cn.utf-8
  charset_dictpath        = /usr/local/mmseg/etc/    --添加詞典路徑
}
--增量數據源索引注釋(481,485s/^/#/g)
--分布式索引注釋(492,523s/^/#/g)
--索引器
indexer{
  mem_limit                       = 128M
}

:wq --保存退出 

生成索引並測試

--創建索引
# /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all
--或者(/usr/local/coreseek/bin)
[root@localhost bin]# ./indexer --all

注意:如果你設置的coreseek配置文件為csft.conf,則index、search和searchd時不用帶上-c /usr/local/soreseek/etc/csft.conf,因為默認就是去尋找這個文件

至此,中文分詞已經安裝完成了,並測試成功。

使用PHP去使用Sphinx技術

Sphinx集成到PHP程序中,有兩種方式:

1.Sphinx php模塊

2.Sphinxapi類

使用Sphinx需要以下幾件事

1、首先得有數據

2、建立Sphinx配置文件

3、生成索引

4、啟動Searchd服務進程,並開放端口9312

5、用PHP客戶端程序去連接Sphinx服務

一、啟動sphinx服務

想要在程序中使用sphinx必須開啟Sphinx服務

啟動進程命令:searchd

-c          #指定配置文件(如果是默認配置文件命名,則可以省略)
--stop      #停止服務
--pidfile   #用來顯示指定一個PID文件
-p          #指定端口
# /usr/local/coreseek/bin/searchd

注意:這里啟動的服務是searchd,不是search,Sphinx默認端口是9312

二、用PHP連接使用Sphinx程序

(1)PHP加載Sphinx模塊

# tar -zxvf sphinx-1.1.0.tgz 
# cd sphinx-1.1.0
[root@localhost sphinx-1.1.0]# /usr/local/php/bin/phpize  --生成腳本
--編譯安裝
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx
-----------------------------報錯--------------------------------------
configure: error: Cannot find libsphinxclient headers
-----------------------------解決方法----------------------------------
在之前的coreseek安裝包中(/root/coreseek-3.2.14/csft-3.2.14/api/libsphinxclient)
[root@localhost api]# cd libsphinxclient/
[root@localhost libsphinxclient]# ./configure
[root@localhost libsphinxclient]# make && make install
安裝完后,繼續安裝sphinx拓展
-------------------------------------------------------------------------
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx
# make && make install
# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
# cp ./sphinx.so /usr/local/php/ext/
--修改php配置文件
extension=sphinx.so    --添加
--重啟apache
# /usr/local/apache2/bin/apachectl restart
--查看phpinfo,如果有sphinx模塊,則說明加載模塊成功

安裝samba服務器實現文件共享

# yum -y install samba* --skip-broken
--編輯配置文件
# vi /etc/samba/smb.conf 

[web]
        path = /usr/local/apache2/htdocs
        browseable = yes
        writable = yes
:wq  --保存退出

--啟動服務
# service smb restart
--保證下次自動啟動
# chkconfig smb on
--關閉245運行級別下啟動
# chkconfig --level 245 smb off
--創建用戶
useradd apache
smbpasswd -a apache

windows中輸入地址\\192.168.10.130\web,也可映射成網絡磁盤,即可訪問web目錄。

--使apache用戶有創建目錄和文件的權限,這樣所有者和所屬組都是apache,有問題
setfacl -m u:apache:rwx -R htdocs/
setfacl -m d:u:apache:rwx -R htdocs/

--編輯apache配置文件,改變apache的執行者
User apache
Group apache
--重啟apache
# /usr/local/apache2/bin/apachectl restart
--此時apache進程的執行者是apache了,不是deamon

php測試腳本

<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>查詢</title>
<body>
    <h1>查詢頁面</h1>
    <form action="find.php" method="post">
    請輸入查詢關鍵字
    <input type="text" name="word" style="width:200px;"/><br/>
    <input type="submit" value="提交查詢"/>
    </form>
</body>
</html>
index.php
<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Find</title>
<body>
    <h1>查詢輸出頁面</h1>
    <?php
        $keyword = $_POST['word'];
        $sphinx = new SphinxClient();
        $sphinx->SetServer("localhost",9312);
        $sphinx->SetMatchMode(SPH_MATCH_ANY);    //匹配模式
        $result = $sphinx->query("$keyword","*");
        /*print_r($result);*/
        $ids = implode(',', array_keys($result['matches']));  //取出文檔id
        mysql_connect("localhost","root","root");
        mysql_select_db("test");
        mysql_query("set names utf8");
        $sql ="select * from post where id in ({$ids})";
        $res = mysql_query($sql);
        $opts=array(
            "before_match"=>"<font style='font-weight:bold;color:red'>",
            "after_match"=>"</font>"
        );
        while($row = mysql_fetch_assoc($res)){
            $res2 = $sphinx->buildExcerpts($row,"main",$keyword,$opts);
            echo "標題:{$res2[1]}<br/>";
            echo "內容:{$res2[2]}<br/>";
            echo "<br/>";
        }
    ?>
</body>
</html>
find.php

Sphinx實時索引

數據中的數據很大,然后我有些新數據后來加入到數據中,也希望能夠檢索到,全部重新建立索引很消耗資源,這樣需要用到“主索引+增量索引”的思路來解決,這個模式實現的基本原理是設置兩個數據源和兩個索引。

1、創建一個計數器

一個簡單的實現是,在數據庫中增加一個計數器,記錄將文檔集分為兩個部分的文檔ID,每次重新構建主索引時,更新這個表。

現在mysql中插入一個計數表

CREATE TABLE sph_counter(
counter_id int not null primary key,
max_doc_Id int not null
);

2、再次修改配置文件

主數據源,增量數據源,主索引,增量索引。

主數據源里面:我們需要把預查詢語句改成下面的語句

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

--主數據源
source main
{
    sql_query_pre = replace into sph_counter select 1,max(id) from post     sql_query
=select id,title,content from post where id <= (select max_doc_id from sph_counter where counter_id=1) } --打開增量數據源 source delta : main { sql_query_pre=set names utf8 sql_query=select id,title,content from post where id > (select max_doc_id from sph_counter where counter_id=1) } --主索引(不用變) --增量索引 index delta : main { source = delta path = /usr/local/coreseek/var/data/delta #morphology = stem_en }
--生成增量索引
[root@localhost bin]# ./indexer delta --rotate

crontab計划任務腳本定時更新索引

[root@localhost bin]# cd /usr/local/coreseek/
--創建腳本執行日志文件
[root@localhost coreseek]# cd var/log/
[root@localhost log]# touch main.log
[root@localhost log]# touch delta.log
[root@localhost log]# ls
delta.log  main.log  query.log  searchd.log  searchd.pid
--創建存放腳本文件夾和文件
[root@localhost coreseek]# mkdir init
[root@localhost init]# touch main.sh
[root@localhost init]# touch delta.sh
[root@localhost init]# ls
delta.sh  main.sh

[root@localhost init]# vi main.sh
------------------------------main.sh--------------------------------------
#!/bin/bash
#main.sh

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

[root@localhost init]# vi delta.sh 
------------------------------delta.sh--------------------------------------
#!/bin/bash
#delta.sh

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

最后,我們需要腳本能夠自動運行,以實現增量索引每5分鍾重新建立,和主索引只在凌晨2:30時重新建立。

[root@localhost init]# crontab -e

*/5 * * * * /usr/local/coreseek/init/delta.sh    --每5分鍾執行一次增量索引
30 2 * * * /usr/local/coreseek/init/main.sh     --每天凌晨建立主索引

--加腳本的執行權限
[root@localhost init]# chmod a+x *
--計划任務已啟動
[root@localhost init]# crontab -l
*/5 * * * * /usr/local/coreseek/init/delta.sh
00 03 * * * /usr/local/coreseek/init/main.sh

分布式索引

分布式是為了改善查詢延遲問題和提高多服務器、多CPU 或多核環境下的吞吐率,對於大量數據(即十億級的記錄數和TB級的文本量)上的搜索應用來說是很關鍵的。

分布式思想:對數據進行水平分區(HP,Horizontally partition),然后並行處理,當searched收到一個對分布式索引的查詢時,它做如下操作

1.連接到遠程代理

2.執行查詢

3.對本地索引進行查詢

4.接收來自遠程代理的搜索結果

5.將所有結果合並,刪除重復項

6.將合並后的結果返回給客戶端

index dist
{
    type=distributed
    local=chunk1
    agent=localhost:9312:chunk2    本地
    agent=192.168.100.2:9312:chunk3  遠程
    agent=192.168.100.2:9312:chunk4  遠程
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM