跟着慕课网学习《Laravel快速开发简书》,搜索一章的时候,创建模板出现的错误总结:
模板文件:
<?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class ESInit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init';
/**
* The console command description.
*
* @var string
*/
protected $description = '初始化 ES ';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//TODO:创建template
$client = new Client();
$url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
$client->delete($url);
$param = [
'json' => [
'template' => config('scout.elasticsearch.index'),
'settings' => [
'number_of_shards' => 1
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => true
],
'dynamic_templates' => [
[
'strings' => [
'match_mapping_type' => 'string',
'mapping' => [
'type' => 'text',
'analyzer' => 'ik_smart',//文本分析
'ignore_above' => 256,
'fields' => [
'keyword' => [
'type' => 'keyword'
]
]
]
]
]
]
]
]
]
];
$client->put($url, $param);
$this->info("=====创建模板成功=====");
//TODO:创建index
$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
$client->put($url, [
'json' => [
'settings' => [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
]);
$this->info("=====创建索引成功=====");
}
}
执行命令:
php artisan es:init
报错:
Client error: `PUT http://127.0.0.1:9200/_template/tmp` resulted in a `400 Bad Request` response:
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters: (truncated...)
原因:
jdk1.9 版本问题,jdk1.8则没有这个问题出现。
解决办法:
<?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class ESInit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init';
/**
* The console command description.
*
* @var string
*/
protected $description = 'init laravel es for post';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 创建template
$client = new Client();
$url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
try {
$client->delete($url);
} catch (\Exception $e) {
$this->info("===delete模版出现错误==" . $e->getMessage());
}
$param = [
'json' => [
'template' => config('scout.elasticsearch.index'),
'mappings' => [
'posts' => [
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
],
'content' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
]
]
]
],
],
];
try {
$client->put($url, $param);
} catch (\Exception $e) {
$this->info("===put模版出现错误==" . $e->getMessage());
}
$this->info("========= 创建模版成功 ========");
// 创建index
$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
try {
$client->delete($url);
} catch (\Exception $e) {
$this->info("===delete索引出现错误==" . $e->getMessage());
}
$param = [
'json' => [
'settings' => [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'posts' => [
'_all' => [
'enabled' => false
]
]
]
]
];
try {
$client->put($url, $param);
} catch (\Exception $e) {
$this->info("===put索引出现错误==" . $e->getMessage());
}
$this->info("========= 创建索引成功 ========");
}
}