ping([ 'client' => [ 'timeout' => 5, 'connect_timeout' => 5 ] ]); } catch (\Exception $exception) { $this->info($exception->getMessage()); $this->info('无法连接到 elasticse arch 服务器,请配置 config/elasticsearch.php 文件'); $this->info('默认使用 MySQL 的模糊搜索'); $this->info('配置完毕后可运行: php artisan add:shop-to-search 添加索引'); return; } // 新建商品索引 if (Product::indexExists()) { Product::deleteIndex(); $this->info('删除索引'); } Product::createIndex(); $this->info('新建索引成功'); // 开始导入数据 $query = Product::query(); $count = $query->count(); $handle = 0; $query->with('category')->chunk(1000, function (Collection $models) use ($count, &$handle) { $models->map(function (Product $product) use ($count, &$handle) { $product->addToIndex($product->getSearchData()); ++ $handle; echo "\r {$handle}/$count"; }); }); echo PHP_EOL; $this->info('索引生成完毕'); } }