尝试访问Cakephp 3和ElasticSearch中的添加用户选项时,如何解决“无此类索引[index:users]”错误?

时间:2019-06-29 10:08:18

标签: elasticsearch cakephp-3.7

我使用的是Cakephp提供的弹性搜索库,它遵循CakePhp上的Click here本教程,但是在尝试访问UsersController的添加方法时,我得到了“没有这样的索引[index:用户]“

感觉好像用户索引不是由cakephp创建的,因此当我为另一个模型Articles手动创建索引时,该错误消失了,但是我又遇到了另一个错误“ type [[article]]缺少[索引:_all]”

在application.php $this->addPlugin('Cake/ElasticSearch', ['bootstrap' => true]);

在app.php中

   'elastic' => [
        'className' => 'Cake\ElasticSearch\Datasource\Connection',
        'driver' => 'Cake\ElasticSearch\Datasource\Connection',
    'host' => '127.0.0.1',
    'port' => 9200,
    ]

创建模型类型 UsersType.php

   namespace App\Model\Type;
   use Cake\ElasticSearch\Type;
   class UsersType extends Type{}

在UsersController.php

 public function beforeFilter(Event $event){
  parent::beforeFilter($event);
 // Load the Type using the 'Elastic' provider.
 $this->loadModel('Users', 'Elastic');
 }
UsersController.php中的

add()方法

    public function add()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user,   $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
        $this->set(compact('user'));
    }

0 个答案:

没有答案