溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

lnmp+coreseek實現(xiàn)站內(nèi)全文檢索(安裝篇)

發(fā)布時間:2020-08-10 07:43:40 來源:網(wǎng)絡(luò) 閱讀:583 作者:夜游神qi 欄目:web開發(fā)
  • 軟件安裝包

    安裝環(huán)境

  • 系統(tǒng)環(huán)境
    • centos7.2
    • 1核2G
  • 軟件環(huán)境
    • coreseek-3.2.14
    • lnmp1.5

安裝mmseg

  • 更新依賴包和安裝編譯環(huán)境
yum -y install m4 autoconf automake libtool
yum -y install gcc gcc-c++ wget
yum -y install mysql-devel

安裝coreseek

tar -xzvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14
cd mmseg-3.2.14/
./bootstrap
./configure --prefix=/usr/local/mmseg3
make
make install

cd ../csft-3.2.14/
sh buildconf.sh
./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql --host=arm
make
make install

<font color='red'>在安裝csft的過程中出現(xiàn)了三種錯誤情況,錯誤情況如下錯誤記錄,可以參考修改即可</font>

配置coreseek配置文件

配置文件主要修改的幾個地方如下,具體的修改信息請參見 coreseek配置文件 目錄的進行配置

source src1 
{     
type = mysql                                #數(shù)據(jù)庫類型
sql_host = localhost                    # MySQL主機IP
sql_user = root                             # MySQL用戶名
sql_pass = 123                          # MySQL密碼
sql_db   =  test                        # MySQL數(shù)據(jù)庫
sql_port = 3306                     # MySQL端口
sql_sock = /tmp/mysql.sock              #如果是linux下需要開啟,指定sock文件
sql_query_pre = SET NAMES UTF8      # MySQL檢索編碼
sql_query_pre = SET SESSION query_cache_type=OFF    #關(guān)閉緩存
sql_query   = \                             #獲取數(shù)據(jù)的SQL語句 
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
}                                           #這行不需要修改
source srclthrottled:srcl               #繼承主數(shù)據(jù)源
主數(shù)據(jù)源索引:
index text1
{   
source = src1                           #索引源聲明
charset_type = utf-8                #數(shù)據(jù)編碼(設(shè)置成utf8)
charset_table =                     #上面指定了utf-8,這里需要開啟
}
增量索引
index testlstemmed:test1        #先進行注釋
index distl                         #分布式也注釋掉
索引器設(shè)置
indexer 
{ 
mem_limit = 256M # 內(nèi)存大小限制 默認是 32M, 推薦為 256M 
}                                       #其他用默認即可
sphinx服務(wù)進程searchd的相關(guān)配置
searchd {
}                                       #全部用默認的就可以了

除了上述的配置項外還需要單獨配置幾項(如果你是安裝的sphinx則進行上述配置即可,如果是coreseek則還需要配置下述內(nèi)容)

index test1
{
#stopwords                      = G:\data\stopwords.txt
#wordforms                      = G:\data\wordforms.txt
#exceptions                         = /data/exceptions.txt
#charset_type                       = sbcs
添加下面這兩行,意思是把中文分詞加入到配置文件中
charset_type        = zh_cn.utf-8
charset_dictpath    = /usr/local/mmseg/etc/    #你安裝mmseg的目錄
}

數(shù)據(jù)庫操作

// 創(chuàng)建數(shù)據(jù)庫
create database test;
// 創(chuàng)建數(shù)據(jù)表
userinfo | CREATE TABLE `userinfo` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `age` int(3) NOT NULL,
  `sex` enum('女','男') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4
// 插入數(shù)據(jù)
INSERT INTO `userinfo` VALUES (1,'張三',12,'女'),(2,'李四',13,'男'),    
(3,'小明',12,'女'),(4,'小紅',13,'女'),(5,'小四',12,'女'),(6,'章澤天',13,'女');

創(chuàng)建索引數(shù)據(jù)(indexer)

// 第一次創(chuàng)建索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf --all
// 第二次創(chuàng)建所以(當(dāng)我們第一次創(chuàng)建索引之后,對數(shù)據(jù)表做了新的操作再按照上面的方式生成索引是會多增加一個參數(shù))
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft.conf  -rotate option

如果上述的操作執(zhí)行正確,則會提示如下信息

Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

 using config file '/usr/local/coreseek/etc/csft.conf'...
WARNING: no such index '-rotate', skipping.
WARNING: no such index 'option', skipping.
total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

測試索引數(shù)據(jù)(search)

/usr/local/coreseek/bin/search '小'

正常情況下會出現(xiàn)下述信息

Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

 using config file '/usr/local/coreseek/etc/csft.conf'...
index 'test1': query '小 ': returned 3 matches of 3 total in 0.004 sec

displaying matches:
1. document=3, weight=1
2. document=4, weight=1
3. document=5, weight=1

words:
1. '小': 3 documents, 3 hits

啟動searchd服務(wù)

該服務(wù)是可以在/usr/local/coreseek/etc/csft.conf文件中的searchd配置項中配置,默認服務(wù)的端口已經(jīng)配置,可以不單獨做配置

// 啟動服務(wù)
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf
// 檢測服務(wù)啟動情況
[root@test ~]# netstat -anp | grep 9312
tcp        0      0 0.0.0.0:9312            0.0.0.0:*               LISTEN      31672/searchd

創(chuàng)建測試代碼

測試代碼可以參見分享文件中的 測試代碼 目錄

require './api/sphinxapi.php';
$sphinx = new SphinxClient();
$sphinx->SetServer('127.0.0.1', 9312);
$sphinx->SetArrayResult(true);
$sphinx->SetMatchMode(SPH_MATCH_ALL);
$sphinx->SetSortMode(SPH_SORT_RELEVANCE);
$result = $sphinx->query('小', '*');
var_dump($result);

查詢出的結(jié)果如下

array(10) {
  ["error"]=>
  string(0) ""
  ["warning"]=>
  string(0) ""
  ["status"]=>
  int(0)
  ["fields"]=>
  array(3) {
    [0]=>
    string(4) "name"
    [1]=>
    string(3) "age"
    [2]=>
    string(3) "sex"
  }
  ["attrs"]=>
  array(0) {
  }
  ["matches"]=>
  array(3) {
    [0]=>
    array(3) {
      ["id"]=>
      int(2)
      ["weight"]=>
      string(1) "1"
      ["attrs"]=>
      array(0) {
      }
    }
    [1]=>
    array(3) {
      ["id"]=>
      int(3)
      ["weight"]=>
      string(1) "1"
      ["attrs"]=>
      array(0) {
      }
    }
    [2]=>
    array(3) {
      ["id"]=>
      int(4)
      ["weight"]=>
      string(1) "1"
      ["attrs"]=>
      array(0) {
      }
    }
  }
  ["total"]=>
  string(1) "3"
  ["total_found"]=>
  string(1) "3"
  ["time"]=>
  string(5) "0.003"
  ["words"]=>
  array(1) {
    ["小"]=>
    array(2) {
      ["docs"]=>
      string(1) "3"
      ["hits"]=>
      string(1) "3"
    }
  }
}

coreseek錯誤記錄

安裝coerseek問題一

cd ../csft-3.2.14/????? 

sh buildconf.sh

./configure --prefix=/usr/local/coreseek --without-python --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql --host=arm

?vi src/sphinxexpr.cpp? #然后將所有的T val = ExprEval ( this->m_pArg, tMatch ).....修改為T val =this->ExprEval ( this->m_pArg, tMatch ),共有三處。

安裝coreseek錯誤二

make[2]: *** [tokenizer_zhcn.o] Error 1
make[2]: Leaving directory `/home/zyf/zyfwork/csft3.1b3/src’
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/zyf/zyfwork/csft3.1b3/src’
make: *** [all-recursive] Error 1

/usr/local/sphinx/src/sphinx.cpp:15557: undefined reference to `libiconv_open’
libsphinx.a(sphinx.o)(.text+0x53a01):/usr/local/sphinx/src/sphinx.cpp:15575: undefined
reference to `libiconv’
libsphinx.a(sphinx.o)(.text+0x53a28):/usr/local/sphinx/src/sphinx.cpp:15581: undefined
reference to `libiconv_close’
collect2: ld returned 1 exit status
make[2]: * [indexer] Error 1
make[2]: Leaving directory `/usr/local/sphinx/src’
make[1]: * [all] Error 2
make[1]: Leaving directory `/usr/local/sphinx/src’
make: * [all-recursive] Error 1

修改 configure 文件把 #define USE_LIBICONV 0 最后的數(shù)值由1改為0

安裝coreseek問題三

db_interface_mysql.h:32:25: 致命錯誤:mysql/mysql.h:沒有那個文件或目錄
// ubuntu系統(tǒng)解決方案
sudo apt-get install libmysqlclient-dev
// centos7解決方案
sudo yum install mysql-devel

參考鏈接

參考鏈接一
參考鏈接二
參考鏈接三

coreseek額外說明

安裝路徑

/usr/local/coreseek

配置目錄

/usr/local/coreseek/etc/

執(zhí)行程序目錄

/usr/local/coreseek/bin/

配置文件說明

進入配置目錄,你會找到如下幾個文件

-rw-r--r--. 1 root root   905 10月 13 21:27 example.sql
-rw-r--r--. 1 root root 18954 10月 13 21:27 sphinx.conf.dist
-rw-r--r--. 1 root root   956 10月 13 21:27 sphinx-min.conf.dist

將sphinx.conf.dist文件復(fù)制一份名為csft.conf即可.其余文件可以不管.

執(zhí)行程序說明

進入執(zhí)行程序目錄可以看到如下幾個文件,具體參考請見sphinx文檔

-rwxr-xr-x. 1 root root 7032560 10月 13 21:27 indexer // 生成索引程序
-rwxr-xr-x. 1 root root 6650552 10月 13 21:27 indextool // 調(diào)試程序
-rwxr-xr-x. 1 root root 6740544 10月 13 21:27 search  // 搜索程序
-rwxr-xr-x. 1 root root 7970448 10月 13 21:27 searchd  // 提供客戶端查詢索引服務(wù)
-rwxr-xr-x. 1 root root 6370256 10月 13 21:27 spelldump // 提取字典內(nèi)容

分享文件目錄說明

├── api.zip // coreseek類文件(該文件和測試代碼目錄下的api.zip是一樣的)
├── coreseek-3.2.14.tar.gz // coreseek安裝程序文件
├── coreseek配置文件 
│?? └── csft.conf // 上述 配置coreseek配置文件 步驟中提交到的配置文件
├── 測試代碼 
│?? ├── api.zip
│?? └── index.php // 測試代碼
└── 測試數(shù)據(jù)庫
    └── test.sql // 測試數(shù)據(jù)庫
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI