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' => 'yii\elasticsearch\Connection',
'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 pro\modules\log\models;
use yii\elasticsearch\ActiveRecord;
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]);