栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > xunsearch

tp5使用xunsearch

xunsearch 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tp5使用xunsearch

  1. xunsearch安装

部署时因项目原因服务器分两台(后端服务器,数据库服务器),以下内容在数据库服务器创建

mkdir /usr/src/xunsearch
cd /usr/src/xunsearch
wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2
  1. 然后解压、安装
tar -xjf xunsearch-full-latest.tar.bz2
cd /usr/src/xunsearch/xunsearch-full-1.4.15/
sh setup.sh

第一次安装的话,过程可能会稍显漫长,待命令运行结束后,如果没有出错中断则表示顺利安装完成。

安装过程中若出现问题,常见的有以下两种:
1.出现这种情况是因为没有安装g++ 执行:yum install gcc-c++
2.没有安装zlib模块 执行:yum install zlib-devel

  1. xunsearch启动
cd $prefix 		//$prefix 请务必替换为您的安装目录,而不是照抄。我这边安装目录是放在了/usr/local/xunsearch/
$prefix/bin/xs-ctl.sh -b local start    // 监听在本地回环地址 127.0.0.1 上
$prefix/bin/xs-ctl.sh -b inet start     // 监听在所有本地 IP 地址上
$prefix/bin/xs-ctl.sh -b a.b.c.d start  // 监听在指定 IP 上
$prefix/bin/xs-ctl.sh -b unix start     // 分别监听在 tmp/indexd.sock 和 tmp/searchd.sock

#服务端和SDK不在同一台服务器的情况:
$prefix/bin/xs-ctl.sh -b inet start #启动服务 绑定全部IP,适合SDK/服务端 在不同服务器的情况
$prefix/bin/xs-ctl.sh -b inet stop  #停止服务 若启动时指定了-b inet 此处也必须指定

我选择$prefix/bin/xs-ctl.sh -b inet start启动方式,监听在所有的本地的ip地址上

可通过进程以及端口查看服务是否启动成功,启动成功如下:
ps aux|grep xs

netstat -antp|grep 83*

  1. 加入开机自动启动xunsearch服务
vi /etc/rc.local
/usr/local/xunsearch/bin/xs-ctl.sh -b inet start  #将改行命令加在文件最后一行即可
保存退出

示例截图如下:

  1. 将xunsearch中的bin目录加入到环境变量中,以后可以在任意目录直接执行xunsearch/bin目录下的相关命令了
vi /etc/profile
 
export PATH=$PATH:/usr/local/xunsearch/bin
 
如果你的profile文件底部已经有了export PATH=$PATH......等省略一堆命令...... 那你直接在后面在加上一个冒号(:) 
然后在冒号后面加上xunsearch的bin目录的路径即可
 
保存退出

示例截图如下:

source /etc/profile #让配置立即生效
使用xs-ctl.sh start

  1. tp5安装xunsearch

首先 composer 安装,以下命令在后端服务器执行

composer require hightman/xunsearch

安装完成后,在 tp5 的 extend 目录下新建 xunsearch/XSSearch.php,内容如下:



    
    public static function xunsearch($keywords,$file = 'demo',$is_scws = false,$limit = 100){
        $xs = new XS($file);
        if($is_scws === true) {
            //中文分词
            $tokenizer = new XSTokenizerScws;
            //词语拆分
            $words = $tokenizer->getTokens($keywords);
            $where = '';
            //拼接成查询条件(OR)
            foreach ($words as $key => $val) {
                if ($key == 0) {
                    $where = $val;
                } else {
                    $where .= ' OR ' . $val;
                }
            }
        }else {
            $where = $keywords;
        }
        $result =  $xs->search->setQuery($where)
        //    ->setSort('id','asc') #按索引排序
            ->setDocOrder(true) #按添加索引排序(升序)
            ->setLimit($limit)
            ->search();
        $xs->search->close();

        return $result;
    }

    
    public function xunsearchSave($data,$file = 'demo'){
        try {
            $xs = new XS($file);
            #创建文档对象
            $doc = new XSDocument;
            $doc->setFields($data);

            #更新(新增)数据
            $index = $xs->index;
            $index->update($doc);

            #强制刷新当前索引列表数据
            $result = $index->flushIndex();

            return $result;
        }catch (Exception $e){
            return false;
        }
    }
}
  1. 创建索引与导入数据源

在我们安装号的 tp5 根目录下的 vender目录下找到安装好的 vendor/hightman/xunsearch的目录结构

_
|- doc/                    离线 HTML 版相关文档
|- app/                    用于存放搜索项目的 ini 文件
|- lib/XS.php              入口文件,所有搜索功能必须且只需包含此文件    
- util/                   辅助工具程序目录
    |- RequireCheck.php    用于检测您的 PHP 环境是否符合运行条件
    |- IniWizzaard.php     用于帮助您编写 xunsearch 项目配置文件
    |- Quest.php           搜索测试工具
    - Indexer.php         索引管理工具

本次用到的数据表

在 app 目录新建一个 intelligence .ini 文件,内容如下:

;项目名称
project.name = intelligence
;默认字符集
project.default_charset = utf-8
;服务器连接参数
    ;服务端连接参数的格式包含 3 种格式:
    ;1.端口号(数字),连接 localhost 的该端口号 (例:8383)
    ;2.地址:端口号,冒号连接地址(域名、IP地址)和端口 (例:127.0.0.1:8383)
    ;3.文件路径,本机的 unix socket 连接路径 (例:/tmp/index.sock)
;自 1.4.7 起,服务端地址可以使用 ; 分隔指定多个。 索引更新将同步到所有服务端,而搜索则随机从中挑选一个可用的服务端以达到均横效果。
; 索引服务端配置,默认值为 8383
server.index = 10.128.98.163:8383
; 搜索服务端配置,默认值为 8384
server.search = 10.128.98.163:8384

;字段选项
;每个字段根据实际情况指定字段选项,所有选项均有默认值,所以即便不指定任何选项而只有中括号定义的字段,那也是一个合法的字段,字段选项包括以下几种:
    ;type 字段类型
    ;string 字符型,适用多数情况,也是默认值
    ;numeric 数值型,包含整型和浮点数,仅当字段需用于以排序或区间检索时才设为该类型,否则请使用 string 即可
    ;date 日期型,形式为 YYYYmmdd 这样固定的 8 字节,如果没有区间检索或排序需求不建议使用
    ;id 主键型,确保每条数据具备唯一值,是索引更新和删除的凭据,每个搜索项目必须有且仅有一个 id 字段,该字段的值不区分大小写
    ;title 标题型,标题或名称字段,至多有一个该类型的字段
    ;body 内容型,主内容字段, 即本搜索项目中内容最长的字段,至多只有一个该类型字段,本字段不支持字段检索
;id字段
[pk_id]
type = id

;问题标题字段
[question]
type = title

;问题描述字段
[question_describe]
type = string

;问题回答字段
[answer]
type = body

;关键词字段
[key_word]
type = string

由于我数据库服务器是在内网的另外一台10.128.98.163
故此修改server.index = 10.128.98.163:8383,
若后端代码与数据库都在同一台服务器即server.index = 8383

  1. 导入索引数据源到 xunsearch

官网文档: http://www.xunsearch.com/doc/php/guide/util.indexer
注意:我这里是在项目根目录下的 vender/hightman/xunsearch 进行命令创建索引的

[root@localhost xunsearch]# util/Indexer.php --rebuild --source=mysql://root:123456@localhost/health/hd_intelligence --sql="SELECT pk_id,question,idx_dictionary_item_id,answer,question_describe,key_word FROM hd_intelligence WHERe status=1 AND is_common=0" --project=intelligence
-bash: util/Indexer.php: 权限不够

这时可能会有一个问题就是权限不够,这是因为文件util/Indexer.php没有执行权限,需要更改一下权限

[root@localhost xunsearch]# chmod -R 700 util

我这里更该了整个util目录的执行权限(推荐),然后在执行导入数据源就没问题了

[root@localhost xunsearch]# util/Indexer.php --rebuild --source=mysql://root:123456@localhost/health/hd_intelligence --sql="SELECt pk_id,question,idx_dictionary_item_id,answer,question_describe,key_word FROM hd_intelligence WHERe status=1 AND is_common=0" --project=intelligence
开始重建索引 ...
初始化数据源 ... mysql://root:123456@localhost/health/hd_intelligence 
开始批量导入数据 (请直接输入数据) ...
完成索引导入:成功 28 条,失败 0 条
完成重建索引 ...
[root@localhost xunsearch]# 

然后在命令行进行测试

关键词:卫生间 是我 intelligence 表的标题字段、关键词、问题描述三个字段其中的内容

[root@localhost xunsearch]# util/Quest.php intelligence 卫生间
在 3 条数据中,大约有 1 条包含 卫生间 ,第 1-1 条,用时:0.0042 秒。

1. 最近的卫生间在哪里? #00000000000000000009# [100%,1.13]
游泳馆直走右拐
Question_describe:这是一个关于养生谷路线问题
Key_word:停车场 路线

[root@localhost xunsearch]#

若遇到mysql导入数据不成功,这种情况索引库进入一个崩溃状态,发生这种情况是由于我数据库服务器端口8383和8384端口没开启成功
在后端服务器通过验证端口,查看是否端口开放成功
telnet 10.128.98.163 8384
回到数据库服务器重新执行开启端口命令
/usr/local/xunsearch/bin/xs-ctl.sh -b inet restart
启动成功

回到后端服务器使用util/Indexer.php --project=intelligence --stop-rebuild 清除刚刚崩溃的状态

关于util/Indexer.php --project=intelligence --stop-rebuild这句命令里的参数可在util/Indexer.php里面查看

清除崩溃状态在重新执行重建索引语句

[root@localhost xunsearch]# util/Indexer.php --rebuild --source=mysql://root:123456@localhost/health/hd_intelligence --sql="SELECt pk_id,question,idx_dictionary_item_id,answer,question_describe,key_word FROM hd_intelligence WHERe status=1 AND is_common=0" --project=intelligence
开始重建索引 ...
初始化数据源 ... mysql://root:123456@localhost/health/hd_intelligence 
开始批量导入数据 (请直接输入数据) ...
完成索引导入:成功 28 条,失败 0 条
完成重建索引 ...
[root@localhost xunsearch]# 

若重建索引时遇到Call to undefined function mysql_connect()
安装的php缺少mysql模块导致
yum install php-mysql -y

查看索引

[root@localhost xunsearch]# util/Indexer.php --info -p intelligence
---------- SERVER INFO BEGIN ----------
{
  id:"indexd", uptime:11392, num_burst:1, max_burst:1,
  num_accept:11, aps:0.0, num_task:6, tps:0.0,
  sock:7, name:"intelligence", home:"data/intelligence", rcv_size:8,
  flag:0x0000, version:"1.4.15"
}
DBS: [db] -> [NULL]
CMDS:
  -> {NULL}
---------- SERVER INFO END ----------
数据库名:db[0x0002]
队列数据:0条
导入进程:无
[root@localhost xunsearch]#

相关查阅文档
Xunsearch中文文档 https://www.kancloud.cn/baidu/xunsearch/62598
Xunsearch 官网 http://www.xunsearch.com/site/feature
全文索引xunsearch的详细讲解及sphinx的比较(笔记1)
https://blog.csdn.net/will5451/article/details/77186283?utm_source=blogxgwz9

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863743.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号