zoukankan      html  css  js  c++  java
  • yii2.0简单使用elasticsearch


    1、安装扩展
    /c/phpStudy/PHPTutorial/php/php-5.5.38/php /c/ProgramData/ComposerSetup/bin/composer.phar require --prefer-dist --ignore-platform-reqs yiisoft/yii2-elasticsearch:"~2.0.0"

    2、添加配置
    'es' => [
    'class' => 'yiielasticsearchConnection',
    'nodes' => [
    ['http_address' => '192.168.32.253:9200'],
    // ['http_address' => '192.168.32.253:80'],
    // configure more hosts if you have a cluster

    ],

    'connectionTimeout' => 2, //连接超时
    'autodetectCluster' => false
    ],

    3、model
    <?php

    namespace promoduleslogmodels;

    use yiielasticsearchActiveRecord;

    class Es extends ActiveRecord
    {

    public static $currentIndex;


    # 定义db链接
    public static function getDb()
    {
    return Yii::$app->get('es');
    }


    # 索引 数据库
    public static function index()
    {
    return 'megacorp';
    }

    # 类型 表名
    public static function type()
    {
    return 'employee';
    }

    //需要返回的字段
    public function attributes()
    {
    return [ 'first_name', 'last_name', 'age','about','interests'];

    }
    }

    添加
    $es = new Es();
    $es->primaryKey = 1; //设置_id
    $es->first_name = '35041801050';
    $es->last_name = '张鹏飞';
    $es->age = 22;
    $es->about = '游戏';
    $es->interests = ['asc'];
    $b = $es->insert(false);

    查询
    //查询所有记录
    Es::find()->all();

    //通过主键_id查询
    $customer = Es::get(1);

    //通过主键_id查询获取多条记录,return object
    $customers = Es::mget([1,2]);

  • 相关阅读:
    前端学习笔记系列一:5 在项目中引入阿里图标icon
    前端学习笔记系列一:3 Vue中的nextTick
    前端学习笔记系列一:4 vue中@click.native
    学习习惯
    美团作价27亿美元正式收购摩拜
    北京 一卡通 退卡
    愚人自以慧,智者自以愚。
    袁隆平分享8字成功经验
    性能计数器 叹号
    升级 windows 2016
  • 原文地址:https://www.cnblogs.com/fwqblogs/p/11957287.html
Copyright © 2011-2022 走看看