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

  • 相关阅读:
    二分法检索数组
    Linux安装CDH
    myeclipse操作hdfs
    Linux安装hbase
    Linux安装zookeeper
    Linux安装msql
    fluem全分布环境搭建
    bash: jps: 未找到命令...
    自动化项目Jenkins持续集成
    linux卸载mysql====安装新的虚拟机 自带的基本都要卸载!? mysql tomcat java Python可以不用卸载
  • 原文地址:https://www.cnblogs.com/fwqblogs/p/11957287.html
Copyright © 2011-2022 走看看