安裝SPHINX支援中文

URL Link //n.sfs.tw/10058

2016-10-23 01:41:51 By 張○○

OS

Linux  2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

一、下載

到官網 http://sphinxsearch.com/downloads/release/ 下載

# wget http://sphinxsearch.com/files/sphinx-2.3.1-1.rhel6.x86_64.rpm

二、安裝

# rpm -ivh sphinx-2.3.1-1.rhel6.x86_64.rpm
正在準備…             ################################### [100%]
   1:sphinx                 ################################## [100%]

三、操作及指令

設定索引檔
    $ vi /etc/sphinx/sphinx.conf

建立全部的索引
    # indexer --all --rotate

建立指定index的索引
    # indexer indexname --rotate

指定設定檔
   # indexer --config /etc/sphinx/sphinx.conf --rotate --all

範例

# indexer --config /etc/sphinx/sphinx.conf --rotate --all

Sphinx 2.3.1-id64-beta (r4926)

Copyright (c) 2001-2015, Andrew Aksyonoff

Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/etc/sphinx/sphinx.conf'...

indexing index 'note_content'...

collected 56 docs, 0.1 MB

sorted 0.0 Mhits, 100.0% done

total 56 docs, 135073 bytes

total 0.023 sec, 5860000 bytes/sec, 2429.50 docs/sec

indexing index 'note_media'...

collected 76 docs, 0.0 MB

sorted 0.0 Mhits, 100.0% done

total 76 docs, 14757 bytes

total 0.003 sec, 3779001 bytes/sec, 19462.22 docs/sec

total 144 reads, 0.000 sec, 2.7 kb/call avg, 0.0 msec/call avg

total 24 writes, 0.000 sec, 32.3 kb/call avg, 0.0 msec/call avg

rotating indices: successfully sent SIGHUP to searchd (pid=4600).

開始/結束 SPHINX的搜尋引擎,如果沒此服務,可寫在 /etc/rc.d/rc.local 中
    # service searchd start/stop

runtime 開始/結束 SPHINX的搜尋引擎
  # searchd --stop
  # searchd

指定設定檔啟動
 # searchd -c /etc/sphinx/sphinx.conf

SPHINX的搜尋引擎的狀態
  # searchd --status

使用Mysql去查詢sphinx的搜尋結果,例如在test1索引中找具有'test'的字串
    # mysql -h 0 -P 9306
    mysql> SELECT * FROM test1 WHERE MATCH('test');

其他說明文件
/usr/share/doc/sphinx-2.3.1

商業上的支援
http://sphinxsearch.com/contacts.html

四、設定索引檔

修改 sphinx.conf 加入中文的支援

source note
{
        type                    = mysql
        sql_host                = localhost
        sql_user                = note
        sql_pass                = TaUNxY8ErBM8adQy
        sql_db                  = note
        sql_port                = 3306  # optional, default is 3306
# 加入這行避免連線的亂碼
        sql_query_pre           = SET NAMES utf8
# 主鍵放第一個
        sql_query               = \
                SELECT nID, title, keyword, remark, text, is_closed, o_uid,catID \
                FROM content
# 要放結果,設定條件用的(整數)欄位,主鍵不必設定
        sql_attr_uint           =o_uid
        sql_attr_uint           =catID
        sql_attr_uint           =is_closed

# 要放結果,設定條件用的(字串)欄位
#        sql_attr_string        = zip

# 要放結果,設定尋找用的(字串)欄位
        sql_field_string       =title
        sql_field_string       =keyword
        sql_field_string       =remark
        sql_field_string       =text
}
 
index inote
{
        source              = note
        path                = /var/lib/sphinx/note
        html_strip          = 1
        min_word_len        = 2
        charset_table = 請看下方說明
        ngram_len = 1
        ngram_chars = 請看下方說明
}
 
indexer
{
#原來是128M,依資料量多寡可改大一點,原則上100萬筆資料就至少要100M
        mem_limit               = 360M
}
#其餘不變不要改
searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log         = /var/log/sphinx/searchd.log
    query_log       = /var/log/sphinx/query.log
    read_timeout        = 5
    max_children        = 0
    pid_file        = /var/run/sphinx/searchd.pid
    seamless_rotate     = 0
    preopen_indexes     = 1
    workers         = threads # for RT to work
    binlog_path     = /var/lib/sphinx/
}

 

上面charset_table,ngram_chars兩個欄位的資料比較多,貼在此,記得這是一行寫完  

五、PHP支援

現在加入PHP程式的支援,讓PHP可以直接呼叫使用

其實在PHP> 5.2.2內建原生就可以支援sphinx,也有多達40個函式可以使用。請見參考資料[1]。

原生的PHP for sphinx函式安裝上比較麻煩,我另寫一篇「PHP for sphinx 函式庫安裝」的文件。

如果安裝失敗或是不想這麼麻煩的話,有人提供自己寫出來的Php函式庫sphinxapi.php,請自行搜尋使用,我用過免安裝而且還滿方便的,聽說效能還比php 提供的sphinx.so更好(我自己想這是合理的,因為php的函式會 多了很多安全的考量)[3]

六、其他說明及除錯

1. 新版本的sphinx沒有search這個程式,在console下可以用indextool來代替,但是沒辦法像search顯示這麼多:

$ indextool --dumphitlist indexname '要查詢的文字'

doc=44143, hit=0x04000002
doc=44143, hit=0x09000002
doc=44147, hit=0x05000002
doc=44147, hit=0x09000005
doc=44161, hit=0x00000001
doc=44162, hit=0x00000001
doc=44163, hit=0x00000001

2. sphinx-chinese 有提供原生支持中文字的語詞的部分,是由大陸的朋友修改的,我有用過,分詞的部分做得真的很好,安裝上也相當容易,缺點是正體中文的使用者得自建分詞,目前版本已跟到2.2.1(2013.11.09)。[2]

有興趣請看 http://www.sphinx-search.com/

3. 如果發生這樣的錯誤

 FATAL: failed to create pid file '/var/run/sphinx/searchd.pid': Permission denied

請確定你有這個目錄

# mkdir /var/run/sphinx

再將此目錄指定給 sphinx

# chown sphinx /var/run/sphinx

七、設定定時執行

# 每天四次做索引
10 2,11,16,21 * * * /usr/local/bin/indexer --config /etc/sphinx/sphinx.conf --rotate --all

參考資料

[1] http://php.net/manual/en/book.sphinx.php
[2] http://www.sphinx-search.com/
[3] http://sphinxsearch.com/forum/view.html?id=7580


原文編修 2015-05-25 15:16:31