1. 程式人生 > 其它 >方便遠端伺服器上開發,記錄一下VS Code的遠端開發by SSH

方便遠端伺服器上開發,記錄一下VS Code的遠端開發by SSH

Sphinx使用的版本我是一點點增高的。

Coreseek3.2是基於sphinx0.9開發的。

Sphinx-for-chinese是基於sphinx2.3.1開發的。

我這裡嘗試一下sphinx的最新版本sphinx3.3.1,當然,這個只做為了解就好,我也只是嘗試一下。

百度上有提供sphinx+swcs中文分詞實現中文全文檢索的樣例,我沒有嘗試。

有興趣,請移步百度。

1:安裝sphinx

Sphinx官網:http://sphinxsearch.com/

安裝的檔案包也是在官網中。

Sphinx3.3.1版本是不需要編譯安裝的,下載下來,解壓,直接就能用。
這個和我目前正在使用的sphinx-for-chinese 是有不同的。

2:建立索引報錯

建立單個索引:

using config file '/usr/local/sphinx/etc/sphinx.conf'...
indexing index 'test1'...
ERROR: index 'test1': sql_connect: failed to load libmysqlclient (or libmariadb) (DSN=mysql://root:***@localhost:3306/test).

解決:

查詢libmysqlclient

find / -name 'libmysqlclient*'
/usr/lib64/mysql/libmysqlclient.so.18
/usr/lib64/mysql/libmysqlclient.so.18.0.0
/usr/local/mariadb/lib/libmysqlclient_r.so
/usr/local/mariadb/lib/libmysqlclient.a
/usr/local/mariadb/lib/libmysqlclient.so
/usr/local/mariadb/lib/libmysqlclient_r.a
/usr/local/download/mariadb-10.5.6/debian/libmysqlclient18.install
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient_r.so
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient.a
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient.so
/usr/local/download/mariadb-10.5.6/libmariadb/libmariadb/libmysqlclient_r.a

建立軟連線:

ln -s /usr/local/mariadb/lib/libmysqlclient.so /usr/lib/libmysqlclient.so
vim /etc/ld.so.conf

增加配置:

/usr/local/mariadb/bin/mysql # 你的資料庫執行檔案

再次建立索引成功:

/usr/local/sphinx/bin/indexer -c /usr/local/sphinx/etc/sphinx.conf  --all --rotate
/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop #停止服務

然後重啟

/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf #啟動服務

3:停止sphinx服務報錯:

/usr/local/sphinx/bin/searchd -c /usr/local/sphinx/etc/sphinx.conf --stop
Sphinx 3.3.1 (commit b72d67b)
Copyright (c) 2001-2020, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
using config file '/usr/local/sphinx/etc/sphinx.conf'...
FATAL: stop: pid file '/usr/local/sphinx/log/searchd.pid' does not exist or is not readable
shutdown complete

重啟伺服器再啟動sphinx就可以了。

4:sphinx配置檔案

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_uint   = date_added
}
  
index test1
{
    source          = src1
    path            = /usr/local/sphinx/data/test1
}
  
index testrt
{
    type            = rt
    rt_mem_limit    = 128M
  
    path            = /usr/local/sphinx/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/sphinx/log/searchd.log
    query_log       = /usr/local/sphinx/log/query.log
    read_timeout    = 5
    max_children    = 30
    pid_file        = /usr/local/sphinx/log/searchd.pid
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old      = 1
    workers         = threads # for RT to work
    binlog_path     = /usr/local/sphinx/data
}

5:建立資料表並寫入測試資料

Sphinx為我們提供了一個測試的sql檔案

檔案在安裝目錄下etc目錄中(example.sql),下邊是檔案內容:

DROP TABLE IF EXISTS test.documents;
CREATE TABLE test.documents
(
    id          INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
    group_id    INTEGER NOT NULL,
    group_id2   INTEGER NOT NULL,
    date_added  DATETIME NOT NULL,
    title       VARCHAR(255) NOT NULL,
    content     TEXT NOT NULL
);
  
REPLACE INTO test.documents ( id, group_id, group_id2, date_added, title, content ) VALUES
    ( 1, 1, 5, NOW(), 'test one', 'this is my test document number one. also checking search within phrases.' ),
    ( 2, 1, 6, NOW(), 'test two', 'this is my test document number two' ),
    ( 3, 2, 7, NOW(), 'another doc', 'this is another group' ),
    ( 4, 2, 8, NOW(), 'doc number four', 'this is to test groups' );
  
DROP TABLE IF EXISTS test.tags;
CREATE TABLE test.tags
(
    docid INTEGER NOT NULL,
    tagid INTEGER NOT NULL,
    UNIQUE(docid,tagid)
);
  
INSERT INTO test.tags VALUES
    (1,1), (1,3), (1,5), (1,7),
    (2,6), (2,4), (2,2),
    (3,15),
    (4,7), (4,40);
 

6:PHP呼叫sphinx

Demo.php

<?php
require("/usr/local/sphinx/api/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;
}
print_r($data);

這篇的細節可能寫的不太清楚,具體使用流程可以參照前兩篇的使用流程。

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

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

歡迎訪問小程式: