[Rocky9] 安裝SPHINX Search 3 支援中文

URL Link //n.sfs.tw/16331

2024-01-19 03:03:53 By 張○○

Sphinx Search是一套非常棒的搜尋引擎,可支援 mysql 及中文語系,但是 sphinx2 和 sphinx3版之間存在極大的差異,例如設定檔及安裝方式都存在差異,我經過了八個小時的研究才完成這篇文章。

在2016年曾寫過一篇2版的安裝SPHINX支援中文@新精讚,如果是2版使用者請參考此篇。

 

OS

Rocky Linux release 9.2 (Blue Onyx)

 

一、下載及安裝

安裝 sphinx search

到官網

http://sphinxsearch.com/downloads/release/

http://sphinxsearch.com/downloads/current/

下載,目前最新版本為 3.6.1 (2023.10)

3版以後並無官網的rpm可下載,請下載tarball

# cd ~
# wget https://sphinxsearch.com/files/sphinx-3.6.1-c9dbeda-linux-amd64.tar.gz

解壓縮  

# tar zxvf sphinx-3.6.1-c9dbeda-linux-amd64.tar.gz

目錄如下:

# ls ~/sphinx-3.6.1
api  bin  doc  etc  misc  src

把其中的 bin/ 目錄下的檔案複製到 /usr/local/bin/

# cp ~/sphinx-3.6.1/bin/*  /usr/local/bin/

建立index檔存放的目錄

# mkdir /usr/local/share/sphinx

 

安裝 libmysqlclient.so

預設安裝完的mysql-common 是沒有 libmysqlclient.so,執行sphinx的 indexer會遇到錯誤:

ERROR: index 'xxx: sql_connect: failed to load libmysqlclient (or libmariadb) (DSN=mysql://xxx:***@127.0.0.1:3306/note).

透過搜尋找到 rpm 下來安裝

# wget https://dl.rockylinux.org/pub/rocky/9/devel/x86_64/os/Packages/m/mysql-libs-8.0.32-1.el9_2.x86_64.rpm

# rpm -ivh mysql-libs-8.0.32-1.el9_2.x86_64.rpm

或是安裝 mysql-libs

# yum install mysql-libs

安裝完畢請建立連結

# ln -s /usr/lib64/mysql/libmysqlclient.so.21 /usr/lib64/libmysqlclient.so

查看版本

# searchd --version
Sphinx 3.6.1 (commit c9dbedab)
Copyright (c) 2001-2023, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Built on: #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022
Built with: GNU 9.4.0
Build date: Oct  4 2023
Build type: release
Configure flags: cmake
Compiled DB drivers: mysql-dynamic pgsql-dynamic odbc-dynamic
Compiled features: libexpat libstemmer re2 jemalloc
Versions: binlog_format v.10, index_format v.63, udf_api v.23

安裝的版本是 3.6版

 

二、設定索引檔

修改 sphinx.conf 並加入中文的支援,以下範例是將資料表的每個欄位都做索引,僅供參考

# vi /etc/sphinx/sphinx.conf

source base
{
        type                    = mysql
        sql_host                = 127.0.0.1
        sql_user                = note
        sql_pass                = TsomepasswordQy
        sql_db                  = note
        sql_port                = 3306  # optional, default is 3306
        sql_query_pre           = SET NAMES utf8
}
source note_content:base
{
        sql_query               = \
            SELECT nID, nID as theid, sn, title, keyword, remark, c.text, 'content' as module, c.is_closed, c.o_uid, c.catID, a.text as catname, u.site_name, u.site_prefix \
            FROM content c left join catalog a using( catID) left join user_basic u on  c.o_uid= u.uid
}

index note_content
{
    field = theid, title, keyword, remark, text
    attr_uint = o_uid, catID, is_closed
    attr_string = sn, module, site_name, catname, site_prefix
        source                 = note_content
        path                   = /usr/local/share/sphinx/note_content
        html_strip             = 1
        min_word_len           = 2
  charset_table = 0..9, A..Z->a..z, a..z, U+0149, U+017F, U+0138, U+00DF, U+00C0..U+00D6->U+00E0..U+00F6, U+00E0..U+00F6, U+00D8..U+00DE->U+00F8..U+00FE, U+00F8..U+00FE, U+0100->U+0101, U+0101, U+0102->U+0103, U+0103, U+0104->U+0105, U+0105, U+0106->U+0107, U+0107, U+0108->U+0109, U+0109, U+010A->U+010B, U+010B, U+010C->U+010D, U+010D, U+010E->U+010F, U+010F, U+0110->U+0111, U+0111, U+0112->U+0113, U+0113, U+0114->U+0115, U+0115, U+0116->U+0117, U+0117, U+0118->U+0119, U+0119, U+011A->U+011B, U+011B, U+011C->U+011D, U+011D, U+011E->U+011F, U+011F, U+0130->U+0131, U+0131, U+0132->U+0133, U+0133, U+0134->U+0135, U+0135, U+0136->U+0137, U+0137, U+0139->U+013A, U+013A, U+013B->U+013C, U+013C, U+013D->U+013E, U+013E, U+013F->U+0140, U+0140, U+0141->U+0142, U+0142, U+0143->U+0144, U+0144, U+0145->U+0146, U+0146, U+0147->U+0148, U+0148, U+014A->U+014B, U+014B, U+014C->U+014D, U+014D, U+014E->U+014F, U+014F, U+0150->U+0151, U+0151, U+0152->U+0153, U+0153, U+0154->U+0155, U+0155, U+0156->U+0157, U+0157, U+0158->U+0159, U+0159, U+015A->U+015B, U+015B, U+015C->U+015D, U+015D, U+015E->U+015F, U+015F, U+0160->U+0161, U+0161, U+0162->U+0163, U+0163, U+0164->U+0165, U+0165, U+0166->U+0167, U+0167, U+0168->U+0169, U+0169, U+016A->U+016B, U+016B, U+016C->U+016D, U+016D, U+016E->U+016F, U+016F, U+0170->U+0171, U+0171, U+0172->U+0173, U+0173, U+0174->U+0175, U+0175, U+0176->U+0177, U+0177, U+0178->U+00FF, U+00FF, U+0179->U+017A, U+017A, U+017B->U+017C, U+017C, U+017D->U+017E, U+017E, U+0410..U+042F->U+0430..U+044F, U+0430..U+044F, U+05D0..U+05EA, U+0531..U+0556->U+0561..U+0586, U+0561..U+0587, U+0621..U+063A, U+01B9, U+01BF, U+0640..U+064A, U+0660..U+0669, U+066E, U+066F, U+0671..U+06D3, U+06F0..U+06FF, U+0904..U+0939, U+0958..U+095F, U+0960..U+0963, U+0966..U+096F, U+097B..U+097F, U+0985..U+09B9, U+09CE, U+09DC..U+09E3, U+09E6..U+09EF, U+0A05..U+0A39, U+0A59..U+0A5E, U+0A66..U+0A6F, U+0A85..U+0AB9, U+0AE0..U+0AE3, U+0AE6..U+0AEF, U+0B05..U+0B39, U+0B5C..U+0B61, U+0B66..U+0B6F, U+0B71, U+0B85..U+0BB9, U+0BE6..U+0BF2, U+0C05..U+0C39, U+0C66..U+0C6F, U+0C85..U+0CB9, U+0CDE..U+0CE3, U+0CE6..U+0CEF, U+0D05..U+0D39, U+0D60, U+0D61, U+0D66..U+0D6F, U+0D85..U+0DC6, U+1900..U+1938, U+1946..U+194F, U+A800..U+A805, U+A807..U+A822, U+0386->U+03B1, U+03AC->U+03B1, U+0388->U+03B5, U+03AD->U+03B5, U+0389->U+03B7, U+03AE->U+03B7, U+038A->U+03B9, U+0390->U+03B9, U+03AA->U+03B9, U+03AF->U+03B9, U+03CA->U+03B9, U+038C->U+03BF, U+03CC->U+03BF, U+038E->U+03C5, U+03AB->U+03C5, U+03B0->U+03C5, U+03CB->U+03C5, U+03CD->U+03C5, U+038F->U+03C9, U+03CE->U+03C9, U+03C2->U+03C3, U+0391..U+03A1->U+03B1..U+03C1, U+03A3..U+03A9->U+03C3..U+03C9, U+03B1..U+03C1, U+03C3..U+03C9, U+0E01..U+0E2E, U+0E30..U+0E3A, U+0E40..U+0E45, U+0E47, U+0E50..U+0E59
        ngram_len = 1
        ngram_chars = U+4E00..U+9FBF, U+3400..U+4DBF, U+20000..U+2A6DF, U+F900..U+FAFF, U+2F800..U+2FA1F, U+2E80..U+2EFF, U+2F00..U+2FDF, U+3100..U+312F, U+31A0..U+31BF, U+3040..U+309F, U+30A0..U+30FF, U+31F0..U+31FF, U+AC00..U+D7AF, U+1100..U+11FF, U+3130..U+318F, U+A000..U+A48F, U+A490..U+A4CF
}

searchd
{
        listen                  = 9312
        listen                  = 9306:mysql41
        log                     = /var/log/searchd.log
        query_log               = /var/log/query.log
        read_timeout            = 5
        max_children            = 0
        pid_file                = /run/searchd.pid
        seamless_rotate         = 0
        preopen_indexes         = 1
        workers                 = threads # for RT to work
        binlog_path             = /var/lib/
}

上面charset_table 的欄位是指納入分析的字元unicode的範圍,原則上我把所有的文字全部加入寫法及unicode範圍可參考[1][2]。

ngram_chars 欄位配合ngram_len = 1是指提供cjk字元的支持,並指定下面的unicode範圍[4],不能和上面charset_table重覆[3]

 

建立索引檔存放位置

# mkdir /usr/local/share/sphinx/

可先試著建立索引,請參考四、建立索引。

三、建立系統服務

因為3版的安裝沒有提供service,所以要自己建立系統服務[5][6]

# vi /usr/lib/systemd/system/searchd.service

[Unit]
Description=Sphinx Search Server

[Service]
Type=simple
# 啟動服務指令
ExecStart=/usr/local/bin/searchd -c /etc/sphinx/sphinx.conf

# 服務行程 PID(通常配合 forking 的服務使用)
PIDFile=/run/searchd.pid

# 啟動服務前,執行的指令
# ExecStartPre=/opt/your_command

# 啟動服務後,執行的指令
ExecStartPost=/usr/bin/echo 'Searchd started'

# 停止服務指令
ExecStop=/usr/local/bin/searchd --stop

# 停止服務後,執行的指令
ExecStopPost=/usr/bin/echo 'Searchd stopped'

# 重新載入服務指令
ExecReload=/usr/local/bin/searchd --stop;/usr/local/bin/searchd -c /etc/sphinx/sphinx.conf;

# 設定服務在 Syslog 中的名稱
SyslogIdentifier=SphinxSearch

# 服務終止時自動重新啟動
Restart=always

# 重新啟動時間格時間(預設為 100ms)
# RestartSec=3s

# 重新啟動時間格時間(預設為 100ms)
# RestartSec=3s

# 啟動服務逾時秒數
# TimeoutStartSec=3s

# 停止服務逾時秒數
# TimeoutStopSec=3s

# 執行時的工作目錄
# WorkingDirectory=/opt/your_folder

# 執行服務的使用者(名稱或 ID 皆可)
# User=myuser

# 執行服務的群組(名稱或 ID 皆可)
# User=mygroup

# 環境變數設定
# Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"

# 服務輸出訊息導向設定
# StandardOutput=syslog

# 服務錯誤訊息導向設定
# StandardError=syslog

[Install]
WantedBy=multi-user.target

修改完後請reload system:

# systemctl daemon-reload

這樣就建立了一個名為 serachd的服務了。

 

四、建立索引

使用 indexer 來建立索引,先決條件是你得有正確的 sphinx.conf

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

Sphinx 3.6.1 (commit c9dbedab)
Copyright (c) 2001-2023, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/etc/sphinx/sphinx.conf'...
WARNING: datadir: explicit index 'path' is deprecated; please convert your config to use 'datadir' directive
indexing index 'note_content'...
collected 3313 docs, 13.3 MB
sorted 3.7 Mhits, 100.0% done
total 3313 docs, 13.33 Mb
total 2.1 sec, 6.273 Mb/sec, 1559 docs/sec

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

雖然有警告,但是還是能正確的建立索引。這警告我還沒辦法解決

感謝網友2/19 jjdai 給我一個建議,可以解決這個警告:

index Xxx { ...} 區段中,path 準備被棄用,你把 path 設定那行註解掉或刪除。然後增加以下區段即可:

common
{
    datadir = /home/sphinx/data
}

看來 v3.x 以後路徑都想基於 datadir,所以也不用去設定 index Yyy {}:Xxx 區段內內的 path,他會在 datadir 目錄內固定建立 Yyy (index 名稱 Yyy 即建立 Yyy)。

 

五、操作及指令

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

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

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

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

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

   # systemctl start/stop/restart searchd

設定開機啟動

  # systemctl enable searchd

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');

 

六、其他說明及除錯

 

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

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

例如:

$ indextool  -c /etc/sphinx/sphinx.conf --dumphitlist note_content 'sphinx'

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. 如果你出現類似下面的警告,那表示你用2版的設定方式,這已經被捨棄(但還是能用)[7]

WARNING: key 'sql_attr_uint' is deprecated in /etc/sphinx/sphinx.conf line 19; use 'attr_uint in index{..} section' instead.
WARNING: key 'sql_attr_string' is deprecated in /etc/sphinx/sphinx.conf line 21; use 'attr_string in index{..} section' instead.
WARNING: 21 more warnings skipped.

 

 

七、設定定時執行

在 crontab -e 中加入這行

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

 

 

參考資料

[1] http://sphinxsearch.com/docs/current/conf-charset-table.html

[2] https://en.wikipedia.org/wiki/CJK_characters

[3] http://sphinxsearch.com/forum/view.html?id=15219

[4] http://sphinxsearch.com/docs/current/conf-charset-table.html

[5] https://blog.gtwang.org/linux/linux-create-systemd-service-unit-for-python-echo-server-tutorial-examples/

[6] https://docs.fedoraproject.org/en-US/quick-docs/systemd-understanding-and-administering/

[7] https://sphinxsearch.com/docs/sphinx3.html#schemas-index-config