SearchController.php 8.1 KB

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