xunsearch安装和使用

xunsearch 2018-02-28 浏览(1781 评论(3

xunsearch安装

1、官网:
    http://xunsearch.com
2、下载:
    wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2
3、解压:
    tar -jxvf xunsearch-full-latest.tar.bz2
4、进入目录:
    cd /xunsearch-full-1.4.10
5、安装(无需编译安装):
    sh setup.sh
        1、安装过程中需要制定安装目录(/usr/local/xunsearch)
        2、首次安装可能需要几分钟,中间不出问题表示安装成功
6、启动测试
    1、cd /usr/local/xunsearch/bin
    2、./xs-ctl.sh start
7、添加开机启动
    在/usr/local/xunsearch/bin目录添加到环境系统变量中
    /usr/local/xunsearch/bin/xs-ctl.sh start
说明:
    1、索引服务(8383):负责索引的添加,修改和删除
    2、搜索服务(8384):负责提供搜索服务

项目配置文件

基本说明:
    1、项目配置保存为.ini文件,通常存储在/usr/local/xunsearch/sdk/php/app中
    2、每个项目必须只能有一个类型为ID的主键字段,ID字段值的字符不区分大小写
常用配置:
    1、名称:project.name = xxx
    2、字符集:project.default_charset = utf-8 (默认)
    3、索引服务字段:server.index = 8383       (默认)
    4、搜索服务端口:server.search = 8384      (默认)
项目字段:
    1、字段名:[字段名]
    2、字段类型:type = xxx
        1、字符集:string 字符型,适用多数情况,也是默认值
        2、数值型:numeric,包含整型和浮点数,仅当字段需用于以排序或区间检索时才设为该类型,
            否则请使用,string 即可
        3、日期型:date ,形式为 YYYYmmdd 这样固定的 8 字节,如果没有区间检索或排序需求不建议使用
        4、主键型:id ,确保每条数据具备唯一值,是索引更新和删除的凭据,每个搜索项目必须有且仅有一个 id         字段,该字段的值不区分大小写
        5、标题型:title ,标题或名称字段,至多有一个该类型的字段
        6、内容型:body ,主内容字段,即本搜索项目中内容最长的字段,至多只有一个该类型字段,本字段不支持字段    检索
        

yii2使用

1、首先要在配置文件 common/config/main-local.php 添加 xunsearch 组件:

'xunsearch' => [

    'class' => 'hightman\xunsearch\Connection', // 此行必须

    //'iniDirectory' => '@app/config',    // 搜索 ini 文件目录,默认:@vendor/hightman/app

    'iniDirectory' => '项目根目录/common/config/xunsearch',

    'charset' => 'utf-8',   // 指定项目使用的默认编码,默认即时 utf-8,可不指定

],

2、在 common/config 目录下添加 xunsearch 配置文件 search.ini, 下面是 search 配置:

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

[id]
type = id

[title]
type = title

[content]
type = body

[status]
index = self
tokenizer = full

[created]
type = numeric

3、新建一个 Search 的 Model 文件

<?php
namespace common\models;

use Yii;
use yii\data\ActiveDataProvider;

class Search extends \hightman\xunsearch\ActiveRecord
{
    public static function projectName() {
        return 'search';  // 这将使用 @app/config/xunsearch/another_name.ini 作为项目名
    }

    public function getInfo($page, $count, $content)
    {
        $offset = ($page - 1) * $count;
        return Search::find()->where($content)->limit($count)->offset($offset)->all();
    }
}

4、测试xunsearch

<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use common\models\Search;

class XunSearch extends Controller
{
    //添加索引
    public function Add()
    {
        $model = Search::findOne(1);
        if(!$model){
            $model = new Search;
        }
        $model->id = 1;
        $model->title = 'hello';
        $model->content = '你好';
        $model->save();
    }

    //xunsearch搜索
    public function Search()
    {
        $condition="你好";
        $page=1;
        $count=20;
        $result = Search::getInfo($page,$count,$condition);
        echo "<pre>";
        print_r($result);exit;

    }

}

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^

黄信强博客

最新评论

总共3条评论

  • 黄信强博客
    oneByone 评论 2018-06-27 09:16:32

    功能很完善 好评回复

  • 黄信强博客
    oneByone 评论 2018-06-27 09:16:13

    士大夫回复

  • 黄信强博客
    oneByone 评论 2018-06-27 09:16:11

    士大夫回复