Yii2操作elasticsearch封装model

这个博客介绍了如何在Yii2框架中封装Elasticsearch的model,包括设置数据库连接、索引、类型、属性映射,并提供了插入、更新、删除和查询数据的方法。
摘要由CSDN通过智能技术生成
<?php
/**
 * 问答ES模型
 * Created by PhpStorm.
 * DateTime: 2019/7/18 11:48
 */

namespace backend\models;

use yii\elasticsearch\ActiveRecord;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use common\libs\CommonFunction;

class QuestionEsModel extends ActiveRecord
{
    /**
     * 数据库
     * @date   2019-07-18
     * @return null|object
     */
    public static function getDb()
    {
        return \Yii::$app->get('elasticsearch');
    }

    /**
     * 索引
     * @date   2019-07-18
     * @return string
     */
    public static function index()
    {
        return 'insurance_question';
    }

    /**
     * 类型
     * @date   2019-07-18
     * @return string
     */
    public static function type()
    {
        return 'question';
    }

    /**
     * 配置属性
     * @date   2019-07-18
     * @return array
     */
    public function attributes()
    {
        $mapConfig = self::mapConfig();
        return array_keys($mapConfig['properties']);
    }

    /**
     * 映射配置
     * @date   2019-07-18
     * @return array
     */
    public static function mapConfig()
    {
        return [
            'properties' => [
                'question_id'    => ['type' => 'integer'],  //问题id
                'hash_id'        => ['type' => 'keyword'], //hash ID
                'title'          => ['type' => 'text', 'analyzer' => "ik_max_word"], //标题
                'description'    => ['type' => 'text', 'analyzer' => "ik_max_word"], //描述
                'category_id'    => ['type' => 'short'], //险种
                'category_name'  => ['type' => 'keyword'], //险种名称
                'category_spell' => ['type' => 'keyword'], //险种拼音
                'view_nums'      => ['type' => 'integer'],  //浏览次数
                'status'         => ['type' => 'short'], //状态
                'reply_count'    => ['type' => 'short'], //回复数
                'created_at'     => ['type' => 'integer'], //问题创建时间
                'updated_at'     => ['type' => 'integer'], //问题更新时间
                'reply'          => [
                    'type' => 'nested',//嵌套文档
                    'properties' => [
                        'reply_id'          => ['type' => 'integer'],  //回复ID
                        'content'           => ['type' => 'text', 'analyzer' => "ik_max_word"], //回复内容
                        'manager_id'        => ['type' => 'integer'], //经理人ID
                        'manager_name'      => ['type' => 'keyword'], //经理人名称
                        'status'            => ['type' => 'short'], //回复状态
                        'quality_grade'     => ['type' => 'short'], //评级
                        'check_fail_reason' => ['type' => 'keyword'], //不通过原因
                        'word_count'        => ['type' => 'integer'], //字数
                        'created_at'        => ['type' => 'integer'], //创建
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值