日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Yii2 Elasticsearch 操作Demo

發(fā)布時(shí)間:2023/12/14 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yii2 Elasticsearch 操作Demo 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

此處用的是yii2 高級(jí)版

1: 配置 : common/config/main.php

'components' => ['elasticsearch' => ['class' => 'yii\elasticsearch\Connection','nodes' => [['http_address' => '192.168.101.5:9200'],// configure more hosts if you have a cluster],'dslVersion' => 7, // default is 5'autodetectCluster' => false],],

2: 類封裝

<?php /*** * @file ConsultingService.php* @author * @version 1.0* @date 2021-07-15*/namespace common\tools\es;use frontend\models\ConsultingService; use frontend\models\QuestionAsk; use yii\elasticsearch\ActiveRecord; use yii\data\Pagination; use yii\helpers\ArrayHelper;class ConsultingService extends ActiveRecord {/*** 數(shù)據(jù)庫*/public static function getDb(){return \Yii::$app->get('elasticsearch');}/*** 索引*/public static function index(){return 'consulting_service';}/*** 設(shè)置索引別名*/public static function alias(){return 'consulting_service_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'department_category_id' => ['type' => 'integer'], //科室分類的id'title' => ['type' => 'text', 'analyzer' => "ik_max_word"], //咨詢的問題標(biāo)題'description' => ['type' => 'text', 'analyzer' => "ik_max_word"], //問題描述]];}/*** 設(shè)置映射*/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();$command = $db->createCommand();if (!$command->indexExists(self::index() . '_' . date('d'))) {return $command->createIndex(self::index() . '_' . date('d'), ['mappings' => static::mapping(),]);} else {return $command->setMapping(self::index() . '_' . date('d'), '', self::mapping());}}/*** 更新別名指向*/public static function updateAlias(){$db = self::getDb();$command = $db->createCommand();$alias = $command->getAliasInfo();$currentIndex = key($alias);$result = $command->addAlias(self::index() . '_' . date('d'), self::alias());if ($currentIndex) {$result = $command->removeAlias($currentIndex, self::alias());}return $result;}/*** 創(chuàng)建索引*/public static function createIndex(){$db = static::getDb();$command = $db->createCommand();$result = $command->createIndex(static::index(), ['mappings' => static::mapping(),]);return $result;}/*** 刪除索引*/public static function deleteIndex(){$db = static::getDb();$command = $db->createCommand();$result = $command->deleteIndex(static::index());return $result;}/*** 獲取醫(yī)院詳情*/public function getDetail($consulting_id, $fields = []){if (!empty($fields)) {return self::find()->select($fields)->where(['id' => $consulting_id])->asArray()->one();}return self::find()->where(['id' => $consulting_id])->asArray()->one();}/*** 根據(jù)問題ID將問題數(shù)據(jù)插入ES中*/public function insertToEs($ask_id){$consultingInfo = ConsultingService::find()->where(['id' => $ask_id])->asArray()->one();if (!empty($consultingInfo)) {try {$consultingEsModel = new self();$consultingEsModel->setPrimaryKey($consultingInfo['id']);$consultingEsModel->setAttribute('id', $ask_id);$consultingEsModel->setAttribute('department_category_id', trim($consultingInfo['department_category_id']));$consultingEsModel->setAttribute('title', $this->filterText($consultingInfo['title']));$consultingEsModel->setAttribute('description', $this->filterText($consultingInfo['description']));$consultingEsModel->save();} catch (\Exception $e) {return ['code' => 0, 'msg' => $e->getMessage()];}} else {return ['code' => 0, 'msg' => '咨詢實(shí)錄問題不存在,插入ES失敗!'];}return ['code' => 1, 'msg' => '咨詢實(shí)錄問題信息插入ES成功!'];}/*** 更新ES中醫(yī)生數(shù)據(jù)*/public function updateToEs($consulting_id){$consultingEsModel = self::get($consulting_id);//如果不存在就插入ESif (is_null($consultingEsModel)) {return $this->insertToEs($consulting_id);} else {$consultingInfo = ConsultingService::find()->where(['id' => $consulting_id])->asArray()->one();try {$consultingEsModel->setAttribute('id', $consulting_id);$consultingEsModel->setAttribute('department_category_id', trim($consultingInfo['department_category_id']));$consultingEsModel->setAttribute('title', $this->filterText($consultingInfo['title']));$consultingEsModel->setAttribute('description', $this->filterText($consultingInfo['description']));$consultingEsModel->save();} catch (\Exception $e) {return ['code' => 0, 'msg' => $e->getMessage()];}return ['code' => 1, 'msg' => '問題更新ES成功!'];}}/*** 從ES中刪除數(shù)據(jù)*/public function deleteFromEs($consulting_id){return self::find()->where(['id' => $consulting_id])->delete();}/*** 初始化時(shí)跑數(shù)據(jù)*/public function initEsData($consulting_id){$consultingInfo = ConsultingService::find()->where(['id' => $consulting_id])->asArray()->one();//組合數(shù)據(jù)$data = [];$data['id'] = $consulting_id;$data['department_category_id'] = intval($consultingInfo['department_category_id']);$data['title'] = $this->filterText($consultingInfo['title']);$data['description'] = $this->filterText($consultingInfo['description']);$db = self::getDb();$command = $db->createCommand();$res = $command->insert(self::index() . '_' . date('d'), '_doc', $data, $consulting_id);return $res;}/*** 過濾文本*/private function filterText($content){//過濾標(biāo)簽空格$contentStr = preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/", "", strip_tags($content));//過濾字符串中的\r\n 以及轉(zhuǎn)義字符$contentStr = stripslashes(str_replace(array("\r\n", "\r", "\n"), "", $contentStr));return strval(trim($contentStr));} }

總結(jié)

以上是生活随笔為你收集整理的Yii2 Elasticsearch 操作Demo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。