I am trying to use elasticsearch in my project but I get the following error:
Elasticsearch Database Exception – yii\elasticsearch\Exception
Elasticsearch request failed with code 400.
Error Info: Array
(
[requestMethod] => PUT
[requestUrl] => http://172.16.129.85:9200/templates2s/templates2/1?op_type=create
[requestBody] => []
[responseCode] => 400
[responseHeaders] => Array
(
[content-type] => application/json; charset=UTF-8
[content-length] => 166
)
[responseBody] => {"error":"MapperParsingException[failed to parse]; nested: ElasticsearchParseException[Failed to derive xcontent from (offset=0, length=2): [91, 93]]; ","status":400}
)
This is my model extending yii\elasticsearch\ACtiveRecord :
<?php
namespace common\models;
use common\models\CampanieMail;
use common\models\Templates;
class Templates2 extends \yii\elasticsearch\ActiveRecord {
/**
* @return array the list of attributes for this record
*/
public function attributes() {
// path mapping for '_id' is setup to field 'id'
return ['id', 'name', 'slug', 'layout'];
}
/**
* @return ActiveQuery defines a relation to the Order record (can be in other database, e.g. redis or sql)
*/
public function getCampanieMail() {
return $this->hasMany(CampanieMail::className(), ['template_id' => 'id']);
}
/**
* Defines a scope that modifies the `$query` to return only active(status = 1) customers
*/
}
and this is the action I used to create a document
public function actionElastic() {
$tpl = new \common\models\Templates2();
$tpl ->primaryKey = 1; // in this case equivalent to $customer->id = 1;
$tpl ->attributes = ['name' => 'test','slug'=>'test'];
$tpl ->save();
}
And the configuration I used is
'elasticsearch' => [
'class' => 'yii\elasticsearch\Connection',
'nodes' => [
['http_address' => '172.16.129.85:9200'],
],
],
Elasticsearch server is running well on a virtual machine with this IP address 172.16.129.85 .