WordPress使用Manticore search实现高效搜索-CentOS系统

Manticore Search是sphinx的分支开发版本,比sphinx的bug更少,使用更简单,且可以实现实时搜索,支持SNIPPET语法,还有很多高级用法,请至官方网站浏览manticoresearch.com

Manticore search支持以下centos/RHEL的32位和64位系统
CentOS 6 and RHEL 6
CentOS 7 and RHEL 7
CentOS 8 and RHEL 8

老牛最开始是在博客上使用了,参照本站搜索页面的高级搜索功能

最近一个WordPress站点准备上Manticore search,系统采用的是CentOS 6.8 64位,以下为安装过程记录

一、添加必要的环境组件

1
yum install mysql-libs postgresql-libs expat unixODBC -y

注意,对于要使用的源类型,一般只需要lib。如果您计划只从mysql数据库创建索引,那么安装mysql libs就足够了。如果您根本不打算使用“indexer”工具(创建索引的工具),则不需要安装这些软件包。
安装完成如下:

如果需要ICU开发包支持,需要额外安装以下包,本教程不安装

1
yum install libicu -y

二、安装Manticore Search有两种方法:
1、通过yum安装(本次教程,采用方式)

1
yum install https://repo.manticoresearch.com/manticore-repo.noarch.rpm -y
1
yum install manticore -y


安装完成后

1
2
3
Running Transaction
Installing : manticore-3.4.0_200327.b212975-1.el6.x86_64                                                                            1/1
Manticore Search (https://manticoresearch.com)

Getting Started with Manticore Search:
https://docs.manticoresearch.com/latest/html/getting-started.html

Learn Manticore with interactive courses:
https://play.manticoresearch.com/

To start Manticore Search service:
> service manticore start/stop

Configuration file:
/etc/manticoresearch/manticore.conf

Verifying : manticore-3.4.0_200327.b212975-1.el6.x86_64 1/1

Installed:
manticore.x86_64 0:3.4.0_200327.b212975-1.el6

2、通过rpm进行安装

1
wget https://github.com/manticoresoftware/manticoresearch/releases/download/3.4.0/manticore-3.4.0_200327.b212975-1.el7.centos.x86_64.rpm
1
rpm -Uhv manticore-3.4.0_200327.b212975-1.el7.centos.x86_64.rpm

三、新版Manticore Search控制命令
以默认配置启动Manticore Search服务
centos7

1
systemctl start manticore

centos6

1
service manticore start

或者以指定配置wpindex.conf启动

1
searchd --config /etc/manticoresearch/wpindex.conf


注意监听的端口!测试下manticore是否工作正常

以默认配置停止Manticore Search服务

1
service manticore stop

停止wpindex.conf配置的搜索服务

1
searchd --config /etc/manticoresearch/wpindex.conf --stop

添加Manticore Search自启动

1
chkconfig --level 345 manticore on

四、添加wp的索引和搜索配置

针对WordPress需要索引的字段编写wpindex.conf配置文件,这个有点儿多,暂时不写,有需要的可以留言,我再写,我也是参照已经定居新西兰的一个程序猿的教程搞的

建立必要的目录

1
mkdir /var/lib/manticore/data
1
cd /var/lib/manticore
1
chmod 777 data

要建立索引
前提是searchd服务器已经启动并监听了9312和9306端口(即第三节中已经按指定wpindex.conf配置启动Manticore Search服务)

添加针对本次需要索引的WordPress数据库配置文件并启用搜索监听服务

1
cd /etc/manticoresearch
1
vi /etc/manticoresearch/wpindex.conf

写入以下配置(自行去官网研究)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# INDEX GROUP:
# MY BLOG
#
# SOURCES:
# src_my_blog
#
# INDEXES:
# idx_blog
#
#
source src_my_blog {
    type = mysql
    sql_host = 127.0.0.1
    sql_user = root
    sql_pass = 这里输入你的mysql的root用户密码
    sql_db = 这里是你需要建立索引的数据库
    sql_query_pre = SET NAMES utf8
    sql_query = \
    SELECT DISTINCT \
    wp_posts.ID as ID, \
    wp_posts.post_title as title, \
    wp_posts.post_content as body, \
    wp_posts.guid as urlid, \
    UNIX_TIMESTAMP(wp_posts.post_date) AS date_added \
    FROM \
    wp_posts \
    WHERE \
    wp_posts.post_type = 'post' AND \
    wp_posts.post_status = 'publish';
    sql_field_string = title
    sql_field_string = body
    sql_field_string = urlid
    sql_attr_timestamp = date_added
    }
    index idx_blog {
    type   = plain
    source = src_my_blog
    path = /var/lib/manticore/data/idx_blog
    min_word_len = 1
    ### support Chinese words query ###
    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           = 127.0.0.1:9312
    listen           = 127.0.0.1:9306:mysql41
    log              = /var/log/manticore/searchd.log
    query_log        = /var/log/manticore/query.log
    pid_file         = /var/run/manticore/searchd.pid

    read_timeout = 5
    client_timeout = 300
    max_children = 30
    persistent_connections_limit = 30
    seamless_rotate = 1
    preopen_indexes = 1
    unlink_old = 1
    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--

手动建立索引

1
indexer --config /etc/manticoresearch/wpindex.conf --all --rotate

测试搜索及索引是否正常,ssh root登录状态进入mysql控制台

1
mysql -h0 -P9306

六、新建crontab任务,每天凌晨4点定时更新索引
在你指定的文件夹内新建一个manticore.sh文件,写入以下命令(参考上面的控制常用命令文章)
/codefiles/crontab是我存放定时任务的shell脚本目录,根据你自己的来

1
cd /codefiles/crontab
1
vi manticore.sh

在manticore.sh内写入以下命令

1
2
3
#!/bin/bash
searchd --config /etc/manticoresearch/wpindex.conf --stop && indexer --config /etc/manticoresearch/wpindex.conf --all --rotate && searchd --config /etc/manticoresearch/wpindex.conf
exit 1

添加执行权限

1
chmod +x manticore.sh
1
crontab -e

vi编辑状态下(按i键),添加以下任务(每天凌晨4点执行索引更新)

1
 0 4 * * * /codefiles/crontab/manticore.sh

七、Manticore Search与WordPress结合,使用ajax来展现搜索结果
进入WordPress主题根目录
新建js文件manticore_search.js
写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function displaysort() {
var q=document.getElementById("manticoresearch").value;
if (q==""){
return;
}
    var x = document.getElementById("sort");
    if (x.style.display === "none") {
        x.style.display = "block";
    }
    var sort= document.getElementById("sortoption").value;
    search(sort);
}
function search(str) {
  if (str=="") {

    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("searchresult").innerHTML=this.responseText;
    }
  }
  var q=document.getElementById("manticoresearch").value;
//
  xmlhttp.open("GET","//你网站域名/wp-content/themes/你主题文件夹名称/manticore_search.php?q="+q+"&sort="+str,true);
  xmlhttp.send();
}

然后在主题根目录新建一个manticore_search.php文件
写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
$q = $_GET['q'];
$sort = $_GET['sort'];
$manticore = new mysqli("127.0.0.1", "", "", "", 9306);
$q=$manticore->real_escape_string($q);
if ($sort=="1")
{
$sphinxQl="SELECT * ,SNIPPET(title,'".$q."','limit=100') as snippet FROM idx_blog WHERE MATCH('".$q."') OPTION max_matches=100";
}
else
{
$sphinxQl="SELECT * ,SNIPPET(title,'".$q."','limit=100') as snippet FROM idx_blog WHERE MATCH('".$q."') ORDER BY date_added DESC";
}
$result = $manticore->query($sphinxQl);

echo "高级搜索结果:";
echo "<ul class='search-page'>";
foreach($result as $row)
{
echo "<li class='entry-title'><a href=".str_ireplace('?p=','',($row['urlid'])).".html".">".highlight_word($row['title'],$q).PHP_EOL."</a></li>";
}
echo "</ul>";
function highlight_word( $content, $word) {
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
    $replace = '<span style="background-color: #FF0;">' . $word . '</span>'; // create replacement
    $content = str_ireplace( $word, $replace, $content ); // replace content
    return $content; // return highlighted data
}
?>

注意:上面的$replace后面的代码可能需要根据你的重写规则进行小调整

然后修改主题的search.php文件,在合适的地方加入以下代码(html内容及样式根据自己主题修改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--manticore search start-->
<script type="text/javascript" src="//你网站域名/wp-content/themes/你主题文件夹名称/manticore_search.js"></script>
<p><input id="manticoresearch" name="search" type="text" />
<button type="button" onclick="displaysort()">高级搜索</button></p>
<div id="sort" style="display:inline-block">
<form>
  <select name="users" id="sortoption" onchange="search(this.value)">
  <option value="1">排序:</option>
    <option value="1">按相关性</option>
    <option value="2">按发布时间</option>
  </select>
  </form>
</div>
<div id="searchresult"></div>
<!--manticore search end-->
原文链接:https://xiaohost.com/3170.html,转载请注明出处。
0

评论1

请先
  1. 来学习学习
    一诺千金 2020-05-27 0