Es.php 14 KB

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