最新消息:

WordPress 整合 CoreSeek 全文索引搜索

wordpress admin 3907浏览 0评论

CoreSeek快速安装

wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz
tar xzvf coreseek-3.2.14.tar.gz

cd mmseg-3.2.14
./bootstrap
./configure –prefix=/usr/local/mmseg3
make
make install
cd ..
cd csft-3.2.14
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 ##如果提示mysql问题,可以查看MySQL数据源安装说明
make && make install
cd ..

安装完测试
cd testpack
cat var/test/test.xml
/usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc var/test/test.xml
/usr/local/coreseek/bin/indexer -c etc/csft.conf –all
/usr/local/coreseek/bin/search -c etc/csft.conf 网络搜索

参考:http://www.coreseek.cn/products-install/install_on_bsd_linux/

如果遇到 libiconv 错误 安装 coreseek 错误 undefined reference to `libiconv`

 

整合 WordPress 搜索

cd /usr/local/coreseek/etc/
vi aikaiyuan.conf
source mysql
{
    type                    = mysql
    sql_host                = 127.0.0.1
    sql_user                = root       # 用户名
    sql_pass                = 123456     # 密码
    sql_db                  = wordpress  # wordpress的数据库
    sql_port                = 3306
    sql_query_pre           = SET NAMES utf8

    sql_query                = SELECT id,  UNIX_TIMESTAMP(post_date) AS date_added, post_content, post_title FROM wp_posts
    sql_attr_uint            = id
    sql_attr_timestamp       = post_date

    sql_query_info_pre      = SET NAMES utf8
    sql_query_info          = SELECT * FROM wp_posts WHERE id=$id
}

index mysql
{
    source            = mysql
    path              = /usr/local/coreseek/var/aikaiyuan/mysql
    docinfo           = extern
    mlock             = 0
    morphology        = none
    min_word_len      = 1
    html_strip        = 0

    charset_dictpath  = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
    charset_type      = zh_cn.utf-8
}

indexer
{
    mem_limit            = 32M
}

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
    log       = /usr/local/coreseek/var/log/searchd_mysql.log
    query_log = /usr/local/coreseek/var/log/query_mysql.log
}

创建索引

/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/aikaiyuan.conf –all

启动服务

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/aikaiyuan.conf

停止服务

/usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/aikaiyuan.conf –stop

添加crontab每天4点重建索引

00 03 * * * /usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/aikaiyuan.conf –all –rotate

1

wordpress全文搜索页面

 

1、在解压的coreseek的文件中大家可以看到有个testpack目录,进入testapck/api我们可以看到有很多接口文件。我们可以直接拷贝这个api目录到我们的主题目录下,呆会搜索页面将会使用到该文件:

cd coreseek-3.2.14/testpack/api
ll
我们可以看到api的目录文件如下
drwxrwxrwx 2 root root  4096 Jan 12  2011 java               java接口
drwxrwxrwx 2 root root  4096 Jan 12  2011 libsphinxclient
drwxrwxrwx 5 root root   138 Jan 12  2011 ruby
-rwxrwxrwx 1 root root 44399 May  7  2010 sphinxapi.php      php接口
-rwxrwxrwx 1 root root 26251 May  7  2010 sphinxapi.py
-rwxrwxrwx 1 root root  1053 May  7  2010 test2.php          php测试文件
-rwxrwxrwx 1 root root   652 May  7  2010 test2.py
-rwxrwxrwx 1 root root   763 Jan 12  2011 test_coreseek.php
-rwxrwxrwx 1 root root  5656 Jun  8  2010 test.php
-rwxrwxrwx 1 root root  3377 May  7  2010 test.py

以下的文件对我们有用的就是sphinxapi.php,他是coreseek的php调用接口,test.php与test2.php是全文索引的测试文件,大家可以参考下,其他文件可以删除。将api目录复制到我们的主题目录后,我们在搜索页面search.php中全文索引的代码如下:

 

 

<?php
include_once( dirname(__FILE__) . "/api/sphinxapi.php" );
$cl = new SphinxClient ();
$cl->SetServer ( '127.0.0.1', 9312);
//以下设置用于返回数组形式的结果
$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(!empty($res['matches'])) {
        foreach($res['matches'] as $value) {
                $id_arr[] = $value['id'];
        }
}

$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='post' AND post_status = 'publish'";  //根据ID读取文章数据

$data = $wpdb->get_results($sql);  //输出数据,之后你可以使用foreach数据$post object
if( is_search() && empty($id_arr)) { echo '暂无搜索结果!'; }else{

#print_r ($data);
foreach($data as $post) {
        echo "<br>";
?>
<h1 class="entry-title"><a href="http://www.aikaiyuan.com/<?php echo the_ID($post);?>.html" title="<?php echo get_the_title($post);?>" rel="bookmark"><?php echo get_the_title($post);?></a></h1>
<?php
    //echo get_the_title($post);  //输出文章标题
//      echo the_ID($post);
        echo the_excerpt(post);
        //echo get_post($post);
}
}
?>

1
1
1

转载请注明:爱开源 » WordPress 整合 CoreSeek 全文索引搜索

您必须 登录 才能发表评论!