SearchController.php 14 KB

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