SearchController.php 11 KB

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