1. 程式人生 > 實用技巧 >sphinx(三)centos7編譯安裝sphinx-for-chinese

sphinx(三)centos7編譯安裝sphinx-for-chinese

Coreseek目前官方網站已經不能訪問了,大概率估計後期就不會有人維護了。對於長期發展來看,可能是不要好。

所以我的部落格中使用了sphinx-for-chinese。是基於sphinx2.3.1開發的。

下載地址:

https://gitee.com/mz/sphinx

進入目錄

cd /usr/local/download/sphinx

安裝依賴:

yum install mysql-devel

編譯,這裡我需要說明一下:我的mysql使用編譯安裝,所有的檔案都被我編譯安裝到/usr/local/mariadb下。如果你的mysql不是使用編譯安裝的,我下邊編譯的命令,你可能沒有辦法使用。

./configure  --prefix=/usr/local/sphinxforchinese  --with-mysql=/usr/local/mariadb

安裝

make&&make install

安裝成功

1:配置支援中文:

(1):複製中文字典

cp /usr/local/download/sphinx/xdict_1.1.txt /usr/local/sphinxforchinese/xdict_1.1.txt

(2):進入sphinx安裝目錄

cd /usr/local/sphinxforchinese

(3):安裝字典

/usr/local/sphinxforchinese/bin/mkdict  xdict_1.1.txt  etc/xdict

配置成功顯示:

[root@iZuf60ynur81p6k0ysvtneZ sphinxforchinese]# /usr/local/sphinxforchinese/bin/mkdict  xdict_1.1.txt  etc/xdict
Preparing...
Making Chinese dictionary:  100% |******************************|
Total words:                 284757
File size:                2854912 bytes
Compression ratio:              100 %
Chinese dictionary was successfully created!

2:啟動:

/usr/local/sphinxforchinese/bin/searchd

報錯:

/usr/local/sphinxforchinese/bin/searchd: error while loading shared libraries: libmariadb.so.3: cannot open shared object file: No such file or directory

解決:

ln -s /usr/local/mariadb/lib/libmariadb.so.3 /usr/lib64/libmariadb.so.3 #建立軟連線

3:再次啟動

/usr/local/sphinxforchinese/bin/searchd

報錯:

[root@iZuf60ynur81p6k0ysvtneZ sphinx]# /usr/local/sphinxforchinese/bin/searchd
Sphinx 2.2.9-id64-release (r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
  
FATAL: no readable config file (looked in /usr/local/sphinxforchinese/etc/sphinx.conf, ./sphinx.conf).
[root@iZuf60ynur81p6k0ysvtneZ sphinx]# /usr/local/sphinxforchinese/bin/searchd
Sphinx 2.2.9-id64-release (r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
  
FATAL: no readable config file (looked in /usr/local/sphinxforchinese/etc/sphinx.conf, ./sphinx.conf).

沒有配置檔案,解決方法:

cp /usr/local/sphinxforchinese/etc/sphinx-min.conf.dist /usr/local/sphinxforchinese/etc/sphinx.conf

4:再次啟動

/usr/local/sphinxforchinese/bin/searchd

報錯:

[root@iZuf60ynur81p6k0ysvtneZ sphinx]# /usr/local/sphinxforchinese/bin/searchd
Sphinx 2.2.9-id64-release (r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
  
using config file './sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
precaching index 'test1'
WARNING: index 'test1': preload: failed to open /usr/local/sphinxforchinese/var/data/test1.sph: No such file or directory; NOT SERVING
precaching index 'testrt'
precached 2 indexes in 0.000 sec

就是找不到索引檔案。

我們來修改剛剛複製的sphinx.conf檔案

#
# Minimal Sphinx configuration sample (clean, simple, functional)
#
  
source src1
{
    # 你的資料庫配置資訊
    type            = mysql
  
    sql_host        = localhost
    sql_user        = mysql
    sql_pass        =
    sql_db          = test
    sql_port        = 3306  # optional, default is 3306
  
    # 資料來源
    sql_query       = \
        SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
        FROM documents
    # 具體欄位優化 
    # 正整數型別
sql_attr_uint       = group_id
# 時間戳型別
    sql_attr_timestamp  = date_added
}
  
index test1
{
    source          = src1
    # 確保目錄存在,不存在建立
    path            = /usr/local/sphinxforchinese/var/data/test1
    # 配置中文字典
    chinese_dictionary = /usr/local/sphinxforchinese/etc/xdict
}
  
index testrt
{
    type            = rt
    rt_mem_limit    = 128M
    # 確保目錄存在,不存在建立
    path            = /usr/local/sphinxforchinese/var/data/testrt
  
    rt_field        = title
    rt_field        = content
    rt_attr_uint    = gid
}
  
indexer
{
    mem_limit       = 128M
}
  
searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    # 確保目錄存在,不存在建立
    log             = /usr/local/sphinxforchinese/var/log/searchd.log
    # 確保目錄存在,不存在建立
    query_log       = /usr/local/sphinxforchinese/var/log/query.log
    read_timeout    = 5
    max_children    = 30
    # 確保目錄存在,不存在建立
    pid_file        = /usr/local/sphinxforchinese/var/log/searchd.pid
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old      = 1
    workers         = threads # for RT to work
    # 確保目錄存在,不存在建立
    binlog_path     = /usr/local/sphinxforchinese/var/data
}

我們將/usr/local/sphinxforchinese /etc目錄下(安裝目錄)的example.sql匯入資料庫

# 使用test資料庫

MariaDB [(none)]> use test;
Database changed
MariaDB [test]> source /usr/local/sphinxforchinese/etc/example.sql
Query OK, 0 rows affected, 1 warning (0.005 sec)
  
Query OK, 0 rows affected (0.010 sec)
  
Query OK, 4 rows affected (0.003 sec)
Records: 4  Duplicates: 0  Warnings: 0
  
Query OK, 0 rows affected, 1 warning (0.002 sec)
  
Query OK, 0 rows affected (0.014 sec)
  
Query OK, 10 rows affected (0.002 sec)
Records: 10  Duplicates: 0  Warnings: 0

建立索引:

/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf  --all --rotate

建立成功:

Sphinx 2.2.9-id64-release (r5006)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)
  
using config file '/usr/local/sphinxforchinese/etc/sphinx.conf'...
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.003 sec, 50168 bytes/sec, 1039.77 docs/sec
skipping non-plain index 'testrt'...
total 4 reads, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
total 12 writes, 0.000 sec, 0.1 kb/call avg, 0.0 msec/call avg
rotating indices: successfully sent SIGHUP to searchd (pid=4092).

使用PHP做測試:

示例PHP檔案在下載的包目錄(/usr/local/download/sphinx/api)下

我這裡使用原生PHP做的測試,因此,你需要將sphinxapi.php放在一個你能引用到的地方

index.php

<?php
// phpinfo();
  
require("/usr/local/nginx/html/sphinxapi.php");
$sphinx = new Sphinxclient();
$sphinx->setServer('127.0.0.1',9312);
$keyword='test';//要搜尋的關鍵字
$index= 'test1';//索引名稱
//查詢出關鍵字所在的主鍵ID
$sphinx->_limit=2000;
$res= $sphinx->Query($keyword, $index);
// $res = $sphinx->Query($keyword, '*');
if(isset($res['matches'])){
    $ids = array_keys($res['matches']);
    $ids = implode(',',$ids);
}else{
    print_r('內容不存在');exit;
}
$sql = "SELECT * from documents where id in ($ids)"; 
$mysqli_con= mysqli_connect('127.0.0.1','mysql', '', 'test', '3306', '/usr/local/mariadb/tmp/mysql.sock');
$res = mysqli_query($mysqli_con, $sql);
while($row = mysqli_fetch_assoc($res)){
    $data[] = $row;
}
foreach($data as $key=>$v)
{
    $v = str_replace($keyword, "<font color='red'>{$keyword}</font>", $v);
    $data[$key]= $v;
}
echo "<pre>";
print_r($data);//*/

訪問得到如下結果:

結果沒問題,但是有一個報錯:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SphinxClient has a deprecated constructor in /usr/local/nginx/html/sphinxapi.php on line 406

解決方案:

找到與類SphinxClient同名的函式,將函式名改為__construct即可。

測試中文結果如下圖所示:

5:sphinx常用命令

(1):啟動

/usr/local/sphinxforchinese/bin/searchd

(2):停止

/usr/local/sphinxforchinese/bin/searchd –stop

(3):建立索引

/usr/local/sphinxforchinese/bin/indexer -c /usr/local/sphinxforchinese/etc/sphinx.conf  --all --rotate

以上大概就是sphinx-for-chinese的安裝及使用方式。

有好的建議,請在下方輸入你的評論。

歡迎訪問個人部落格
https://guanchao.site

歡迎訪問小程式: