SearchController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\SearchBookResource;
  13. use Illuminate\Support\Facades\Log;
  14. use App\Tools\Tools;
  15. class SearchController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function index(Request $request)
  23. {
  24. //
  25. $searchChapters = [];
  26. $searchBooks = [];
  27. $searchBookId = [];
  28. $queryBookId = '';
  29. if($request->has('book')){
  30. $queryBookId = ' AND pcd_book_id = ' . (int)$request->get('book');
  31. }else if($request->has('tags')){
  32. //查询搜索范围
  33. $tags = explode(',',$request->get('tags'));
  34. $tagIds = Tag::whereIn('name',$tags)->select('id')->get();
  35. $paliTextIds = TagMap::where('table_name','pali_texts')->whereIn('tag_id',$tagIds)->select('anchor_id')->get();
  36. $paliPara=[];
  37. foreach ($paliTextIds as $key => $value) {
  38. # code...
  39. if(isset($paliPara[$value->anchor_id])){
  40. $paliPara[$value->anchor_id]++;
  41. }else{
  42. $paliPara[$value->anchor_id]=1;
  43. }
  44. }
  45. $paliId=[];
  46. foreach ($paliPara as $key => $value) {
  47. # code...
  48. if($value===count($tags)){
  49. $paliId[] = $key;
  50. }
  51. }
  52. $para = PaliText::where('level',1)->whereIn('uid',$paliId)->get();
  53. Log::info($para);
  54. if(count($para)>0){
  55. foreach ($para as $key => $value) {
  56. # code...
  57. $book_id = BookTitle::where('book',$value['book'])->where('paragraph',$value['paragraph'])->value('id');
  58. if(!empty($book_id)){
  59. $searchBookId[] = $book_id;
  60. }
  61. $searchChapters[] = ['book'=>$value['book'],'paragraph'=>$value['paragraph']];
  62. }
  63. $queryBookId = ' AND pcd_book_id in ('.implode(',',$searchBookId).') ';
  64. }
  65. }
  66. $key = explode(';',$request->get('key')) ;
  67. $param = [];
  68. $countParam = [];
  69. switch ($request->get('match','case')) {
  70. case 'complete':
  71. # code...
  72. $querySelect_rank = "
  73. ts_rank('{0.1, 0.2, 0.4, 1}',
  74. full_text_search_weighted,
  75. websearch_to_tsquery('pali', ?))
  76. AS rank, ";
  77. $querySelect_highlight = " ts_headline('pali', content,
  78. websearch_to_tsquery('pali', ?),
  79. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  80. AS highlight,";
  81. $queryWhere = " full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ";
  82. $param = [$key[0],$key[0],$key[0]];
  83. break;
  84. case 'case':
  85. # code...
  86. $querySelect_rank_base = " ts_rank('{0.1, 0.2, 0.4, 1}',
  87. full_text_search_weighted,
  88. websearch_to_tsquery('pali', ?)) ";
  89. $querySelect_rank_head = implode('+', array_fill(0, count($key), $querySelect_rank_base));
  90. $param = array_merge($param,$key);
  91. $querySelect_rank = " {$querySelect_rank_head} AS rank, ";
  92. $querySelect_highlight = " ts_headline('pali', content,
  93. websearch_to_tsquery('pali', ?),
  94. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  95. AS highlight,";
  96. array_push($param,implode(' ',$key));
  97. $queryWhereBase = " full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ";
  98. $queryWhereBody = implode(' or ', array_fill(0, count($key), $queryWhereBase));
  99. $queryWhere = " ({$queryWhereBody}) ";
  100. $param = array_merge($param,$key);
  101. break;
  102. case 'similar':
  103. # 形似,去掉变音符号
  104. $querySelect_rank = "
  105. ts_rank('{0.1, 0.2, 0.4, 1}',
  106. full_text_search_weighted_unaccent,
  107. websearch_to_tsquery('pali_unaccent', ?))
  108. AS rank, ";
  109. $querySelect_highlight = " ts_headline('pali_unaccent', content,
  110. websearch_to_tsquery('pali_unaccent', ?),
  111. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  112. AS highlight,";
  113. $queryWhere = " full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', ?) ";
  114. $key = Tools::getWordEn($key);
  115. $param = [$key,$key,$key];
  116. break;
  117. }
  118. $querySelect_2 = " book,paragraph,content ";
  119. $queryCount = "SELECT count(*) as co FROM fts_texts WHERE {$queryWhere} {$queryBookId};";
  120. $resultCount = DB::select($queryCount, $key);
  121. $limit = $request->get('limit',10);
  122. $offset = $request->get('offset',0);
  123. switch ( $request->get('orderby',"rank")) {
  124. case 'rank':
  125. $orderby = " ORDER BY rank DESC ";
  126. break;
  127. case 'paragraph':
  128. $orderby = " ORDER BY book,paragraph ";
  129. break;
  130. default:
  131. $orderby = "";
  132. break;
  133. };
  134. $query = "SELECT
  135. {$querySelect_rank}
  136. {$querySelect_highlight}
  137. {$querySelect_2}
  138. FROM fts_texts
  139. WHERE
  140. {$queryWhere}
  141. {$queryBookId}
  142. {$orderby}
  143. LIMIT ? OFFSET ? ;";
  144. $param[] = $limit;
  145. $param[] = $offset;
  146. $result = DB::select($query, $param);
  147. //待查询单词列表
  148. //$caseMan = new CaseMan();
  149. //$wordSpell = $caseMan->BaseToWord($key);
  150. return $this->ok(["rows"=>SearchResource::collection($result),"count"=>$resultCount[0]->co]);
  151. }
  152. public function book_list(Request $request){
  153. $searchChapters = [];
  154. $searchBooks = [];
  155. $searchBookId = [];
  156. $queryBookId = '';
  157. if($request->has('tags')){
  158. //查询搜索范围
  159. $tags = explode(',',$request->get('tags'));
  160. $tagIds = Tag::whereIn('name',$tags)->select('id')->get();
  161. $paliTextIds = TagMap::where('table_name','pali_texts')->whereIn('tag_id',$tagIds)->select('anchor_id')->get();
  162. $paliPara=[];
  163. foreach ($paliTextIds as $key => $value) {
  164. # code...
  165. if(isset($paliPara[$value->anchor_id])){
  166. $paliPara[$value->anchor_id]++;
  167. }else{
  168. $paliPara[$value->anchor_id]=1;
  169. }
  170. }
  171. $paliId=[];
  172. foreach ($paliPara as $key => $value) {
  173. # code...
  174. if($value===count($tags)){
  175. $paliId[] = $key;
  176. }
  177. }
  178. $para = PaliText::where('level',1)->whereIn('uid',$paliId)->get();
  179. Log::info($para);
  180. if(count($para)>0){
  181. foreach ($para as $key => $value) {
  182. # code...
  183. $book_id = BookTitle::where('book',$value['book'])->where('paragraph',$value['paragraph'])->value('id');
  184. if(!empty($book_id)){
  185. $searchBookId[] = $book_id;
  186. }
  187. $searchChapters[] = ['book'=>$value['book'],'paragraph'=>$value['paragraph']];
  188. }
  189. $queryBookId = ' AND pcd_book_id in ('.implode(',',$searchBookId).') ';
  190. }
  191. }
  192. $key = $request->get('key');
  193. $queryWhere = "( full_text_search_weighted @@ websearch_to_tsquery('pali', ?) OR
  194. full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', ?) )";
  195. $query = "SELECT pcd_book_id, count(*) as co FROM fts_texts WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  196. $result = DB::select($query, [$key,$key]);
  197. return $this->ok(["rows"=>SearchBookResource::collection($result),"count"=>count($result)]);
  198. }
  199. /**
  200. * Store a newly created resource in storage.
  201. *
  202. * @param \Illuminate\Http\Request $request
  203. * @return \Illuminate\Http\Response
  204. */
  205. public function store(Request $request)
  206. {
  207. //
  208. }
  209. /**
  210. * Display the specified resource.
  211. *
  212. * @param int $id
  213. * @return \Illuminate\Http\Response
  214. */
  215. public function show($id)
  216. {
  217. //
  218. }
  219. /**
  220. * Update the specified resource in storage.
  221. *
  222. * @param \Illuminate\Http\Request $request
  223. * @param int $id
  224. * @return \Illuminate\Http\Response
  225. */
  226. public function update(Request $request, $id)
  227. {
  228. //
  229. }
  230. /**
  231. * Remove the specified resource from storage.
  232. *
  233. * @param int $id
  234. * @return \Illuminate\Http\Response
  235. */
  236. public function destroy($id)
  237. {
  238. //
  239. }
  240. }