Yii2中ElasticSearch的使用示例
- 3375
- PHP
- 0
- super_dodo
- 2021/04/29
配置部分如下:
1 2 3 4 5 6 7 | 'elasticsearch' => [ 'class' => 'yii\elasticsearch\Connection' , 'nodes' => [ [ 'http_address' => '192.168.0.199:9200' ], [ 'http_address' => '192.168.0.210:9200' ], ], ], |
您配置了es的集群,那么需要在http_address中把每一个节点的ip都要配置上,
我只有两个节点,那么,我只写了两个IP。
这样就完成了在Yii2中es的配置。
yii2 elasticSearch - model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | <?php namespace flow\models\elasticsearch; use \yii\elasticsearch\ActiveRecord; class IndexElasticSearch extends ActiveRecord { public static $indexIndex ; public $_dbName ; public static function getDb() { return \Yii:: $app ->get( 'elasticsearch' ); } /** * Description: 定义字段映射的方法 * Author: JiaMeng <666@majiameng.com> * Updater: * @param $data */ public function map( $data ){ foreach ( $data as $k => $v ){ if (in_array( $k , $this ->attributes())){ $this -> $k = $v ; } } } //db public static function index() { return 'index' ; } //table public static function type() { return 'index' ; } // 属性 public function attributes() { $mapConfig = self::mapConfig(); return array_keys ( $mapConfig [ 'properties' ]); } // mapping配置 public static function mapConfig(){ return [ 'properties' => [ 'id' => [ 'type' => 'integer' , "index" => true], 'title' => [ 'type' => 'text' , "index" => true, "analyzer" => 'ik_max_word' , 'search_analyzer' => 'ik_max_word' ], //ik中文分词 'type' => [ 'type' => 'integer' , "index" => true], 'inputtime' => [ 'type' => 'integer' , "index" => true], //文章创建时间 'updatetime' => [ 'type' => 'integer' , "index" => true], //文章更新时间 'content' => [ 'type' => 'text' , "index" => true, "analyzer" => 'ik_max_word' , 'search_analyzer' => 'ik_max_word' ], //文章内容 'other' => [ 'type' => 'text' , "index" => false], //'"index" => false' 不进行分词 ] ]; } public static function mapping() { return [ static ::type() => self::mapConfig(), ]; } /** * Set (update) mappings for this model */ 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()); } public static function getMapping(){ $db = self::getDb(); $command = $db ->createCommand(); return $command ->getMapping(); } /** * Description: 保存数据 * Author: JiaMeng <666@majiameng.com> * Updater: * @param $params * @return self */ static public function edit( $params ){ /** 查询当前id是否被被使用 */ $id = $params [ 'type' ]. '_' . $params [ 'id' ]; $query = [ "match" => [ '_id' => $id ] ]; $elastic = self::find()->query( $query )->one(); if ( empty ( $elastic )){ /** 添加数据 */ $elastic = new self(); $elastic ->primaryKey = $id ; } $elastic ->map( $params ); if (! $elastic ->save()){ echo array_values ( $askimg ->firstErrors)[0]; } return $elastic ; } } |
yii2 elasticSearch - search搜索
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | $must = []; //根据keyword搜索关键词 if (! empty ( $keyword )){ $must [] = [ "multi_match" => [ //分词多字段搜索 'query' => $keyword , 'fields' => [ 'title' , 'comments' ], //搜索的字段 ], ]; } //根据type精确搜索 if (! empty ( $type )){ $must [] = [ "term" => [ 'type' => $type ] ] } //根据多条id精确搜索(类似于mysql的in) $ids = [1,2,3,4]; if (! empty ( $ids )){ $must [] = [ "terms" => [ 'id' => $ids ] ] } $query = [ 'bool' =>[ 'must' => $must ], ]; $this ->page = 1; $this ->pageSize = 10; $searchModel = IndexElasticSearch::find() ->query( $query ); $elastic = $searchModel ->orderBy( 'id desc' ) ->offset(( $this ->page-1)* $this ->pageSize) ->limit( $this ->page* $this ->pageSize) ->asArray()->all(); return $elastic ; |
1.清除ElasticSearch所有数据
1 | curl -v -X DELETE //127.0.0.1:9200/_all |
相关阅读
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
热门文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
最新文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例