123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- <?php
- namespace app\api\controller;
- use app\common\model\ArticleItem;
- use app\common\model\DatumIntro;
- use app\common\model\DatumUrl;
- use app\common\model\SupplierGoods;
- use app\common\model\UserForum;
- use app\common\model\VideoUrl;
- use app\common\service\Elastic;
- /**
- * Es
- * Class Es
- */
- class Es extends Base
- {
- protected $index;
- public function initialize(){
- $this->index = config('text.es_index');
- }
- /**
- * @title 验证索引
- * @desc
- * @author qc
- * @url /api/Es/checkIndex
- * @method GET
- */
- public function checkIndex()
- {
- $index = $this->index;
- $res = Elastic::checkIndex($index);
- $this->success('ok',$res);
- }
- public function addTest()
- {
- $index = $this->index;
- $type = input('type','video');
- $id = input('id');
- switch($type) {
- case "video":
- $res= VideoUrl::esAdd($id,[]);
- break;
- }
- $this->success('ok',$res);
- }
- /**
- * @title 创建索引
- * @desc
- * @author qc
- * @url /api/Es/createIndex
- * @method GET
- */
- public function createIndex()
- {
- $index = $this->index;
- $res = Elastic::createIndex($index);
- $this->success('ok',$res);
- }
- /**
- * @title 删除索引
- * @desc
- * @author qc
- * @url /api/Es/delIndex
- * @method GET
- */
- public function delIndex()
- {
- $index = $this->index;
- $res = Elastic::delIndex($index);
- $this->success('ok',$res);
- }
- /**
- * @title 获取mapping(结构)
- * @desc
- * @author qc
- * @url /api/Es/getMapping
- * @method GET
- */
- public function getMapping()
- {
- $index = $this->index;
- $res = Elastic::getMapping($index);
- $this->success('ok',$res);
- }
- /**
- * @title 添加字段
- * @desc
- * @author qc
- * @url /api/Es/addColumn
- * @method GET
- */
- public function addColumn()
- {
- $index = $this->index;
- $res = Elastic::addColumn($index);
- $this->success('ok',$res);
- }
- /**
- * @title 添加视频
- * @desc
- * @author qc
- * @url /api/Es/addVideo
- * @method GET
- */
- public function addVideo()
- {
- $list = VideoUrl::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- // ->where('a.status','=',1)
- ->where('b.is_deleted','=',0)
- ->where('b.type','=',2)
- // ->where('b.status','=',1)
- ->leftJoin('VideoIntro b','a.video_id = b.id')
- ->select()->toArray();
- foreach ($list as $info) {
- VideoUrl::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加图文
- * @desc
- * @author qc
- * @url /api/Es/addArticle
- * @method GET
- */
- public function addArticle()
- {
- $list = ArticleItem::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- // ->where('a.status','=',1)
- ->where('b.is_deleted','=',0)
- ->where('b.type','=',2)
- // ->where('b.status','=',1)
- ->leftJoin('ArticleIntro b','a.article_id = b.id')
- ->select()->toArray();
- foreach ($list as $info) {
- ArticleItem::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加资料
- * @desc
- * @author qc
- * @url /api/Es/addDatum
- * @method GET
- */
- public function addDatum()
- {
- $list = DatumUrl::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- //->where('a.status','=',1)
- ->where('b.is_deleted','=',0)
- ->where('b.type','=',2)
- //->where('b.status','=',1)
- ->leftJoin('DatumIntro b','a.datum_id = b.id')
- ->select()->toArray();
- foreach ($list as $info) {
- DatumUrl::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加资料专栏
- * @desc
- * @author qc
- * @url /api/Es/addDatumIntro
- * @method GET
- */
- public function addDatumIntro()
- {
- $sel_where = [];
- $sel_where[] = ['is_deleted','=',0];
- $sel_where[] = ['status','=',1];
- $sel_where[] = ['url_num','>',0];
- $list = DatumIntro::field('a.*')->alias('a')
- ->where($sel_where)
- ->select();
- // return json($list);
- foreach ($list as $info) {
- DatumUrl::esiAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加问答
- * @desc
- * @author qc
- * @url /api/Es/addForum
- * @method GET
- */
- public function addForum()
- {
- $list = UserForum::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- UserForum::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加活动
- * @desc
- * @author qc
- * @url /api/Es/addActivity
- * @method GET
- */
- public function addActivity()
- {
- $list = \app\common\model\Activity::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- \app\common\model\Activity::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 供应商商品
- * @desc
- * @author qc
- * @url /api/Es/addSupplierGoods
- * @method GET
- */
- public function addSupplierGoods()
- {
- $list = SupplierGoods::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- SupplierGoods::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加新闻
- * @desc
- * @author qc
- * @url /api/Es/addPress
- * @method GET
- */
- public function addPress()
- {
- $list = \app\common\model\Press::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- \app\common\model\Press::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加需求
- * @desc
- * @author qc
- * @url /api/Es/addDemand
- * @method GET
- */
- public function addDemand()
- {
- $list = \app\common\model\PlatformDemand::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- \app\common\model\PlatformDemand::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加招聘
- * @desc
- * @author qc
- * @url /api/Es/addRecruit
- * @method GET
- */
- public function addRecruit()
- {
- $list = \app\common\model\Recruit::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- \app\common\model\Recruit::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 添加商城商品
- * @desc
- * @author qc
- * @url /api/Es/addGoods
- * @method GET
- */
- public function addGoods()
- {
- $list = \app\common\model\StoreGoods::field('a.*')->alias('a')
- ->where('a.id','>',0)
- ->where('a.is_deleted','=',0)
- ->select()->toArray();
- foreach ($list as $info) {
- \app\common\model\StoreGoods::esAdd($info['id'],$info);
- }
- $this->success('ok');
- }
- /**
- * @title 获取详情
- * @desc
- * @author qc
- * @url /api/Es/getInfo
- * @method GET
- */
- public function getInfo()
- {
- $index = $this->index;
- $id = input('id','217');
- $module = input('module','video');
- $info = Elastic::getInfo($index,$module.'_'.$id);
- $this->success('ok',$info);
- }
- /**
- * @title 更新
- * @desc
- * @author qc
- * @url /api/Es/update
- * @method GET
- */
- public function update()
- {
- $index = $this->index;
- $id = input('id','217');
- $module = input('module','video');
- $info = Elastic::update($index,$module.'_'.$id,['title'=>'es'.date("Y-m-d H:i:s")]);
- $this->success('ok',$info);
- }
- /**
- * @title 更新Level
- * @desc
- * @author qc
- * @url /api/Es/updateLevel
- * @method GET
- */
- public function updateLevel()
- {
- $index = $this->index;
- $search = input('search','');
- $module = input('module','');
- $sort_type = input('sort_type',1);
- switch ($sort_type) {
- case 1:
- $order = $module ? [['_score'=>['order'=>'desc']],['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
- break;
- case 2:
- $order = $module ? [['id'=>['order'=>'asc']]] : ['create_at'=>['order'=>'asc']];
- break;
- case 3:
- $order = $module ? [['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
- break;
- }
- $where = [
- 'index'=>$index,
- 'from' => $this->off_set,
- 'size' => 10000,
- 'body'=>[
- 'sort' => $order,
- 'query'=>[
- 'bool'=>[
- 'must'=> [
- ['range' => ['release_time'=> ['lte'=>time()]]],//
- ],
- ],
- ],
- 'highlight'=>[
- "fields"=>[
- 'title'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- 'label'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- 'desc'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- ]
- ]
- ]
- ];
- if($module) $where['body']['query']['bool']['must'][] = ['match' =>['module'=> $module]];
- if($search) $where['body']['query']['bool']['must'][] = ['bool' =>[
- 'should'=> [
- 'multi_match' => [
- 'fields'=> ['title','label','desc','content','detail'],
- 'query'=> $search,
- "fuzziness"=>"AUTO",
- 'type'=> 'best_fields',
- ]]]];
- $list = Elastic::select($index,$where);
- foreach ($list['hits']['hits'] as &$v){
- Elastic::update($this->index,$v['_id'],['level'=>0]);
- }
- $this->success('ok',['list'=>$list,'page'=>$this->page,'page_num'=>$this->page_num]);
- }
- /**
- * @title 搜索
- * @desc
- * @author qc
- * @url /api/Es/select
- * @method GET
- * https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html
- */
- public function select()
- {
- $index = $this->index;
- $search = input('search','');
- $module = input('module','');
- $sort_type = input('sort_type',1);
- switch ($sort_type) {
- case 1:
- $order = $module ? [['_score'=>['order'=>'desc']],['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
- break;
- case 2:
- $order = $module ? [['id'=>['order'=>'asc']]] : ['create_at'=>['order'=>'asc']];
- break;
- case 3:
- $order = $module ? [['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
- break;
- }
- $where = [
- 'index'=>$index,
- 'from' => $this->off_set,
- 'size' => $this->page_num,
- 'body'=>[
- 'sort' => $order,
- 'query'=>[
- 'bool'=>[
- 'must'=> [
- ['match' => ['module'=> $module]],// module = module
- ['match' => ['status'=>1]], // status !=0
- ['match' => ['is_deleted'=> 0]],// is_deleted !=1
- ['range' => ['release_time'=> ['lte'=>time()]]],//
- /*['bool'=>[
- 'should'=> [
- 'multi_match' => [
- 'fields'=> ['title','label','desc','content','detail'],
- 'query'=> $search,
- "fuzziness"=>"AUTO",
- 'type'=> 'best_fields',
- ]],
- ]]*/
- ],
- ],
- ],
- 'highlight'=>[
- "fields"=>[
- 'title'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- 'label'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- 'desc'=>[
- 'pre_tags'=>["<span style='color:#FF6700'>"],
- 'post_tags'=>["</span>"]
- ],
- ]
- ]
- ]
- ];
- if($module) $where['body']['query']['bool']['must'][] = ['match' =>['module'=> $module]];
- if($search) $where['body']['query']['bool']['must'][] = ['bool' =>[
- 'should'=> [
- 'multi_match' => [
- 'fields'=> ['title','label','desc','content','detail'],
- 'query'=> $search,
- "fuzziness"=>"AUTO",
- 'type'=> 'best_fields',
- ]]]];
- $res = Elastic::select($index,$where);
- $this->success('ok',['list'=>$res,'page'=>$this->page,'page_num'=>$this->page_num]);
- }
- /**
- * @title 删除记录
- * @desc
- * @author qc
- * @url /api/Es/delInfo
- * @method GET
- * https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html
- */
- public function delInfo()
- {
- $res = Elastic::delete($this->index,input('id'));
- $this->success('ok',$res);
- }
- }
|