SearchController.php 8.9 KB

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