將Manticore Search與Wordpress數據庫結合起來

2018-10-03 约 834 字 预计阅读 2 分钟

前兩天安裝了Manticore Search,但隻是根據其安裝說明測試了一下,還沒有完全了解其概念。

今天又學習了一下,他的概念是將慢速的數據庫內容存在其內,然後用其高速算法檢索出id,再正常的應用即可。

這樣我就再做一個配置文件
sudo nano sphinx.conf

貼上下列內容:
#
# INDEX GROUP:
# MY BLOG
#
# SOURCES:
# src_my_blog
#
# INDEXES:
# idx_blog
#
#
source src_my_blog {
type = mysql
sql_host = localhost
sql_user = yourusername
sql_pass = yourpassword
sql_db = wordpress
sql_query_pre = SET NAMES utf8
sql_query = \
SELECT \
p.ID as ID, \
p.post_title as title, \
p.post_content as body, \
UNIX_TIMESTAMP(p.post_date) AS date_added \
FROM \
wp_posts as p \
WHERE \
p.post_type = 'post' AND \
p.post_status = 'publish';
sql_attr_string = title
sql_field_string = body
sql_attr_timestamp = date_added
}
index idx_blog {
source = src_my_blog
path = /var/lib/manticore/data/idx_blog
docinfo = extern
mlock = 0
min_word_len = 1
### support Chinese words query ###
charset_type = utf-8
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
ngram_len = 1
ngram_chars = U+3000..U+2FA1F
html_strip = 1
html_index_attrs = img=alt,title; a=title;
html_remove_elements = style, script, object, embed, span
}
#############################################################################
## indexer settings
#############################################################################
indexer
{
# memory limit, in bytes, kiloytes (16384K) or megabytes (256M)
# optional, default is 128M, max is 2047M, recommended is 256M to 1024M
mem_limit = 256M
}
#############################################################################
## searchd settings
#############################################################################
searchd
{
listen = 9312
listen = 9306:mysql41
log = /var/log/manticore/searchd.log
query_log = /var/log/manticore/query.log
read_timeout = 5
client_timeout = 300
max_children = 30
persistent_connections_limit = 30
pid_file = /var/run/manticore/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
mva_updates_pool = 1M
max_packet_size = 8M
max_filters = 256
max_filter_values = 4096
max_batch_queries = 32
workers = threads # for RT to work
binlog_path = /var/lib/manticore/data # binlog.001 etc will be created there
}
# --eof--

CTRL-X存盤退出。

運行檢索命令將其加入:
sudo indexer --config /path/sphinx.conf --all --rotate

再運行searchd命令,啟動search服務
sudo searchd --config /path/sphinx.conf

好了,我們再次mysql登陸到Manticore Search數據服務器
mysql -h0 -P9306

看一下數據表收錄沒有
mysql> show tables;

+----------+-------+
| Index | Type |
+----------+-------+
| idx_blog | local |
+----------+-------+
1 row in set (0.00 sec)

沒有問題,idx_blog進入了。

這下都是本博客的文章了。運行幾個命令測試一下
mysql> select * from idx_blog where match('天道');

出來11個結果



缺省是按相關度排序的。我們可以自己控製其排序,比如最新的帖子列在上麵,SQL命令如下
mysql> select * from idx_blog where match('天道') ORDER BY date_added DESC;

結果就是



說明收錄帖子,和搜索功能都沒有問題了。

但是咋跟PHP語言結合起來,顯示結果在Wordpress平台下的帖子裏麵呢?

w3schools找到一篇介紹PHP和AJAX的文章。學習了一下,做一個前端HTML。



然後再生成一個php文件,作為後台處理。



實驗了SphinxQL的snippet功能和ajax排序,代碼實現效果如下:

Manticore Search:



可以看出Snippet對中文支持還是不錯。字符全部都加黑了。

author

Jesse Lau

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


留点评论吧: