构建该model之前需要自行配置yii2-elasticsearch组件安装
我是通过composer安装的
在composer.json中
安装完成之后
然后需要在yii的配置文件中加入小组件相关配置
接下来就是构建业务模型
集成的ActiveRecord类应该为组件目录下的AR类
use yii\elasticsearch\ActiveRecord
这个AR了重写了yii2的AR类部分方法,有些查询需要原生es语句才可以。
Model:
class HospitalEsModel extends ActiveRecord
{
/**
* 数据库
*/
public static function getDb()
{
return \Yii::$app->get('elasticsearch');
}
/**
* 索引
*/
public static function index()
{
return 'hospital';
}
/**
* 设置索引别名
*/
public static function alias()
{
return 'hospital_new';
}
/**
* 类型
*/
public static function type()
{
return '_doc';
}
/**
* 返回字段
*/
public function attributes()
{
$mapConfig = self::mapConfig();
return array_keys($mapConfig['properties']);
}
/**
* 映射配置
*/
public static function mapConfig()
{
return [
'properties' => [
'id' => ['type' => 'integer'], //医院id
'province_id' => ['type' => 'integer'], //省份ID
'province_name' => ['type' => 'keyword'], //省份名称
'city_id' => ['type' => 'integer'], //城市ID
'city_name' => ['type' => 'keyword'], //城市名称
'district_id' => ['type' => 'integer'], //区县ID
'district_name' => ['type' => 'keyword'],//区县名称
'name' => ['type' => 'text', 'analyzer' => "ik_max_word"], //医院名称
'nick_name' => ['type' => 'text', 'analyzer' => "ik_max_word"], //医院别名
'phone' => ['type' => 'keyword'], //医院电话
'address' => ['type' => 'text', 'analyzer' => "ik_max_word"], //医院地址
'site' => ['type' => 'keyword'], //医院网址
'mail_num' => ['type' => 'keyword'], //邮编
'fax_num' => ['type' => 'keyword'], //传真
'email' => ['type' => 'keyword'], //邮箱
'routes' => ['type' => 'text', 'analyzer' => "ik_max_word"], //路线
'level' => ['type' => 'keyword'], //医院等级文本
'insurance' => ['type' => 'keyword'], //是否保险
'type' => ['type' => 'keyword'], //类型:综合、专科
'kind' => ['type' => 'keyword'], //性质:公立、私立
'tsks' => ['type' => 'text', 'analyzer' => "ik_max_word"], //特色
'photo' => ['type' => 'keyword'], //头图
'photo_list' => ['type' => 'text'], //图片列表
'description' => ['type' => 'text', 'analyzer' => "ik_max_word"], //简介
'city_uri' => ['type' => 'keyword'],//城市URI
'page_uri' => ['type' => 'keyword'], //页面URI
'url' => ['type' => 'keyword'], //医院网址
'is_shequ' => ['type' => 'integer'], //是否是社区服务站
'insurance_num' => ['type' => 'keyword'], //医保编码
'display_order' => ['type' => 'integer'], //显示顺序
'status' => ['type' => 'byte'], //状态, 0正常, 1未审核
'level_num' => ['type' => 'integer'], //医院等级对应数字
'store_id' => ['type' => 'integer'], //药店ID
'score' => ['type' => 'integer'], //综合评分
'source' => ['type' => 'keyword'], //来源
'fudan_score' => ['type' => 'integer'], //复旦大学医院综合评分
]
];
}
/**
* 设置映射
*/
public static function mapping()
{
return self::mapConfig();
}
/**
* 获取映射
*/
public static function getMapping()
{
$db = self::getDb();
$command = $db->createCommand();
return $command->getMapping(static::index());
}
/**
* 更新映射
*/
public static function updateMapping()
{
$db = self::getDb()