ElasticSearchCurd.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace common\models\elasticsearch;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\elasticsearch\ActiveRecord;
  6. /**
  7. * Class ElasticSearchCurd
  8. * @package addons\RfExample\common\models
  9. */
  10. class ElasticSearchCurd extends ActiveRecord
  11. {
  12. public static $currentIndex;
  13. /**
  14. * 数据库名称
  15. * @return string
  16. */
  17. public static function index()
  18. {
  19. return 'rageframe';
  20. }
  21. /**
  22. * 表名
  23. *
  24. * @return string
  25. */
  26. public static function type()
  27. {
  28. return 'curd';
  29. }
  30. /**
  31. * 属性(表字段)
  32. *
  33. * @return array|string[]
  34. */
  35. public function attributes()
  36. {
  37. $mapConfig = self::mapConfig();
  38. return array_keys($mapConfig['properties']);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['title'], 'required'],
  47. [['cover'],'string'],
  48. [['sort', 'status', 'created_at', 'updated_at'], 'integer'],
  49. [['content'], 'required'],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'title' => '标题',
  59. 'sort' => '排序',
  60. 'content' => '内容',
  61. 'status' => '状态',
  62. 'cover' => '封面',
  63. 'created_at' => '创建时间',
  64. 'updated_at' => '更新时间',
  65. ];
  66. }
  67. /**
  68. * mapping配置(表字段说明)
  69. *
  70. * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
  71. * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
  72. * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
  73. *
  74. * @return array
  75. */
  76. public static function mapConfig()
  77. {
  78. return [
  79. 'properties' => [
  80. // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
  81. // index 默认可不设置
  82. 'title' => ['type' => 'text'],
  83. 'content' => ['type' => 'text'],
  84. 'cover' => ['type' => 'keyword'],
  85. 'sort' => ['type' => 'integer'],
  86. 'status' => ['type' => 'integer'],
  87. 'created_at' => ['type' => 'long'],
  88. 'updated_at' => ['type' => 'long'],
  89. ],
  90. ];
  91. }
  92. /**
  93. * @return array
  94. */
  95. public static function mapping()
  96. {
  97. return [
  98. static::type() => self::mapConfig(),
  99. ];
  100. }
  101. /**
  102. * 更新字段
  103. *
  104. * @throws \yii\base\InvalidConfigException
  105. */
  106. public static function updateMapping()
  107. {
  108. $db = self::getDb();
  109. $command = $db->createCommand();
  110. if (!$command->indexExists(self::index())) {
  111. $command->createIndex(self::index());
  112. }
  113. $command->setMapping(self::index(), self::type(), self::mapping(),['include_type_name'=>'true']);
  114. }
  115. /**
  116. * 删除
  117. *
  118. * @throws \yii\base\InvalidConfigException
  119. */
  120. public static function deleteMapping()
  121. {
  122. $db = self::getDb();
  123. $command = $db->createCommand();
  124. $command->deleteIndex(static::index());
  125. }
  126. /**
  127. * 获取字段
  128. *
  129. * @return mixed
  130. * @throws \yii\base\InvalidConfigException
  131. */
  132. public static function getMapping()
  133. {
  134. $db = self::getDb();
  135. $command = $db->createCommand();
  136. return $command->getMapping();
  137. }
  138. /**
  139. * @return array
  140. */
  141. public function behaviors()
  142. {
  143. return [
  144. [
  145. 'class' => TimestampBehavior::class,
  146. 'attributes' => [
  147. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  148. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  149. ],
  150. ],
  151. ];
  152. }
  153. }