Yii2+ElasticSearch增删改查实现

构建该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()
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP开光程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值