SearchController.php 8.5 KB

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