centos6.5安装coreseek替换wordpress的搜索功能

本网站用的阿里云ECS,推荐大家用。自己搞个学习研究也不错

引言

实在受不了wordpress乌龟般慢的搜索速度了,感觉网站挂了似得。
之前已经苦逼过怎么优化wordpress了,参见另一篇文章数据过万后wordpress优化过程记录2, 也换成了全世界最好的编程语言php7,过程见于安装php7为wordpress提速。这次是要把wordpress自带的mysql like的搜索替换成全文搜索引擎。

安装coreseek

安装依赖包

yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel  expat-devel* mysql-server mysql

coreseek下载

官网打不开了,网上找了个包,放到了csdn上。

tar xzvf coreseek-3.2.14.tar.gz
cd coreseek-3.2.14

安装mmseg

$ cd mmseg-3.2.14
$ ./bootstrap    #输出的warning信息可以忽略,如果出现error则需要解决
$ ./configure --prefix=/usr/local/mmseg3
$ make && make install
$ cd ..
##如果提示libtool: unrecognized option `--tag=CC' ,请查看libtool问题解决方案
##安装完成后,mmseg使用的词典和配置文件,将自动安装到/usr/local/mmseg3/etc中
##中文分词测试,如果显示不正常,请检查当前环境下的locale和UTF-8中文字符显示设置
$  /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc mmseg-3.2.14/src/t1.txt
    中文/x 分/x 词/x 测试/x
    中国人/x 上海市/x

Word Splite took: 1 ms.

安装coreseek

$ cd csft-4.1
#执行configure,进行编译配置:
$ sh buildconf.sh
$ ./configure --prefix=/usr/local/coreseek  --without-unixodbc --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/ --with-mysql
$ make && make install

测试coreseek

cd ../testpack
$  /usr/local/coreseek/bin/indexer -c etc/csft.conf

以下为正常情况下的提示信息:

Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

ERROR: nothing to do.

$  /usr/local/coreseek/bin/indexer -c etc/csft.conf --all

以下为正常索引全部数据时的提示信息:
Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

using config file ‘etc/csft.conf’…
indexing index ‘xml’…
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 7585 bytes
total 0.007 sec, 979088 bytes/sec, 387.24 docs/sec
total 3 reads, 0.000 sec, 2.8 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 2.2 kb/call avg, 0.0 msec/call avg

$  /usr/local/coreseek/bin/indexer -c etc/csft.conf xml

以下为正常索引指定数据时的提示信息:
Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

using config file ‘etc/csft.conf’…
indexing index ‘xml’…
collected 3 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 3 docs, 7585 bytes
total 0.006 sec, 1260594 bytes/sec, 498.58 docs/sec
total 3 reads, 0.000 sec, 2.8 kb/call avg, 0.0 msec/call avg
total 9 writes, 0.000 sec, 2.2 kb/call avg, 0.0 msec/call avg

$  /usr/local/coreseek/bin/search -c etc/csft.conf

以下为正常测试搜索时的提示信息:
Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

using config file ‘etc/csft.conf’…
index ‘xml’: query ”: returned 3 matches of 3 total in 0.000 sec

displaying matches:
1. document=1, weight=1, published=Thu Apr 1 22:20:07 2010, author_id=1
2. document=2, weight=1, published=Thu Apr 1 23:25:48 2010, author_id=1
3. document=3, weight=1, published=Thu Apr 1 12:01:00 2010, author_id=2

words:

$  /usr/local/coreseek/bin/search -c etc/csft.conf -a Twittter和Opera都提供了搜索服务

以下为正常测试搜索关键词时的提示信息:
Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

using config file ‘etc/csft.conf’…
index ‘xml’: query ‘Twittter和Opera都提供了搜索服务 ‘: returned 0 matches of 0 total in 0.015 sec

words:
1. ‘twittter’: 1 documents, 3 hits
2. ‘和’: 3 documents, 15 hits
3. ‘opera’: 1 documents, 25 hits
4. ‘都’: 2 documents, 4 hits
5. ‘提供’: 0 documents, 0 hits
6. ‘了’: 3 documents, 18 hits
7. ‘搜索’: 2 documents, 5 hits
8. ‘服务’: 1 documents, 1 hits

$  /usr/local/coreseek/bin/searchd -c etc/csft.conf

以下为正常开启搜索服务时的提示信息:(csft-4.0版类似)
Coreseek Fulltext 4.1 [ Sphinx 2.0.2-dev (r2922)]
Copyright (c) 2007-2011,
Beijing Choice Software Technologies Inc (http://www.coreseek.com)

using config file ‘etc/csft.conf’…
WARNING: compat_sphinxql_magics=1 is deprecated; please update your application and config
listening on all interfaces, port=9312
precaching index ‘xml’
precached 1 indexes in 0.001 se

配置coreseek支持mysql数据源

配置csft_mysql.conf文件

复制mysql配置文件到coreseek安装目录etc/下(比如/usr/local/coreseek/etc/)

cp /usr/local/src/coreseek-3.2.14/testpack/etc/csft_mysql.conf /usr/local/coreseek/etc/
cd /usr/local/coreseek/etc/
vi csft_mysql.conf

源定义

source phperz
{
type = mysql

sql_host                = localhost
sql_user                = root
sql_pass                = xxxx
sql_db                    = phperz
sql_port                = 3306
sql_query_pre            = SET NAMES utf8

sql_query                = SELECT ID, post_author, UNIX_TIMESTAMP(post_date) AS post_date, post_title, post_content FROM wp_posts where post_status='publish' and post_type in ('page','post')
                                                          #sql_query第一列id需为整数
                                                          #title、content作为字符串/文本字段,被全文索引
sql_attr_uint            =  post_author#从SQL读取到的值必须为整数
sql_attr_timestamp        = post_date#从SQL读取到的值必须为整数,作为时间属性

sql_query_info_pre      = SET NAMES utf8                                        #命令行查询时,设置正确的字符集
sql_query_info            = SELECT * FROM wp_posts WHERE ID=$id #命令行查询时,从数据库读取原始数据信息

}

index定义

index phperz
{
source = phperz #对应的source名称
path = /usr/local/coreseek/var/data/phperz #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/…
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0

#中文分词配置,详情请查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
#charset_dictpath = etc/                             #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/...
charset_type        = zh_cn.utf-8

}

全局index定义

indexer
{
mem_limit = 128M
}

searchd服务定义

searchd
{
listen = 9312
read_timeout = 5
max_children = 30
max_matches = 1000
seamless_rotate = 0
preopen_indexes = 0
unlink_old = 1
pid_file = /usr/local/coreseek/var/log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/…
log = /usr/local/coreseek/var/log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/…
query_log = /usr/local/coreseek/var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/…
}

建立索引

路经部分需要改成你自己的地址
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf –all

可能出现的错误
ERROR: index ‘phperz’: sql_connect: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) (DSN=mysql://root:***@localhost:3306/phperz).
这是因为mysql的sock文件路经不正确导致的.
确认一下你的mysql.sock路经,建立一个软连接,比如
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

coreseek+php使用方式

复制安装目录下的/usr/local/src/coreseek-4.1-beta/testpack/api/sphinxapi.php文件到你的项目里
你的程序里include sphinxapi.php
php使用方法见/usr/local/src/coreseek-4.1-beta/testpack/api/test.php

wordpress修改, 把api目录拷贝到主题目录中后,修改search.php,我的修改如下(和主题相关,不能完全照抄,仅供参考)

<?php get_header(); ?>
<?php
//coreseek搜索结果
include_once( dirname(__FILE__) . "/api/sphinxapi.php" );
$cl = new SphinxClient ();
$cl->SetServer ( '127.0.0.1', 9312);
$cl->SetConnectTimeout(3);
$cl->SetMatchMode(1);
//以下设置用于返回数组形式的结果
$cl->SetArrayResult ( true );

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$start = ($paged - 1) * 10;
$cl->SetLimits($start,10);

$keyword =  $s = isset($_GET['s']) ? htmlspecialchars(trim($_GET['s'])) : ''; //获取搜索词
$res = $cl->Query ( "$keyword", "*" );


#print_r($res); //查看全文索引结果
$total = $res['total'];  //所有返回文章数,用于分页

if ( $res===false )
{
    print "Query failed: " . $cl->GetLastError() . ".\n";

} else
{
    if ( $cl->GetLastWarning() )
        print "WARNING: " . $cl->GetLastWarning() . "\n\n";
}
$have_posts = true;
if(isset($res['matches']) && !empty($res['matches'])) {
    foreach($res['matches'] as $value) {
        $id_arr[] = $value['id'];
    }
}
else
{
$have_posts = false;
}

$id_str = @implode(",", $id_arr);
$args  = array();
$args = array( 'include' => $id_arr);
wp_reset_query();
$sql = "select * from wp_posts where ID in($id_str) and post_type in ('post','page') AND post_status = 'publish'";  //根据ID读取文章数据
$data = $wpdb->get_results($sql);  //输出数据,之后你可以使用foreach数据$post object
?>
<div class="content-wrap">
        <div class="content">
                <?php echo _hui('ads_search_01_s') ? '<div class="ads ads-content">'.hui_get_adcode('ads_search_01').'</div>' : '' ?>
                <h1 class="title"><strong><?php echo htmlspecialchars($s); ?> <?php echo __('的搜索结果', 'haoui') ?></strong></h1>
                <?php if ( !$have_posts ) : ?>
                        <h3 class="text-muted text-center"><?php echo __('暂无搜索结果', 'haoui') ?></h3>
                <?php else: ?>
                        <?php get_template_part( 'excerpt' );  ?>
                <?php endif; ?>
        </div>
</div>

<?php
get_sidebar();

get_footer();
?>



coreseek日常维护

启动
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf
停止
/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft_mysql.conf –stop
建立索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf –all
重建索引
/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/csft_mysql.conf –all –rotate

你需要把启动命令加到开机自启动里
把重建索引命令加到计划任务里每天执行

后记

折腾了3小时才搞定,希望对大家有用。可以访问我的网站试试搜索效果。写这篇博客的时候,我的博客已经快3万了哦。

经查证,直接修改search.php没有用。因为sql查询在之前就已经查了,search.php相当于一个模板页面了。

阅读完原代码,修改了wp-include/query.php, 终于优化完成了。

优化前:

Page generated in 2.18491s, 38.61% PHP, 61.39% MySQL

优化后:

Page generated in 0.54773s, 95.51% PHP, 4.49% MySQL

 

未经允许不得转载:演道网 » centos6.5安装coreseek替换wordpress的搜索功能

赞 (0)
分享到:更多 ()