安裝測試Manticore Search

2018-09-30 约 1827 字 预计阅读 4 分钟

最近有個項目腹案。打算就用Wordpress作為前端,用Python作為後台提供內容。

但是Wordpress的搜索功能不夠強大,速度也非常慢。

如果貿然用不成熟的技術上新網站新項目,隻會起到事倍功半的反效果。

我這個博客就是用來做各類技術練手之用,通過這博客一步步實現,就能令自己的“能”每天增加一點點。

上個月重啟博客以來,今天翻過頭來看最初的簡陋網站,發現這個月還是學了不少新技術新知識。

“吾能即吾道”,能夠做的事情越多,道路則越寬,所謂的修行財侶法地也就是靠這每天進步一點點的“能”慢慢達成。

回歸正題。

現在搜索的OPEN SOURCE最知名的就是Elasticsearch,前些日子試著在自己的電腦上安裝了一下,一直配置不成功,估計是要求的硬件資源過於龐大。

本擬直接購買外部的Elasticsearch service罷了,也不算太昂貴,最低起步是16美元每月。

但是用外部的服務不太明了其中的過程,是比較省心省力,但是沒有達到格物致知的效果,不究竟。

還是想配置一遍,了解清楚,以後若流量大了需要更擴容的服務,再購買外部服務也能輕易轉換。

但Elasticsearch雖然是OPEN SOURCE,但是用JAVA編的,本身JAVA不是很熟悉,占用資源那是相當驚人。

目前的硬件條件裝這個軟件力有未逮。

本人一向是“究竟而不執著”,此時不能,就換一個解決方案。

找到與其競爭的Sphinx Search。用C++編製,這個C++的運行效率肯定就是高多了。

但這個項目感覺好長時間沒有更新了。有點老舊。

突然間發現一個Manticore,是Sphinx Search的分支項目,去年才開始上線,搜索研究了一下,發現一篇文章,評測師認為該項目比之Sphinx Search源碼要更好,bug更少

值得嚐試一下。

備份了現有服務器所有內容,開始增加搜索功能之旅。

安裝Manticore Search


首先到其網站上下載適合本服務器係統的最新版本。本服務器用的是Ubuntu 18.04。網頁上可以輕易找到最新版本鏈接。當前時間下載和安裝的命令如下:
wget https://github.com/manticoresoftware/manticoresearch/releases/download/2.7.3/manticore_2.7.3-180926-d07c4d43-stemmer.bionic_amd64-bin.deb

sudo dpkg -i manticore_2.7.3-180926-d07c4d43-stemmer.bionic_amd64-bin.deb

根據其安裝教程,還需要安裝幾個lib
sudo apt-get install libmysqlclient20 libodbc1 libpq5 libexpat1

然後啟動此服務
sudo systemctl start manticore

服務器啟動時也啟動此服務
sudo systemctl enable manticore

創建Manticore Search測試數據庫


先用root用戶名口令登陸mysql
mysql -u root -p

創建一個test數據庫
mysql> CREATE DATABASE test;

Manticore search提供了一個例子文件,導入到test數據庫
mysql> SOURCE /usr/share/doc/MANTICORE/example-conf/example.sql

退出mysql命令界麵
mysql> exit

好了,這樣就有了一個測試數據庫

配置Manticore search


因為manticore search是sphinx的分支項目,為了保持對元項目的兼容,配置文件仍然是一樣的。

故此編輯其配置文件
sudo nano /etc/sphinxsearch/sphinx.conf

將數據庫用戶口令修改進去:
source src1
{
# data source type. mandatory, no default value
# known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
type = mysql

#####################################################################
## SQL settings (for 'mysql' and 'pgsql' types)
#####################################################################

# some straightforward parameters for SQL source types
sql_host = localhost
sql_user = root
sql_pass = yourpassword
sql_db = test
sql_port = 3306 # optional, default is 3306

....

配置文件很長,暫時其他無需修改。

測試Manticore search收錄及檢索


test數據庫是在mysql裏麵儲存的。Manicore search需要將之收錄到自己的係統中。用下列命令:
sudo -u manticore indexer -c /etc/sphinxsearch/sphinx.conf test1 --rotate

出來一堆提示
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.160 sec, 1202 bytes/sec, 24.91 docs/sec
total 4 reads, 0.000 sec, 8.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=21729).

成功收錄。我們需要用mysql登陸到Manicore
mysql -h0 -P9306

到了mysql命令行
mysql> show TABLES;

看到下列結果
+--------+-------------+
| Index | Type |
+--------+-------------+
| dist1 | distributed |
| test1 | local |
| testrt | rt |
+--------+-------------+
3 rows in set (0.00 sec)

表示test1已經收錄成功了。我們就可以測試其檢索功能
mysql> CALL KEYWORDS ('one two three', 'test1', 1);
+------+-----------+------------+------+------+
| qpos | tokenized | normalized | docs | hits |
+------+-----------+------------+------+------+
| 1 | one | one | 1 | 2 |
| 2 | two | two | 1 | 2 |
| 3 | three | three | 0 | 0 |
+------+-----------+------------+------+------+
3 rows in set (0.00 sec)

文檔上有一些測試命令,可以參考測試。

不過測試之前,可以看一看他這個example寫了些啥。退出mysql命令行後,運行一下
cat /usr/share/doc/MANTICORE/example-conf/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);

可以根據其內容進行SQL命令的測試。

今天暫時就先測試到此,後麵要研究其跟wordpress的接口,屆時再寫一篇。

更新:跟Wordpress的接口與普通Mysql數據庫並無二致,請閱讀Manticore search教程2

 

 

 

author

Jesse Lau

網名遁去的一,簡稱遁一。2012年定居新西蘭至今,自由職業者。
本文采用知識共享署名 4.0 國際許可協議進行許可。簡而言之,可隨意轉發轉載,轉載請注明出處。


留点评论吧: