SearchController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. use App\Models\WbwTemplate;
  16. class SearchController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function index(Request $request){
  24. switch ($request->get('view','pali')) {
  25. case 'pali':
  26. $pageHead = ['M','P','T','V','O'];
  27. $key = $request->get('key');
  28. if(substr($key,0,4) === 'para' || in_array(substr($key,0,1),$pageHead)){
  29. return $this->page($request);
  30. }else{
  31. return $this->pali($request);
  32. }
  33. break;
  34. case 'page':
  35. return $this->page($request);
  36. break;
  37. default:
  38. # code...
  39. break;
  40. }
  41. }
  42. public function pali(Request $request)
  43. {
  44. //
  45. $searchChapters = [];
  46. $searchBooks = [];
  47. $searchBookId = [];
  48. $queryBookId = '';
  49. if($request->has('book')){
  50. $queryBookId = ' AND pcd_book_id = ' . (int)$request->get('book');
  51. }else if($request->has('tags')){
  52. //查询搜索范围
  53. //查询搜索范围
  54. $tagItems = explode(';',$request->get('tags'));
  55. $bookId = [];
  56. foreach ($tagItems as $tagItem) {
  57. # code...
  58. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  59. }
  60. $queryBookId = ' AND pcd_book_id in ('.implode(',',$bookId).') ';
  61. }
  62. $key = explode(';',$request->get('key')) ;
  63. $param = [];
  64. $countParam = [];
  65. switch ($request->get('match','case')) {
  66. case 'complete':
  67. case 'case':
  68. # code...
  69. $querySelect_rank_base = " ts_rank('{0.1, 0.2, 0.4, 1}',
  70. full_text_search_weighted,
  71. websearch_to_tsquery('pali', ?)) ";
  72. $querySelect_rank_head = implode('+', array_fill(0, count($key), $querySelect_rank_base));
  73. $param = array_merge($param,$key);
  74. $querySelect_rank = " {$querySelect_rank_head} AS rank, ";
  75. $querySelect_highlight = " ts_headline('pali', content,
  76. websearch_to_tsquery('pali', ?),
  77. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  78. AS highlight,";
  79. array_push($param,implode(' ',$key));
  80. break;
  81. case 'similar':
  82. # 形似,去掉变音符号
  83. $key = Tools::getWordEn($key[0]);
  84. $querySelect_rank = "
  85. ts_rank('{0.1, 0.2, 0.4, 1}',
  86. full_text_search_weighted_unaccent,
  87. websearch_to_tsquery('pali_unaccent', ?))
  88. AS rank, ";
  89. $param[] = $key;
  90. $querySelect_highlight = " ts_headline('pali_unaccent', content,
  91. websearch_to_tsquery('pali_unaccent', ?),
  92. 'StartSel = ~~, StopSel = ~~,MaxWords=3500, MinWords=3500,HighlightAll=TRUE')
  93. AS highlight,";
  94. $param[] = $key;
  95. break;
  96. }
  97. $_queryWhere = $this->getQueryWhere($request->get('key'),$request->get('match','case'));
  98. $queryWhere = $_queryWhere['query'];
  99. $param = array_merge($param,$_queryWhere['param']);
  100. $querySelect_2 = " book,paragraph,content ";
  101. $queryCount = "SELECT count(*) as co FROM fts_texts WHERE {$queryWhere} {$queryBookId};";
  102. $resultCount = DB::select($queryCount, $_queryWhere['param']);
  103. $limit = $request->get('limit',10);
  104. $offset = $request->get('offset',0);
  105. switch ( $request->get('orderby',"rank")) {
  106. case 'rank':
  107. $orderby = " ORDER BY rank DESC ";
  108. break;
  109. case 'paragraph':
  110. $orderby = " ORDER BY book,paragraph ";
  111. break;
  112. default:
  113. $orderby = "";
  114. break;
  115. };
  116. $query = "SELECT
  117. {$querySelect_rank}
  118. {$querySelect_highlight}
  119. {$querySelect_2}
  120. FROM fts_texts
  121. WHERE
  122. {$queryWhere}
  123. {$queryBookId}
  124. {$orderby}
  125. LIMIT ? OFFSET ? ;";
  126. $param[] = $limit;
  127. $param[] = $offset;
  128. $result = DB::select($query, $param);
  129. //待查询单词列表
  130. //$caseMan = new CaseMan();
  131. //$wordSpell = $caseMan->BaseToWord($key);
  132. return $this->ok(["rows"=>SearchResource::collection($result),"count"=>$resultCount[0]->co]);
  133. }
  134. public function page(Request $request)
  135. {
  136. //
  137. $searchChapters = [];
  138. $searchBooks = [];
  139. $searchBookId = [];
  140. $queryBookId = '';
  141. $bookId = [];
  142. if($request->has('book')){
  143. $bookId[] = $request->get('book');
  144. }else if($request->has('tags')){
  145. //查询搜索范围
  146. //查询搜索范围
  147. $tagItems = explode(';',$request->get('tags'));
  148. foreach ($tagItems as $tagItem) {
  149. # code...
  150. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  151. }
  152. }
  153. //type='.ctl.' and word like 'P%038'
  154. $key = $request->get('key');
  155. $searchKey = '';
  156. $table = WbwTemplate::where('type','.ctl.');
  157. if(is_numeric($key)){
  158. $table = $table->where('word','like',$request->get('type')."%0".$key);
  159. }else{
  160. $table = $table->where('word',$key);
  161. }
  162. if(count($bookId)>0){
  163. $table = $table->whereIn('pcd_book_id',$bookId);
  164. }
  165. $count = $table->count();
  166. $table = $table->select(['book','paragraph']);
  167. $table->skip($request->get("offset",0))->take($request->get('limit',10));
  168. $result = $table->get();
  169. return $this->ok(["rows"=>SearchResource::collection($result),"count"=>$count]);
  170. }
  171. public function book_list(Request $request){
  172. $searchChapters = [];
  173. $searchBooks = [];
  174. $queryBookId = '';
  175. if($request->has('tags')){
  176. //查询搜索范围
  177. $tagItems = explode(';',$request->get('tags'));
  178. $bookId = [];
  179. foreach ($tagItems as $tagItem) {
  180. # code...
  181. $bookId = array_merge($bookId,$this->getBookIdByTags(explode(',',$tagItem)));
  182. }
  183. $queryBookId = ' AND pcd_book_id in ('.implode(',',$bookId).') ';
  184. }
  185. $key = $request->get('key');
  186. switch ($request->get('view','pali')) {
  187. case 'pali':
  188. # code...
  189. $pageHead = ['M','P','T','V','O'];
  190. if(substr($key,0,4) === 'para' || in_array(substr($key,0,1),$pageHead)){
  191. $queryWhere = "type='.ctl.' AND word = ?";
  192. $query = "SELECT pcd_book_id, count(*) as co FROM wbw_templates WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  193. $result = DB::select($query, [$key]);
  194. }else{
  195. $queryWhere = $this->getQueryWhere($key,$request->get('match','case'));
  196. $query = "SELECT pcd_book_id, count(*) as co FROM fts_texts WHERE {$queryWhere['query']} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  197. $result = DB::select($query, $queryWhere['param']);
  198. }
  199. break;
  200. case 'page';
  201. $type = $request->get('type','P');
  202. $word = "{$type}%0{$key}";
  203. $queryWhere = "type='.ctl.' AND word like ?";
  204. $query = "SELECT pcd_book_id, count(*) as co FROM wbw_templates WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  205. $result = DB::select($query, [$word]);
  206. break;
  207. default:
  208. # code...
  209. return $this->error('unknown view');
  210. break;
  211. }
  212. return $this->ok(["rows"=>SearchBookResource::collection($result),"count"=>count($result)]);
  213. }
  214. private function getQueryWhere($key,$match){
  215. $key = explode(';',$key) ;
  216. $param = [];
  217. $queryWhere = '';
  218. switch ($match) {
  219. case 'complete':
  220. case 'case':
  221. # code...
  222. $queryWhereBase = " full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ";
  223. $queryWhereBody = implode(' or ', array_fill(0, count($key), $queryWhereBase));
  224. $queryWhere = " ({$queryWhereBody}) ";
  225. $param = array_merge($param,$key);
  226. break;
  227. case 'similar':
  228. # 形似,去掉变音符号
  229. $queryWhere = " full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', ?) ";
  230. $key = Tools::getWordEn($key[0]);
  231. $param = [$key];
  232. break;
  233. };
  234. return (['query'=>$queryWhere,'param'=>$param]);
  235. }
  236. private function getBookIdByTags($tags){
  237. $searchBookId = [];
  238. if(empty($tags)){
  239. return $searchBookId;
  240. }
  241. //查询搜索范围
  242. $tagIds = Tag::whereIn('name',$tags)->select('id')->get();
  243. $paliTextIds = TagMap::where('table_name','pali_texts')->whereIn('tag_id',$tagIds)->select('anchor_id')->get();
  244. $paliPara=[];
  245. foreach ($paliTextIds as $key => $value) {
  246. # code...
  247. if(isset($paliPara[$value->anchor_id])){
  248. $paliPara[$value->anchor_id]++;
  249. }else{
  250. $paliPara[$value->anchor_id]=1;
  251. }
  252. }
  253. $paliId=[];
  254. foreach ($paliPara as $key => $value) {
  255. # code...
  256. if($value===count($tags)){
  257. $paliId[] = $key;
  258. }
  259. }
  260. $para = PaliText::where('level',1)->whereIn('uid',$paliId)->get();
  261. if(count($para)>0){
  262. foreach ($para as $key => $value) {
  263. # code...
  264. $book_id = BookTitle::where('book',$value['book'])->where('paragraph',$value['paragraph'])->value('id');
  265. if(!empty($book_id)){
  266. $searchBookId[] = $book_id;
  267. }
  268. }
  269. }
  270. return $searchBookId;
  271. }
  272. /**
  273. * Store a newly created resource in storage.
  274. *
  275. * @param \Illuminate\Http\Request $request
  276. * @return \Illuminate\Http\Response
  277. */
  278. public function store(Request $request)
  279. {
  280. //
  281. }
  282. /**
  283. * Display the specified resource.
  284. *
  285. * @param int $id
  286. * @return \Illuminate\Http\Response
  287. */
  288. public function show($id)
  289. {
  290. //
  291. }
  292. /**
  293. * Update the specified resource in storage.
  294. *
  295. * @param \Illuminate\Http\Request $request
  296. * @param int $id
  297. * @return \Illuminate\Http\Response
  298. */
  299. public function update(Request $request, $id)
  300. {
  301. //
  302. }
  303. /**
  304. * Remove the specified resource from storage.
  305. *
  306. * @param int $id
  307. * @return \Illuminate\Http\Response
  308. */
  309. public function destroy($id)
  310. {
  311. //
  312. }
  313. }