讨论数量:
如果你要是和我一样 没有弄同义词搜索的话,你可以进行如下操作(注意注释):
#app/Console/Commands/Elasticsearch/Indices/ProjectIndex.php
...
public static function getProperties()
{
return [
'type' => ['type' => 'keyword'],
'title' => ['type' => 'text'],
'long_title' => ['type' => 'text'],
// 应该是这样的 但是我没有配置 同义词映射
// 'title' => ['type' => 'text', 'analyzer' => 'ik_smart', 'search_analyzer' => 'ik_smart_synonym'],
// 'long_title' => ['type' => 'text', 'analyzer' => 'ik_smart', 'search_analyzer' => 'ik_smart_synonym'],
'category_id' => ['type' => 'integer'],
'category' => ['type' => 'keyword'],
'category_path' => ['type' => 'keyword'],
'description' => ['type' => 'text', 'analyzer' => 'ik_smart'],
'price' => ['type' => 'scaled_float', 'scaling_factor' => 100],
'on_sale' => ['type' => 'boolean'],
'rating' => ['type' => 'float'],
'sold_count' => ['type' => 'integer'],
'review_count' => ['type' => 'integer'],
'skus' => [
'type' => 'nested',
'properties' => [
'title' => [
'type' => 'text',
// 我没有配置 同义词映射
// 'analyzer' => 'ik_smart',
// 'search_analyzer' => 'ik_smart_synonym',
'copy_to' => 'skus_title',
],
'description' => [
'type' => 'text',
// 我没有配置 同义词映射
// 'analyzer' => 'ik_smart',
'copy_to' => 'skus_description',
],
'price' => ['type' => 'scaled_float', 'scaling_factor' => 100],
],
],
'properties' => [
'type' => 'nested',
'properties' => [
'name' => ['type' => 'keyword'],
'value' => ['type' => 'keyword', 'copy_to' => 'properties_value'],
'search_value' => ['type' => 'keyword'],
],
],
];
}
#app/Console/Commands/Elasticsearch/Migrate.php
#注释掉所有的 $indexClass::getSettings(),
<?php
namespace App\Console\Commands\Elasticsearch;
use Illuminate\Console\Command;
/**
* Elasticsearch 索引结构迁移
* Class Migrate
* @auth: kingofzihua
* @package App\Console\Commands\Elasticsearch
*/
class Migrate extends Command
{
/**
* @auth: kingofzihua
* @var string
*/
protected $signature = 'es:migrate';
/**
* @auth: kingofzihua
* @var string
*/
protected $description = 'Elasticsearch 索引结构迁移';
/**
* @auth: kingofzihua
* @var
*/
protected $es;
/**
* Migrate constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* @auth: kingofzihua
* @throws \Exception
*/
public function handle()
{
$this->es = app('es');
// 索引类数组,先留空
$indices = [Indices\ProjectIndex::class];
// 遍历索引类数组
foreach ($indices as $indexClass) {
// 调用类数组的 getAliasName() 方法来获取索引别名
$aliasName = $indexClass::getAliasName();
$this->info('正在处理索引 ' . $aliasName);
// 通过 exists 方法判断这个别名是否存在
if (!$this->es->indices()->exists(['index' => $aliasName])) {
$this->info('索引不存在,准备创建');
$this->createIndex($aliasName, $indexClass);
$this->info('创建成功,准备初始化数据');
$indexClass::rebuild($aliasName);
$this->info('操作成功');
continue;
}
// 如果索引已经存在,那么尝试更新索引,如果更新失败会抛出异常
try {
$this->info('索引存在,准备更新');
$this->updateIndex($aliasName, $indexClass);
} catch (\Exception $e) {
$this->warn('更新失败,准备重建');
$this->reCreateIndex($aliasName, $indexClass);
}
$this->info($aliasName . ' 操作成功');
}
}
/**
* 创建新索引
* @auth: kingofzihua
* @param $aliasName
* @param $indexClass
*/
protected function createIndex($aliasName, $indexClass)
{
// 调用 create() 方法创建索引
$this->es->indices()->create([
// 第一个版本的索引名后缀为 _0
'index' => $aliasName . '_0',
'body' => [
// 调用索引类的 getSettings() 方法获取索引设置
// 'settings' => $indexClass::getSettings(),
'mappings' => [
'_doc' => [
// 调用索引类的 getProperties() 方法获取索引字段
'properties' => $indexClass::getProperties(),
],
],
'aliases' => [
// 同时创建别名
$aliasName => new \stdClass(),
],
],
]);
}
/**
* 更新已有索引
* @auth: kingofzihua
* @param $aliasName
* @param $indexClass
*/
protected function updateIndex($aliasName, $indexClass)
{
// 暂时关闭索引
$this->es->indices()->close(['index' => $aliasName]);
// 更新索引设置
$this->es->indices()->putSettings([
'index' => $aliasName,
// 'body' => $indexClass::getSettings(),
]);
// 更新索引字段
$this->es->indices()->putMapping([
'index' => $aliasName,
'type' => '_doc',
'body' => [
'_doc' => [
'properties' => $indexClass::getProperties(),
],
],
]);
// 重新打开索引
$this->es->indices()->open(['index' => $aliasName]);
}
/**
* 重建索引
* @auth: kingofzihua
* @param $aliasName
* @param $indexClass
* @throws \Exception
*/
protected function reCreateIndex($aliasName, $indexClass)
{
// 获取索引信息,返回结构的 key 为索引名称,value 为别名
$indexInfo = $this->es->indices()->getAliases(['index' => $aliasName]);
// 取出第一个 key 即为索引名称
$indexName = array_keys($indexInfo)[0];
// 用正则判断索引名称是否以 _数字 结尾
if (!preg_match('~_(\d+)$~', $indexName, $m)) {
$msg = '索引名称不正确:' . $indexName;
$this->error($msg);
throw new \Exception($msg);
}
// 新的索引名称
$newIndexName = $aliasName . '_' . ($m[1] + 1);
$this->info('正在创建索引' . $newIndexName);
$this->es->indices()->create([
'index' => $newIndexName,
'body' => [
// 'settings' => $indexClass::getSettings(),
'mappings' => [
'_doc' => [
'properties' => $indexClass::getProperties(),
],
],
],
]);
$this->info('创建成功,准备重建数据');
$indexClass::rebuild($newIndexName);
$this->info('重建成功,准备修改别名');
$this->es->indices()->putAlias(['index' => $newIndexName, 'name' => $aliasName]);
$this->info('修改成功,准备删除旧索引');
$this->es->indices()->delete(['index' => $indexName]);
$this->info('删除成功');
}
}
修改完后 继续就可以了!
推荐文章: