Yii2 ElasticSearch

Yii2 ElasticSearch

  1. yii2 安装 ElasticSearch composer require --prefer-dist yiisoft/yii2-elasticsearch:"~2.0.0"
    在这里插入图片描述
  2. 在main.php 中配置
'elasticsearch' => [
    'class' => 'yii\elasticsearch\Connection',
    'autodetectCluster' => false,
    'nodes' => [
        ['http_address' => '127.0.0.1:9200'],
    ],
],

在这里插入图片描述

Elasticsearch Database Exception – yii\elasticsearch\Exception Cluster autodetection did not find any active node. Make sure a GET /_nodes reguest on the hosts defined in the config returns the "http_address" field for each node.
在这里插入图片描述
在这里插入图片描述
改为false

创建模型

<?php


namespace common\elasticsearch;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\elasticsearch\ActiveRecord;

class TestSearch extends ActiveRecord
{
    /**
     * @return null|object|\yii\elasticsearch\Connection
     * @throws \yii\base\InvalidConfigException
     */
    public static function getDb()
    {
        return Yii::$app->get('elasticsearch');
    }

    /**
     * 数据库名称
     * @return string
     */
    public static function index()
    {
        return 'test';
    }

    /**
     * 表名
     *
     * @return string
     */
    public static function type()
    {
        return 'curd';
    }

    /**
     * 属性(表字段)
     *
     * @return array|string[]
     */
    public function attributes()
    {
        $mapConfig = self::mapConfig();
        return array_keys($mapConfig['properties']);
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title', 'descr'], 'required'],
            [['sort','t','e','status'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'title' => '标题',
            'sort' => '排序',
            'descr' => '内容',
            'status' => '状态',
            't' => '创建时间',
            'e' => '更新时间',
        ];
    }

    /**
     * mapping配置(表字段说明)
     *
     * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
     * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
     * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
     *
     * @return array
     */
    public static function mapConfig()
    {
        return [
            'properties' => [
                // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
                // index 默认可不设置
                'title' => ['type' => 'text', 'analyzer'=> 'ik_max_word', 'search_analyzer'=> 'ik_smart'],
                'sort' => ['type' => 'text', 'analyzer'=> 'ik_max_word', 'search_analyzer'=> 'ik_smart'],
                'descr' => ['type' => 'text', 'analyzer'=> 'ik_max_word', 'search_analyzer'=> 'ik_smart'],
                'status' => ['type' => 'integer'],
                't' => ['type' => 'long'],
                'e' => ['type' => 'long'],
            ]
        ];
    }

    /**
     * @return array
     */
    public static function mapping()
    {
        return [
            static::type() => self::mapConfig(),
        ];
    }

    /**
     * 更新字段
     *
     * @throws \yii\base\InvalidConfigException
     */
    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());
    }

    /**
     * 删除
     *
     * @throws \yii\base\InvalidConfigException
     */
    public static function deleteMapping()
    {
        $db = self::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index());
    }

    /**
     * 获取字段
     *
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     */
    public static function getMapping()
    {
        $db = self::getDb();
        $command = $db->createCommand();

        return $command->getMapping();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值