SearchController.php 15 KB

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