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

  • 相关阅读:
    模拟登陆+数据爬取 (python+selenuim)
    matplotlib基本使用(矩形图、饼图、热力图、3D图)
    tensorflow进阶篇-4(损失函数1)
    CS231n学习笔记-图像分类笔记(下篇)
    CS231n学习笔记-图像分类笔记(上篇)
    numpy 基本使用1
    tensorflow基础篇-2
    tensorflow进阶篇-3
    tensorflow基础篇-1
    自定义滚动条第一版
  • 原文地址:https://www.cnblogs.com/fwqblogs/p/11957287.html
Copyright © 2011-2022 走看看