tp5.0 创建数据的全量索引
作者:互联网
1.项目目录/application/cli/controller/Es.php (也可自定义)
<?php namespace app\cli\controller; use think\Controller; use think\Request; class Es extends Controller { /** * 创建商品索引并导入全部商品文档 * cd public * php index.php /cli/Es/createAllGoodsDocs */ public function createAllGoodsDocs() { try{ //实例化ES工具类 $es = new \tools\es\MyElasticsearch(); //创建索引 if($es->exists_index('goods_index')) $es->delete_index('goods_index'); $es->create_index('goods_index'); $i = 0; while(true){ //查询商品数据 每次处理1000条 这里是重新数据库 可自行操作 $goods = \app\common\model\Goods::with('category')->field('id,goods_name,goods_desc, goods_price,goods_logo,cate_id')->limit($i, 1000)->select(); if(empty($goods)){ //查询结果为空,则停止 break; } //添加文档 foreach($goods as $v){ unset($v['cate_id']); $es->add_doc($v['id'],$v, 'goods_index', 'goods_type'); } $i += 1000; } die('success'); }catch (\Exception $e){ $msg = $e->getMessage(); die($msg); } } }
2.在命令行里面切换到pinlic目录下 执行命令 这里根据自己定义的方法路径来
php index.php /cli/Es/createAllGoodsDocs
注:其中,使用了封装的ES工具类 : 项目目录/extends/tools/es/MyElasticsearch.php
标签:index,tp5.0,goods,索引,全量,php,id,es,1000 来源: https://www.cnblogs.com/gj210623/p/15349741.html