Es.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\ArticleItem;
  4. use app\common\model\DatumIntro;
  5. use app\common\model\DatumUrl;
  6. use app\common\model\SupplierGoods;
  7. use app\common\model\UserForum;
  8. use app\common\model\VideoUrl;
  9. use app\common\service\Elastic;
  10. /**
  11. * Es
  12. * Class Es
  13. */
  14. class Es extends Base
  15. {
  16. protected $index;
  17. public function initialize(){
  18. $this->index = config('text.es_index');
  19. }
  20. /**
  21. * @title 验证索引
  22. * @desc
  23. * @author qc
  24. * @url /api/Es/checkIndex
  25. * @method GET
  26. */
  27. public function checkIndex()
  28. {
  29. $index = $this->index;
  30. $res = Elastic::checkIndex($index);
  31. $this->success('ok',$res);
  32. }
  33. public function addTest()
  34. {
  35. $index = $this->index;
  36. $type = input('type','video');
  37. $id = input('id');
  38. switch($type) {
  39. case "video":
  40. $res= VideoUrl::esAdd($id,[]);
  41. break;
  42. }
  43. $this->success('ok',$res);
  44. }
  45. /**
  46. * @title 创建索引
  47. * @desc
  48. * @author qc
  49. * @url /api/Es/createIndex
  50. * @method GET
  51. */
  52. public function createIndex()
  53. {
  54. $index = $this->index;
  55. $res = Elastic::createIndex($index);
  56. $this->success('ok',$res);
  57. }
  58. /**
  59. * @title 删除索引
  60. * @desc
  61. * @author qc
  62. * @url /api/Es/delIndex
  63. * @method GET
  64. */
  65. public function delIndex()
  66. {
  67. $index = $this->index;
  68. $res = Elastic::delIndex($index);
  69. $this->success('ok',$res);
  70. }
  71. /**
  72. * @title 获取mapping(结构)
  73. * @desc
  74. * @author qc
  75. * @url /api/Es/getMapping
  76. * @method GET
  77. */
  78. public function getMapping()
  79. {
  80. $index = $this->index;
  81. $res = Elastic::getMapping($index);
  82. $this->success('ok',$res);
  83. }
  84. /**
  85. * @title 添加字段
  86. * @desc
  87. * @author qc
  88. * @url /api/Es/addColumn
  89. * @method GET
  90. */
  91. public function addColumn()
  92. {
  93. $index = $this->index;
  94. $res = Elastic::addColumn($index);
  95. $this->success('ok',$res);
  96. }
  97. /**
  98. * @title 添加视频
  99. * @desc
  100. * @author qc
  101. * @url /api/Es/addVideo
  102. * @method GET
  103. */
  104. public function addVideo()
  105. {
  106. $list = VideoUrl::field('a.*')->alias('a')
  107. ->where('a.id','>',0)
  108. ->where('a.is_deleted','=',0)
  109. // ->where('a.status','=',1)
  110. ->where('b.is_deleted','=',0)
  111. ->where('b.type','=',2)
  112. // ->where('b.status','=',1)
  113. ->leftJoin('VideoIntro b','a.video_id = b.id')
  114. ->select()->toArray();
  115. foreach ($list as $info) {
  116. VideoUrl::esAdd($info['id'],$info);
  117. }
  118. $this->success('ok');
  119. }
  120. /**
  121. * @title 添加图文
  122. * @desc
  123. * @author qc
  124. * @url /api/Es/addArticle
  125. * @method GET
  126. */
  127. public function addArticle()
  128. {
  129. $list = ArticleItem::field('a.*')->alias('a')
  130. ->where('a.id','>',0)
  131. ->where('a.is_deleted','=',0)
  132. // ->where('a.status','=',1)
  133. ->where('b.is_deleted','=',0)
  134. ->where('b.type','=',2)
  135. // ->where('b.status','=',1)
  136. ->leftJoin('ArticleIntro b','a.article_id = b.id')
  137. ->select()->toArray();
  138. foreach ($list as $info) {
  139. ArticleItem::esAdd($info['id'],$info);
  140. }
  141. $this->success('ok');
  142. }
  143. /**
  144. * @title 添加资料
  145. * @desc
  146. * @author qc
  147. * @url /api/Es/addDatum
  148. * @method GET
  149. */
  150. public function addDatum()
  151. {
  152. $list = DatumUrl::field('a.*')->alias('a')
  153. ->where('a.id','>',0)
  154. ->where('a.is_deleted','=',0)
  155. //->where('a.status','=',1)
  156. ->where('b.is_deleted','=',0)
  157. ->where('b.type','=',2)
  158. //->where('b.status','=',1)
  159. ->leftJoin('DatumIntro b','a.datum_id = b.id')
  160. ->select()->toArray();
  161. foreach ($list as $info) {
  162. DatumUrl::esAdd($info['id'],$info);
  163. }
  164. $this->success('ok');
  165. }
  166. /**
  167. * @title 添加资料专栏
  168. * @desc
  169. * @author qc
  170. * @url /api/Es/addDatumIntro
  171. * @method GET
  172. */
  173. public function addDatumIntro()
  174. {
  175. $sel_where = [];
  176. $sel_where[] = ['is_deleted','=',0];
  177. $sel_where[] = ['status','=',1];
  178. $sel_where[] = ['url_num','>',0];
  179. $list = DatumIntro::field('a.*')->alias('a')
  180. ->where($sel_where)
  181. ->select();
  182. // return json($list);
  183. foreach ($list as $info) {
  184. DatumUrl::esiAdd($info['id'],$info);
  185. }
  186. $this->success('ok');
  187. }
  188. /**
  189. * @title 添加问答
  190. * @desc
  191. * @author qc
  192. * @url /api/Es/addForum
  193. * @method GET
  194. */
  195. public function addForum()
  196. {
  197. $list = UserForum::field('a.*')->alias('a')
  198. ->where('a.id','>',0)
  199. ->where('a.is_deleted','=',0)
  200. ->select()->toArray();
  201. foreach ($list as $info) {
  202. UserForum::esAdd($info['id'],$info);
  203. }
  204. $this->success('ok');
  205. }
  206. /**
  207. * @title 添加活动
  208. * @desc
  209. * @author qc
  210. * @url /api/Es/addActivity
  211. * @method GET
  212. */
  213. public function addActivity()
  214. {
  215. $list = \app\common\model\Activity::field('a.*')->alias('a')
  216. ->where('a.id','>',0)
  217. ->where('a.is_deleted','=',0)
  218. ->select()->toArray();
  219. foreach ($list as $info) {
  220. \app\common\model\Activity::esAdd($info['id'],$info);
  221. }
  222. $this->success('ok');
  223. }
  224. /**
  225. * @title 供应商商品
  226. * @desc
  227. * @author qc
  228. * @url /api/Es/addSupplierGoods
  229. * @method GET
  230. */
  231. public function addSupplierGoods()
  232. {
  233. $list = SupplierGoods::field('a.*')->alias('a')
  234. ->where('a.id','>',0)
  235. ->where('a.is_deleted','=',0)
  236. ->select()->toArray();
  237. foreach ($list as $info) {
  238. SupplierGoods::esAdd($info['id'],$info);
  239. }
  240. $this->success('ok');
  241. }
  242. /**
  243. * @title 添加新闻
  244. * @desc
  245. * @author qc
  246. * @url /api/Es/addPress
  247. * @method GET
  248. */
  249. public function addPress()
  250. {
  251. $list = \app\common\model\Press::field('a.*')->alias('a')
  252. ->where('a.id','>',0)
  253. ->where('a.is_deleted','=',0)
  254. ->select()->toArray();
  255. foreach ($list as $info) {
  256. \app\common\model\Press::esAdd($info['id'],$info);
  257. }
  258. $this->success('ok');
  259. }
  260. /**
  261. * @title 添加需求
  262. * @desc
  263. * @author qc
  264. * @url /api/Es/addDemand
  265. * @method GET
  266. */
  267. public function addDemand()
  268. {
  269. $list = \app\common\model\PlatformDemand::field('a.*')->alias('a')
  270. ->where('a.id','>',0)
  271. ->where('a.is_deleted','=',0)
  272. ->select()->toArray();
  273. foreach ($list as $info) {
  274. \app\common\model\PlatformDemand::esAdd($info['id'],$info);
  275. }
  276. $this->success('ok');
  277. }
  278. /**
  279. * @title 添加招聘
  280. * @desc
  281. * @author qc
  282. * @url /api/Es/addRecruit
  283. * @method GET
  284. */
  285. public function addRecruit()
  286. {
  287. $list = \app\common\model\Recruit::field('a.*')->alias('a')
  288. ->where('a.id','>',0)
  289. ->where('a.is_deleted','=',0)
  290. ->select()->toArray();
  291. foreach ($list as $info) {
  292. \app\common\model\Recruit::esAdd($info['id'],$info);
  293. }
  294. $this->success('ok');
  295. }
  296. /**
  297. * @title 添加商城商品
  298. * @desc
  299. * @author qc
  300. * @url /api/Es/addGoods
  301. * @method GET
  302. */
  303. public function addGoods()
  304. {
  305. $list = \app\common\model\StoreGoods::field('a.*')->alias('a')
  306. ->where('a.id','>',0)
  307. ->where('a.is_deleted','=',0)
  308. ->select()->toArray();
  309. foreach ($list as $info) {
  310. \app\common\model\StoreGoods::esAdd($info['id'],$info);
  311. }
  312. $this->success('ok');
  313. }
  314. /**
  315. * @title 获取详情
  316. * @desc
  317. * @author qc
  318. * @url /api/Es/getInfo
  319. * @method GET
  320. */
  321. public function getInfo()
  322. {
  323. $index = $this->index;
  324. $id = input('id','217');
  325. $module = input('module','video');
  326. $info = Elastic::getInfo($index,$module.'_'.$id);
  327. $this->success('ok',$info);
  328. }
  329. /**
  330. * @title 更新
  331. * @desc
  332. * @author qc
  333. * @url /api/Es/update
  334. * @method GET
  335. */
  336. public function update()
  337. {
  338. $index = $this->index;
  339. $id = input('id','217');
  340. $module = input('module','video');
  341. $info = Elastic::update($index,$module.'_'.$id,['title'=>'es'.date("Y-m-d H:i:s")]);
  342. $this->success('ok',$info);
  343. }
  344. /**
  345. * @title 更新Level
  346. * @desc
  347. * @author qc
  348. * @url /api/Es/updateLevel
  349. * @method GET
  350. */
  351. public function updateLevel()
  352. {
  353. $index = $this->index;
  354. $search = input('search','');
  355. $module = input('module','');
  356. $sort_type = input('sort_type',1);
  357. switch ($sort_type) {
  358. case 1:
  359. $order = $module ? [['_score'=>['order'=>'desc']],['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
  360. break;
  361. case 2:
  362. $order = $module ? [['id'=>['order'=>'asc']]] : ['create_at'=>['order'=>'asc']];
  363. break;
  364. case 3:
  365. $order = $module ? [['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
  366. break;
  367. }
  368. $where = [
  369. 'index'=>$index,
  370. 'from' => $this->off_set,
  371. 'size' => 10000,
  372. 'body'=>[
  373. 'sort' => $order,
  374. 'query'=>[
  375. 'bool'=>[
  376. 'must'=> [
  377. ['range' => ['release_time'=> ['lte'=>time()]]],//
  378. ],
  379. ],
  380. ],
  381. 'highlight'=>[
  382. "fields"=>[
  383. 'title'=>[
  384. 'pre_tags'=>["<span style='color:#FF6700'>"],
  385. 'post_tags'=>["</span>"]
  386. ],
  387. 'label'=>[
  388. 'pre_tags'=>["<span style='color:#FF6700'>"],
  389. 'post_tags'=>["</span>"]
  390. ],
  391. 'desc'=>[
  392. 'pre_tags'=>["<span style='color:#FF6700'>"],
  393. 'post_tags'=>["</span>"]
  394. ],
  395. ]
  396. ]
  397. ]
  398. ];
  399. if($module) $where['body']['query']['bool']['must'][] = ['match' =>['module'=> $module]];
  400. if($search) $where['body']['query']['bool']['must'][] = ['bool' =>[
  401. 'should'=> [
  402. 'multi_match' => [
  403. 'fields'=> ['title','label','desc','content','detail'],
  404. 'query'=> $search,
  405. "fuzziness"=>"AUTO",
  406. 'type'=> 'best_fields',
  407. ]]]];
  408. $list = Elastic::select($index,$where);
  409. foreach ($list['hits']['hits'] as &$v){
  410. Elastic::update($this->index,$v['_id'],['level'=>0]);
  411. }
  412. $this->success('ok',['list'=>$list,'page'=>$this->page,'page_num'=>$this->page_num]);
  413. }
  414. /**
  415. * @title 搜索
  416. * @desc
  417. * @author qc
  418. * @url /api/Es/select
  419. * @method GET
  420. * https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html
  421. */
  422. public function select()
  423. {
  424. $index = $this->index;
  425. $search = input('search','');
  426. $module = input('module','');
  427. $sort_type = input('sort_type',1);
  428. switch ($sort_type) {
  429. case 1:
  430. $order = $module ? [['_score'=>['order'=>'desc']],['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
  431. break;
  432. case 2:
  433. $order = $module ? [['id'=>['order'=>'asc']]] : ['create_at'=>['order'=>'asc']];
  434. break;
  435. case 3:
  436. $order = $module ? [['id'=>['order'=>'desc']]] : ['create_at'=>['order'=>'desc']];
  437. break;
  438. }
  439. $where = [
  440. 'index'=>$index,
  441. 'from' => $this->off_set,
  442. 'size' => $this->page_num,
  443. 'body'=>[
  444. 'sort' => $order,
  445. 'query'=>[
  446. 'bool'=>[
  447. 'must'=> [
  448. ['match' => ['module'=> $module]],// module = module
  449. ['match' => ['status'=>1]], // status !=0
  450. ['match' => ['is_deleted'=> 0]],// is_deleted !=1
  451. ['range' => ['release_time'=> ['lte'=>time()]]],//
  452. /*['bool'=>[
  453. 'should'=> [
  454. 'multi_match' => [
  455. 'fields'=> ['title','label','desc','content','detail'],
  456. 'query'=> $search,
  457. "fuzziness"=>"AUTO",
  458. 'type'=> 'best_fields',
  459. ]],
  460. ]]*/
  461. ],
  462. ],
  463. ],
  464. 'highlight'=>[
  465. "fields"=>[
  466. 'title'=>[
  467. 'pre_tags'=>["<span style='color:#FF6700'>"],
  468. 'post_tags'=>["</span>"]
  469. ],
  470. 'label'=>[
  471. 'pre_tags'=>["<span style='color:#FF6700'>"],
  472. 'post_tags'=>["</span>"]
  473. ],
  474. 'desc'=>[
  475. 'pre_tags'=>["<span style='color:#FF6700'>"],
  476. 'post_tags'=>["</span>"]
  477. ],
  478. ]
  479. ]
  480. ]
  481. ];
  482. if($module) $where['body']['query']['bool']['must'][] = ['match' =>['module'=> $module]];
  483. if($search) $where['body']['query']['bool']['must'][] = ['bool' =>[
  484. 'should'=> [
  485. 'multi_match' => [
  486. 'fields'=> ['title','label','desc','content','detail'],
  487. 'query'=> $search,
  488. "fuzziness"=>"AUTO",
  489. 'type'=> 'best_fields',
  490. ]]]];
  491. $res = Elastic::select($index,$where);
  492. $this->success('ok',['list'=>$res,'page'=>$this->page,'page_num'=>$this->page_num]);
  493. }
  494. /**
  495. * @title 删除记录
  496. * @desc
  497. * @author qc
  498. * @url /api/Es/delInfo
  499. * @method GET
  500. * https://www.elastic.co/guide/en/elasticsearch/reference/master/search-search.html
  501. */
  502. public function delInfo()
  503. {
  504. $res = Elastic::delete($this->index,input('id'));
  505. $this->success('ok',$res);
  506. }
  507. }