/**
  * @param $class \yii\db\ActiveRecord|string
  * @return int
  * @throws \yii\elasticsearch\Exception
  */
 public function upload($class)
 {
     $bulk = '';
     $index = Index::index();
     $type = Index::type();
     $timestamp = time();
     /** @var \yii\base\Behavior[] */
     $behaviors = (new $class())->behaviors;
     $query = $class::find();
     array_walk($behaviors, function ($behavior) use($query) {
         if ($behavior instanceof TaggableBehavior) {
             $query->with('tags');
         }
     });
     foreach ($query->each() as $model) {
         /** @var \yii\db\ActiveRecord|\gromver\platform\basic\interfaces\model\SearchableInterface|\gromver\platform\basic\interfaces\model\ViewableInterface $model */
         $action = Json::encode(["index" => ["_id" => $model->getPrimaryKey(), "_type" => $type, "_index" => $index]]);
         $indexModel = Index::findOne(['model_id' => $model->getPrimaryKey(), 'model_class' => $model->className()]) or $indexModel = new Index();
         $indexModel->model_id = $model->getPrimaryKey();
         $indexModel->model_class = $model->className();
         $indexModel->title = $model->getSearchTitle();
         $indexModel->content = $model->getSearchContent();
         $indexModel->tags = $model->getSearchTags();
         $indexModel->url_backend = $model->getBackendViewLink();
         $indexModel->url_frontend = $model->getFrontendViewLink();
         $indexModel->updated_at = $timestamp;
         ModuleEvent::trigger(Module::EVENT_BEFORE_CREATE_INDEX . $model->className(), new ElasticIndexEvent(['model' => $model, 'index' => $indexModel, 'sender' => $this->module]));
         if ($indexModel->validate()) {
             $bulk .= $action . "\n" . Json::encode($indexModel->toArray()) . "\n";
         }
     }
     $url = [$index, $type, '_bulk'];
     $response = ActiveRecord::getDb()->post($url, [], $bulk);
     $n = 0;
     $errors = [];
     foreach ($response['items'] as $item) {
         if (isset($item['index']['status']) && ($item['index']['status'] == 201 || $item['index']['status'] == 200)) {
             $n++;
         } else {
             $errors[] = $item['index'];
         }
     }
     if (!empty($errors) || isset($response['errors']) && $response['errors']) {
         throw new Exception(__METHOD__ . ' failed inserting ' . $class . ' model records.', $errors);
     }
     return $n;
 }
 public function down()
 {
     if (!($index = Index::index())) {
         throw new Exception(Index::className() . '::index must be set.');
     }
     ActiveRecord::getDb()->createCommand()->deleteIndex($index);
     //->deleteAllIndexes();//
     echo "Index {$index} are deleted successfully.";
 }
 protected function launch()
 {
     $query = Index::find();
     if (empty($this->models)) {
         $this->models = array_keys(self::models());
     }
     // ищем только по тем моделям которые в списке
     $this->filters[] = ['terms' => ['model_class' => $this->models]];
     // ивент на модификацию фильтров ($this->filters) сторонними модулями
     foreach ($this->models as $modelClass) {
         ModuleEvent::trigger(self::EVENT_BEFORE_SEARCH . $modelClass, new ElasticBeforeSearchEvent(['query' => $query, 'sender' => $this]));
     }
     $query->query = ['filtered' => ['filter' => ['and' => ['filters' => $this->filters]]]];
     if (!empty($this->query)) {
         $query->query['filtered']['query']['multi_match'] = ['query' => $this->query, 'fields' => ['title', 'content', 'tags']];
     }
     $query->highlight = $this->highlight;
     echo $this->render($this->layout, ['dataProvider' => new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this->pageSize]]), 'itemLayout' => $this->itemLayout]);
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public function deletePage($event)
 {
     $index = Index::find()->where(['model_id' => $event->model->getPrimaryKey(), 'model_class' => $event->model->className()])->one();
     ModuleEvent::trigger(self::EVENT_BEFORE_DELETE_INDEX . $event->model->className(), new ElasticIndexEvent(['model' => $event->model, 'index' => $index, 'sender' => $this]));
     if ($index) {
         $index->delete();
     }
 }