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]);

  • 相关阅读:
    redis 笔记
    经验
    增加模块-概念图
    node API buffer
    VS2010中使用CL快速 生成DLL的方法
    WIN7下VS2010中使用cl编译的步骤
    Win7下VS2010编译的程序在XP报错:找不到msvcp100d.dll或者msvcp100.dll
    C#速学
    Windows下架设SVN服务
    Redis 压力测试
  • 原文地址:https://www.cnblogs.com/fwqblogs/p/11957287.html
Copyright © 2011-2022 走看看