SearchController.php 14 KB

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