1、运行下面指令下载、解压安装包

wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2
tar -xjf xunsearch-full-latest.tar.bz2

2、执行安装脚本,根据提示进行操作,主要是输入 xunsearch 软件包的安装目录,强烈建议单独 规划一个目录,而不是混到别的软件目录中。

cd xunsearch-full-1.3.0/
sh setup.sh

3、待命令运行结束后,如果没有出错中断,则表示顺利安装完成,然后就可以启动/重新启动 xunsearch 的后台服务,下面命令中的 $prefix 请务必替换为您的安装目录,而不是照抄。

cd $prefix ; bin/xs-ctl.sh restart

默认是安装在/usr/local/xunsearch下的

4、强烈建议您将此命令添加到开机启动脚本中,以便每次服务器重启后能自动启动搜索服务程序, 在 Linux 系统中您可以将脚本指令写进 /etc/rc.local 即可。

5、默认情况 xs-ctl.sh 启动的服务程序是绑定并监听在 127.0.0.1 上,如果是xunsearch和web服务器不是同一台服务器那么启动xunsearch的时候使用下面的命令

bin/xs-ctl.sh -b 61.139.2.69 start  // 监听在指定 IP 上

6、PHP-SDK 的代码不需要另行下载,已经包含在 xunsearch 的安装结果中了,在此假设您将 xunsearch 安装在 $prefix 目录,那么 $prefix/sdk/php 即是 PHP-SDK 的代码目录。

7、检测xunsearch运行环境是否符合

php $prefix/sdk/php/util/RequiredCheck.php

8、配置xunsearch索引项目,可以使用xunsearch官方提供的在线生成工具生成http://www.xunsearch.com/tools/iniconfig

配置文件相关说明http://www.xunsearch.com/doc/php/guide/ini.guide,示例

project.name = blog
project.default_charset = utf-8
server.index = 192.168.1.10:8383
server.search = 192.168.1.10:8384

[article_id]
type = id

[category_name]
type = string

[views]
type = numeric

[comments]
type = numeric

[cover]

[title]
type = title

[summary]
index = mixed

[content]
type = body

[created_at]
type = numeric


9、导入mysql数据

/usr/local/xunsearch/sdk/php/util/Indexer.php --rebuild --source=mysql://root:xxx@localhost/blog --sql="select a.article_id,c.category_name,a.views,a.comments,a.cover,a.title,a.summary,a.content,a.created_at from article a left join categories c on c.category_id = a.category_id" --project=blog

10、生成搜索骨架代码,项目开发可以基于这个骨架代码进行改写

# 在指定的 /path/to/web 目录生成 blog 搜索代码,代码目录为:/path/to/web/demo
util/SearchSkel.php blog /path/to/web
[blog]
  |- search.php     # 搜索功能入口
  |- search.tpl     # 搜索结果输出模板文件
  \- suggest.php    # 提取搜索输入框下拉建议,通过 autocomplete 组件自动调用


运行骨架文件search.php,效果如下

6.png

最后参考search.php中的代码,将代码移植到项目中去。


示例代码

<?php
require_once './sdk/lib/XS.php';
//$xs = new XS('blog');
//$index = $xs->index;
//$index->clean();
//exit;

$q = stripslashes($_GET['q']);
$p = isset($_GET['p']) ? max(1, intval($p)) : 1;

try {
    $xs = new XS('blog');
    $search = $xs->search;
    $search->setCharset('UTF-8');
    $search->setFuzzy(true);
    $search->setQuery($q);
    $search->setSort('article_id', false, true);
    $search->setLimit(XSSearch::PAGE_SIZE, ($p - 1) * XSSearch::PAGE_SIZE);

    $search_begin = microtime(true);
    $docs         = $search->search();
    $search_cost  = microtime(true) - $search_begin;

    // 根据keyword检索出来的总数
    $count = $search->getLastCount();
    // 所有数据条数
    $total = $search->getDbTotal();

    // 搜索建议 您是不是要找XXX
    $corrected = [];
    if ($count < 1 || $count < ceil(0.001 * $total)) {
        $corrected = $search->getCorrectedQuery();
    }

    // 相关搜索
    $related = $search->getRelatedQuery();

    // 热门搜索
    $hot = $search->getHotQuery();

    var_dump($docs);
    var_dump($count);
    var_dump($total);

    var_dump($corrected);
    var_dump($related);
    var_dump($hot);
} catch (XSException $e) {
    var_dump($e);
}