我使用的是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'));
}