SearchController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\BookTitle;
  5. use App\Models\FtsText;
  6. use App\Models\Tag;
  7. use App\Models\TagMap;
  8. use App\Models\PaliText;
  9. use Illuminate\Support\Facades\Http;
  10. use Illuminate\Support\Facades\DB;
  11. use App\Http\Resources\SearchResource;
  12. use App\Http\Resources\SearchTitleResource;
  13. use App\Http\Resources\SearchBookResource;
  14. use Illuminate\Support\Facades\Log;
  15. use App\Tools\Tools;
  16. use App\Models\WbwTemplate;
  17. use App\Models\PageNumber;
  18. use App\Tools\PaliSearch;
  19. use Illuminate\Support\Facades\App;
  20. class SearchController extends Controller
  21. {
  22. /**
  23. * Display a listing of the resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index(Request $request){
  28. switch ($request->get('view','pali')) {
  29. case 'pali':
  30. $pageHead = ['M','P','T','V','O'];
  31. $key = $request->get('key');
  32. if(substr($key,0,4) === 'para' || in_array(substr($key,0,1),$pageHead)){
  33. return $this->page($request);
  34. }else{
  35. if (App::environment(['local', 'staging'])) {
  36. return $this->pali_rpc($request);
  37. }else{
  38. return $this->pali($request);
  39. }
  40. }
  41. break;
  42. case 'page':
  43. return $this->page($request);
  44. break;
  45. case 'title':
  46. $key = strtolower($request->get('key'));
  47. $table = PaliText::where('level','<',8)
  48. ->where(function ($query) use($key){
  49. $query->where('title_en','like',"%{$key}%")
  50. ->orWhere('title','like',"%{$key}%");
  51. });
  52. Log::info($table->toSql());
  53. if($request->has('tags')){
  54. //查询搜索范围
  55. $tagItems = explode(';',$request->get('tags'));
  56. $bookId = [];
  57. foreach ($tagItems as $tagItem) {
  58. # code...
  59. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  60. }
  61. $table = $table->whereIn('pcd_book_id',$bookId);
  62. }
  63. $count = $table->count();
  64. $table = $table->orderBy($request->get('orderby','book'),$request->get('dir','asc'));
  65. $table = $table->skip($request->get("offset",0))
  66. ->take($request->get('limit',10));
  67. $result = $table->get();
  68. return $this->ok(["rows"=>SearchTitleResource::collection($result),"count"=>$count]);
  69. break;
  70. default:
  71. # code...
  72. break;
  73. }
  74. }
  75. public function pali(Request $request)
  76. {
  77. //
  78. $bookId = [];
  79. if($request->has('book')){
  80. $bookId = [(int)$request->get('book')];
  81. }else if($request->has('tags')){
  82. //查询搜索范围
  83. //查询搜索范围
  84. $tagItems = explode(';',$request->get('tags'));
  85. foreach ($tagItems as $tagItem) {
  86. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  87. }
  88. }
  89. $searchChapters = [];
  90. $searchBooks = [];
  91. $searchBookId = [];
  92. $queryBookId = '';
  93. if(count($bookId) > 0){
  94. $queryBookId = ' AND pcd_book_id in ('.implode(',',$bookId).') ';
  95. }
  96. $key = explode(';',$request->get('key')) ;
  97. $param = [];
  98. $countParam = [];
  99. switch ($request->get('match','case')) {
  100. case 'complete':
  101. case 'case':
  102. # code...
  103. $querySelect_rank_base = " ts_rank('{0.1, 1, 0.3, 0.2}',
  104. full_text_search_weighted,
  105. websearch_to_tsquery('pali', ?)) ";
  106. $querySelect_rank_head = implode('+', array_fill(0, count($key), $querySelect_rank_base));
  107. $param = array_merge($param,$key);
  108. $querySelect_rank = " {$querySelect_rank_head} AS rank, ";
  109. $querySelect_highlight = " ts_headline('pali', content,
  110. websearch_to_tsquery('pali', ?),
  111. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  112. AS highlight,";
  113. array_push($param,implode(' ',$key));
  114. break;
  115. case 'similar':
  116. # 形似,去掉变音符号
  117. $key = Tools::getWordEn($key[0]);
  118. $querySelect_rank = "
  119. ts_rank('{0.1, 1, 0.3, 0.2}',
  120. full_text_search_weighted_unaccent,
  121. websearch_to_tsquery('pali_unaccent', ?))
  122. AS rank, ";
  123. $param[] = $key;
  124. $querySelect_highlight = " ts_headline('pali_unaccent', content,
  125. websearch_to_tsquery('pali_unaccent', ?),
  126. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  127. AS highlight,";
  128. $param[] = $key;
  129. break;
  130. }
  131. $_queryWhere = $this->getQueryWhere($request->get('key'),$request->get('match','case'));
  132. $queryWhere = $_queryWhere['query'];
  133. $param = array_merge($param,$_queryWhere['param']);
  134. $querySelect_2 = " book,paragraph,content ";
  135. $queryCount = "SELECT count(*) as co FROM fts_texts WHERE {$queryWhere} {$queryBookId};";
  136. $resultCount = DB::select($queryCount, $_queryWhere['param']);
  137. $limit = $request->get('limit',10);
  138. $offset = $request->get('offset',0);
  139. switch ( $request->get('orderby',"rank")) {
  140. case 'rank':
  141. $orderby = " ORDER BY rank DESC ";
  142. break;
  143. case 'paragraph':
  144. $orderby = " ORDER BY book,paragraph ";
  145. break;
  146. default:
  147. $orderby = "";
  148. break;
  149. };
  150. $query = "SELECT
  151. {$querySelect_rank}
  152. {$querySelect_highlight}
  153. {$querySelect_2}
  154. FROM fts_texts
  155. WHERE
  156. {$queryWhere}
  157. {$queryBookId}
  158. {$orderby}
  159. LIMIT ? OFFSET ? ;";
  160. $param[] = $limit;
  161. $param[] = $offset;
  162. $result = DB::select($query, $param);
  163. return $this->ok(["rows"=>SearchResource::collection($result),"count"=>$resultCount[0]->co]);
  164. }
  165. public function pali_rpc(Request $request)
  166. {
  167. //
  168. $bookId = [];
  169. if($request->has('book')){
  170. $bookId = [(int)$request->get('book')];
  171. }else if($request->has('tags')){
  172. //查询搜索范围
  173. //查询搜索范围
  174. $tagItems = explode(';',$request->get('tags'));
  175. foreach ($tagItems as $tagItem) {
  176. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  177. }
  178. }
  179. $key = explode(';',$request->get('key')) ;
  180. $limit = $request->get('limit',10);
  181. $offset = $request->get('offset',0);
  182. $matchMode = $request->get('match','case');
  183. $result = PaliSearch::search($key,$bookId,$matchMode,$offset,$limit);
  184. return $this->ok(["rows"=>SearchResource::collection(collect($result['rows'])),"count"=>$result['total']]);
  185. }
  186. public function page(Request $request)
  187. {
  188. //
  189. $searchChapters = [];
  190. $searchBooks = [];
  191. $searchBookId = [];
  192. $queryBookId = '';
  193. $bookId = [];
  194. if($request->has('book')){
  195. $bookId[] = $request->get('book');
  196. }else if($request->has('tags')){
  197. //查询搜索范围
  198. //查询搜索范围
  199. $tagItems = explode(';',$request->get('tags'));
  200. foreach ($tagItems as $tagItem) {
  201. # code...
  202. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  203. }
  204. }
  205. $key = $request->get('key');
  206. $searchKey = '';
  207. $page = explode('.',$key);
  208. if(count($page)===2){
  209. $table = PageNumber::where('type',$request->get('type'))
  210. ->where('volume',(int)$page[0])
  211. ->where('page',(int)$page[1]);
  212. }else{
  213. if(is_numeric($key)){
  214. $table = PageNumber::where('type',$request->get('type'))->where('page',$key);
  215. }else{
  216. $table = PageNumber::where('type',$request->get('type'))->where('page',(int)$key);
  217. }
  218. }
  219. if(count($bookId)>0){
  220. $table = $table->whereIn('pcd_book_id',$bookId);
  221. }
  222. $count = $table->count();
  223. $table = $table->select(['book','paragraph']);
  224. $table->skip($request->get("offset",0))->take($request->get('limit',10));
  225. $result = $table->get();
  226. return $this->ok(["rows"=>SearchResource::collection($result),"count"=>$count]);
  227. }
  228. public function book_list(Request $request){
  229. $searchChapters = [];
  230. $searchBooks = [];
  231. $queryBookId = '';
  232. $bookId = [];
  233. if($request->has('tags')){
  234. //查询搜索范围
  235. $tagItems = explode(';',$request->get('tags'));
  236. foreach ($tagItems as $tagItem) {
  237. # code...
  238. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  239. }
  240. $queryBookId = ' AND pcd_book_id in ('.implode(',',$bookId).') ';
  241. }
  242. $key = $request->get('key');
  243. switch ($request->get('view','pali')) {
  244. case 'pali':
  245. # code...
  246. $pageHead = ['M','P','T','V','O'];
  247. if(substr($key,0,4) === 'para' || in_array(substr($key,0,1),$pageHead)){
  248. $queryWhere = "type='.ctl.' AND word = ?";
  249. $query = "SELECT pcd_book_id, count(*) as co FROM wbw_templates WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  250. $result = DB::select($query, [$key]);
  251. }else{
  252. if (App::environment(['local', 'staging'])) {
  253. $rpc_result = PaliSearch::book_list(explode(';',$key) ,$bookId,
  254. $request->get('match','case'));
  255. $result = collect($rpc_result['rows']);
  256. }else{
  257. $queryWhere = $this->getQueryWhere($key,$request->get('match','case'));
  258. $query = "SELECT pcd_book_id, count(*) as co FROM fts_texts WHERE {$queryWhere['query']} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  259. $result = DB::select($query, $queryWhere['param']);
  260. }
  261. }
  262. break;
  263. case 'page':
  264. $type = $request->get('type','P');
  265. $word = "{$type}%0{$key}";
  266. $queryWhere = "type='.ctl.' AND word like ?";
  267. $query = "SELECT pcd_book_id, count(*) as co FROM wbw_templates WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  268. $result = DB::select($query, [$word]);
  269. break;
  270. case 'title':
  271. $keyLike = '%'.$key.'%';
  272. $queryWhere = "\"level\" < 8 and (\"title_en\"::text like ? or \"title\"::text like ?)";
  273. $query = "SELECT pcd_book_id, count(*) as co FROM pali_texts WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  274. $result = DB::select($query, [$keyLike,$keyLike]);
  275. break;
  276. default:
  277. # code...
  278. return $this->error('unknown view');
  279. break;
  280. }
  281. if($result){
  282. return $this->ok(["rows"=>SearchBookResource::collection($result),"count"=>count($result)]);
  283. }else{
  284. return $this->ok(["rows"=>[],"count"=>0]);
  285. }
  286. }
  287. private function getQueryWhere($key,$match){
  288. $key = explode(';',$key) ;
  289. $param = [];
  290. $queryWhere = '';
  291. switch ($match) {
  292. case 'complete':
  293. case 'case':
  294. # code...
  295. $queryWhereBase = " full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ";
  296. $queryWhereBody = implode(' or ', array_fill(0, count($key), $queryWhereBase));
  297. $queryWhere = " ({$queryWhereBody}) ";
  298. $param = array_merge($param,$key);
  299. break;
  300. case 'similar':
  301. # 形似,去掉变音符号
  302. $queryWhere = " full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', ?) ";
  303. $key = Tools::getWordEn($key[0]);
  304. $param = [$key];
  305. break;
  306. };
  307. return (['query'=>$queryWhere,'param'=>$param]);
  308. }
  309. public function getBookIdByTags($tags){
  310. $searchBookId = [];
  311. if(empty($tags)){
  312. return $searchBookId;
  313. }
  314. //查询搜索范围
  315. $tagIds = Tag::whereIn('name',$tags)->select('id')->get();
  316. $paliTextIds = TagMap::where('table_name','pali_texts')->whereIn('tag_id',$tagIds)->select('anchor_id')->get();
  317. $paliPara=[];
  318. foreach ($paliTextIds as $key => $value) {
  319. # code...
  320. if(isset($paliPara[$value->anchor_id])){
  321. $paliPara[$value->anchor_id]++;
  322. }else{
  323. $paliPara[$value->anchor_id]=1;
  324. }
  325. }
  326. $paliId=[];
  327. foreach ($paliPara as $key => $value) {
  328. # code...
  329. if($value===count($tags)){
  330. $paliId[] = $key;
  331. }
  332. }
  333. $para = PaliText::where('level',1)->whereIn('uid',$paliId)->get();
  334. if(count($para)>0){
  335. foreach ($para as $key => $value) {
  336. # code...
  337. $book_id = BookTitle::where('book',$value['book'])
  338. ->where('paragraph',$value['paragraph'])
  339. ->value('sn');
  340. if(!empty($book_id)){
  341. $searchBookId[] = $book_id;
  342. }
  343. }
  344. }
  345. return $searchBookId;
  346. }
  347. /**
  348. * Store a newly created resource in storage.
  349. *
  350. * @param \Illuminate\Http\Request $request
  351. * @return \Illuminate\Http\Response
  352. */
  353. public function store(Request $request)
  354. {
  355. //
  356. }
  357. /**
  358. * Display the specified resource.
  359. *
  360. * @param int $id
  361. * @return \Illuminate\Http\Response
  362. */
  363. public function show($id)
  364. {
  365. //
  366. }
  367. /**
  368. * Update the specified resource in storage.
  369. *
  370. * @param \Illuminate\Http\Request $request
  371. * @param int $id
  372. * @return \Illuminate\Http\Response
  373. */
  374. public function update(Request $request, $id)
  375. {
  376. //
  377. }
  378. /**
  379. * Remove the specified resource from storage.
  380. *
  381. * @param int $id
  382. * @return \Illuminate\Http\Response
  383. */
  384. public function destroy($id)
  385. {
  386. //
  387. }
  388. }