Yii2 ElasticSearch的使用
Yii2 elasticSearch – 配置
配置部分如下:
'elasticsearch' => [
'class' => 'yii\elasticsearch\Connection',
'nodes' => [
['http_address' => '192.168.0.199:9200'],
['http_address' => '192.168.0.210:9200'],
],
],
您配置了es的集群,那么需要在http_address中把每一个节点的ip都要配置上,
我只有两个节点,那么,我只写了两个IP。
这样就完成了在Yii2中es的配置。
yii2 elasticSearch - model
<?php
namespace flow\models\elasticsearch;
use \yii\elasticsearch\ActiveRecord;
class IndexElasticSearch extends ActiveRecord
{
public static $indexIndex;
public $_dbName;
public static function getDb()
{
return \Yii::$app->get('elasticsearch');
}
/**
* Description: 定义字段映射的方法
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param $data
*/
public function map($data){
foreach($data as $k=>$v){
if(in_array($k,$this->attributes())){
$this->$k = $v;
}
}
}
//db
public static function index()
{
return 'index';
}
//table
public static function type()
{
return 'index';
}
// 属性
public function attributes()
{
$mapConfig = self::mapConfig();
return array_keys($mapConfig['properties']);
}
// mapping配置
public static function mapConfig(){
return [
'properties' => [
'id' => ['type' => 'integer', "index" => true],
'title' => ['type' => 'text', "index" => true,"analyzer"=>'ik_max_word','search_analyzer'=>'ik_max_word'],//ik中文分词
'type' => ['type' => 'integer', "index" => true],
'inputtime' => ['type' => 'integer', "index" => true],//文章创建时间
'updatetime' => ['type' => 'integer', "index" => true],//文章更新时间
'content' => ['type' => 'text',"index" => true,"analyzer"=>'ik_max_word','search_analyzer'=>'ik_max_word'],//文章内容
'other' => ['type' => 'text',"index" => false],//'"index" => false' 不进行分词
]
];
}
public static function mapping()
{
return [
static::type() => self::mapConfig(),
];
}
/**
* Set (update) mappings for this model
*/
public static function updateMapping(){
$db = self::getDb();
$command = $db->createCommand();
if(!$command->indexExists(self::index())){
$command->createIndex(self::index());
}
$command->setMapping(self::index(), self::type(), self::mapping());
}
public static function getMapping(){
$db = self::getDb();
$command = $db->createCommand();
return $command->getMapping();
}
/**
* Description: 保存数据
* Author: JiaMeng <666@majiameng.com>
* Updater:
* @param $params
* @return self
*/
static public function edit($params){
/** 查询当前id是否被被使用 */
$id = $params['type'].'_'.$params['id'];
$query = [
"match" => [
'_id' => $id
]
];
$elastic = self::find()->query($query)->one();
if(empty($elastic)){
/** 添加数据 */
$elastic = new self();
$elastic->primaryKey = $id;
}
$elastic->map($params);
if(!$elastic->save()){
echo array_values($askimg->firstErrors)[0];
}
return $elastic;
}
}
yii2 elasticSearch - search搜索
$must = [];
//根据keyword搜索关键词
if(!empty($keyword)){
$must[] = [
"multi_match" => [//分词多字段搜索
'query' => $keyword,
'fields' => ['title','comments'],//搜索的字段
],
];
}
//根据type精确搜索
if(!empty($type)){
$must[] = [
"term" => [
'type' => $type
]
]
}
//根据多条id精确搜索(类似于mysql的in)
$ids = [1,2,3,4];
if(!empty($ids)){
$must[] = [
"terms" => [
'id' => $ids
]
]
}
$query = [
'bool'=>[
'must'=>$must
],
];
$this->page = 1;
$this->pageSize = 10;
$searchModel = IndexElasticSearch::find()
->query($query);
$elastic = $searchModel
->orderBy('id desc')
->offset(($this->page-1)*$this->pageSize)
->limit($this->page*$this->pageSize)
->asArray()->all();
return $elastic;
1.清除ElasticSearch所有数据
curl -v -X DELETE //127.0.0.1:9200/_all