您现在的位置是:首页 > 博客日记 > Php Php

Yii2 ElasticSearch的使用

2018-08-17 17:01:00 【Php】 人已围观

Yii2 ElasticSearch的使用

Yii2 elasticSearch – 配置

配置部分如下:

  1. 'elasticsearch' => [
  2. 'class' => 'yii\elasticsearch\Connection',
  3. 'nodes' => [
  4. ['http_address' => '192.168.0.199:9200'],
  5. ['http_address' => '192.168.0.210:9200'],
  6. ],
  7. ],

您配置了es的集群,那么需要在http_address中把每一个节点的ip都要配置上,

我只有两个节点,那么,我只写了两个IP。

这样就完成了在Yii2中es的配置。

yii2 elasticSearch - model

  1. <?php
  2. namespace flow\models\elasticsearch;
  3. use \yii\elasticsearch\ActiveRecord;
  4. class IndexElasticSearch extends ActiveRecord
  5. {
  6. public static $indexIndex;
  7. public $_dbName;
  8. public static function getDb()
  9. {
  10. return \Yii::$app->get('elasticsearch');
  11. }
  12. /**
  13. * Description: 定义字段映射的方法
  14. * Author: JiaMeng <666@majiameng.com>
  15. * Updater:
  16. * @param $data
  17. */
  18. public function map($data){
  19. foreach($data as $k=>$v){
  20. if(in_array($k,$this->attributes())){
  21. $this->$k = $v;
  22. }
  23. }
  24. }
  25. //db
  26. public static function index()
  27. {
  28. return 'index';
  29. }
  30. //table
  31. public static function type()
  32. {
  33. return 'index';
  34. }
  35. // 属性
  36. public function attributes()
  37. {
  38. $mapConfig = self::mapConfig();
  39. return array_keys($mapConfig['properties']);
  40. }
  41. // mapping配置
  42. public static function mapConfig(){
  43. return [
  44. 'properties' => [
  45. 'id' => ['type' => 'integer', "index" => true],
  46. 'title' => ['type' => 'text', "index" => true,"analyzer"=>'ik_max_word','search_analyzer'=>'ik_max_word'],//ik中文分词
  47. 'type' => ['type' => 'integer', "index" => true],
  48. 'inputtime' => ['type' => 'integer', "index" => true],//文章创建时间
  49. 'updatetime' => ['type' => 'integer', "index" => true],//文章更新时间
  50. 'content' => ['type' => 'text',"index" => true,"analyzer"=>'ik_max_word','search_analyzer'=>'ik_max_word'],//文章内容
  51. 'other' => ['type' => 'text',"index" => false],//'"index" => false' 不进行分词
  52. ]
  53. ];
  54. }
  55. public static function mapping()
  56. {
  57. return [
  58. static::type() => self::mapConfig(),
  59. ];
  60. }
  61. /**
  62. * Set (update) mappings for this model
  63. */
  64. public static function updateMapping(){
  65. $db = self::getDb();
  66. $command = $db->createCommand();
  67. if(!$command->indexExists(self::index())){
  68. $command->createIndex(self::index());
  69. }
  70. $command->setMapping(self::index(), self::type(), self::mapping());
  71. }
  72. public static function getMapping(){
  73. $db = self::getDb();
  74. $command = $db->createCommand();
  75. return $command->getMapping();
  76. }
  77. /**
  78. * Description: 保存数据
  79. * Author: JiaMeng <666@majiameng.com>
  80. * Updater:
  81. * @param $params
  82. * @return self
  83. */
  84. static public function edit($params){
  85. /** 查询当前id是否被被使用 */
  86. $id = $params['type'].'_'.$params['id'];
  87. $query = [
  88. "match" => [
  89. '_id' => $id
  90. ]
  91. ];
  92. $elastic = self::find()->query($query)->one();
  93. if(empty($elastic)){
  94. /** 添加数据 */
  95. $elastic = new self();
  96. $elastic->primaryKey = $id;
  97. }
  98. $elastic->map($params);
  99. if(!$elastic->save()){
  100. echo array_values($askimg->firstErrors)[0];
  101. }
  102. return $elastic;
  103. }
  104. }

yii2 elasticSearch - search搜索

  1. $must = [];
  2. //根据keyword搜索关键词
  3. if(!empty($keyword)){
  4. $must[] = [
  5. "multi_match" => [//分词多字段搜索
  6. 'query' => $keyword,
  7. 'fields' => ['title','comments'],//搜索的字段
  8. ],
  9. ];
  10. }
  11. //根据type精确搜索
  12. if(!empty($type)){
  13. $must[] = [
  14. "term" => [
  15. 'type' => $type
  16. ]
  17. ]
  18. }
  19. //根据多条id精确搜索(类似于mysql的in)
  20. $ids = [1,2,3,4];
  21. if(!empty($ids)){
  22. $must[] = [
  23. "terms" => [
  24. 'id' => $ids
  25. ]
  26. ]
  27. }
  28. $query = [
  29. 'bool'=>[
  30. 'must'=>$must
  31. ],
  32. ];
  33. $this->page = 1;
  34. $this->pageSize = 10;
  35. $searchModel = IndexElasticSearch::find()
  36. ->query($query);
  37. $elastic = $searchModel
  38. ->orderBy('id desc')
  39. ->offset(($this->page-1)*$this->pageSize)
  40. ->limit($this->page*$this->pageSize)
  41. ->asArray()->all();
  42. return $elastic;
1.清除ElasticSearch所有数据
  1. curl -v -X DELETE http://127.0.0.1:9200/_all


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()