SearchController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\SearchRequest;
  4. use App\Http\Resources\SearchBookResource;
  5. use App\Http\Resources\SearchResource;
  6. use App\Http\Resources\SearchTitleResource;
  7. use App\Models\BookTitle;
  8. use App\Models\PageNumber;
  9. use App\Models\PaliText;
  10. use App\Models\Tag;
  11. use App\Models\TagMap;
  12. use App\Tools\PaliSearch;
  13. use App\Tools\Tools;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Http\Response;
  16. use Illuminate\Support\Facades\DB;
  17. class SearchController extends Controller
  18. {
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return Response
  23. */
  24. public function index(SearchRequest $request)
  25. {
  26. switch ($request->input('view', 'pali')) {
  27. case 'pali':
  28. $pageHead = ['M', 'P', 'T', 'V', 'O'];
  29. $key = $request->input('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_rpc($request);
  34. }
  35. break;
  36. case 'page':
  37. return $this->page($request);
  38. break;
  39. case 'title':
  40. $key = strtolower($request->input('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. if ($request->has('tags')) {
  47. // 查询搜索范围
  48. $tagItems = explode(';', $request->input('tags'));
  49. $bookId = [];
  50. foreach ($tagItems as $tagItem) {
  51. // code...
  52. $bookId = array_merge($bookId, $this->getBookIdByTags(explode(',', $tagItem)));
  53. }
  54. $table = $table->whereIn('pcd_book_id', $bookId);
  55. }
  56. $count = $table->count();
  57. $table = $table->orderBy($request->input('orderby', 'book'), $request->input('dir', 'asc'));
  58. $table = $table->skip($request->input('offset', 0))
  59. ->take($request->input('limit', 10));
  60. $result = $table->get();
  61. return $this->ok(['rows' => SearchTitleResource::collection($result), 'count' => $count]);
  62. break;
  63. default:
  64. // code...
  65. break;
  66. }
  67. }
  68. public function pali(Request $request)
  69. {
  70. //
  71. $bookId = [];
  72. if ($request->has('book')) {
  73. $bookId = [(int) $request->input('book')];
  74. } elseif ($request->has('tags')) {
  75. // 查询搜索范围
  76. // 查询搜索范围
  77. $tagItems = explode(';', $request->input('tags'));
  78. foreach ($tagItems as $tagItem) {
  79. $bookId = array_merge($bookId, $this->getBookIdByTags(explode(',', $tagItem)));
  80. }
  81. }
  82. $searchChapters = [];
  83. $searchBooks = [];
  84. $searchBookId = [];
  85. $queryBookId = '';
  86. if (count($bookId) > 0) {
  87. $queryBookId = ' AND pcd_book_id in ('.implode(',', $bookId).') ';
  88. }
  89. $key = explode(';', $request->input('key'));
  90. $param = [];
  91. $countParam = [];
  92. switch ($request->input('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->input('key'), $request->input('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->input('limit', 10);
  131. $offset = $request->input('offset', 0);
  132. switch ($request->input('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 pali_rpc(Request $request)
  159. {
  160. //
  161. $bookId = [];
  162. if ($request->has('book')) {
  163. $bookId = [(int) $request->input('book')];
  164. } elseif ($request->has('tags')) {
  165. // 查询搜索范围
  166. // 查询搜索范围
  167. $tagItems = explode(';', $request->input('tags'));
  168. foreach ($tagItems as $tagItem) {
  169. $bookId = array_merge($bookId, $this->getBookIdByTags(explode(',', $tagItem)));
  170. }
  171. }
  172. $key = explode(';', $request->input('key'));
  173. $limit = $request->input('limit', 10);
  174. $offset = $request->input('offset', 0);
  175. $matchMode = $request->input('match', 'case');
  176. $result = PaliSearch::search($key, $bookId, $matchMode, $offset, $limit);
  177. return $this->ok(['rows' => SearchResource::collection(collect($result['rows'])), 'count' => $result['total']]);
  178. }
  179. public function page(Request $request)
  180. {
  181. //
  182. $searchChapters = [];
  183. $searchBooks = [];
  184. $searchBookId = [];
  185. $queryBookId = '';
  186. $bookId = [];
  187. if ($request->has('book')) {
  188. $bookId[] = $request->input('book');
  189. } elseif ($request->has('tags')) {
  190. // 查询搜索范围
  191. // 查询搜索范围
  192. $tagItems = explode(';', $request->input('tags'));
  193. foreach ($tagItems as $tagItem) {
  194. // code...
  195. $bookId = array_merge($bookId, $this->getBookIdByTags(explode(',', $tagItem)));
  196. }
  197. }
  198. $key = $request->input('key');
  199. $searchKey = '';
  200. $page = explode('.', $key);
  201. if (count($page) === 2) {
  202. $table = PageNumber::where('type', $request->input('type'))
  203. ->where('volume', (int) $page[0])
  204. ->where('page', (int) $page[1]);
  205. } else {
  206. if (is_numeric($key)) {
  207. $table = PageNumber::where('type', $request->input('type'))->where('page', $key);
  208. } else {
  209. $table = PageNumber::where('type', $request->input('type'))->where('page', (int) $key);
  210. }
  211. }
  212. if (count($bookId) > 0) {
  213. $table = $table->whereIn('pcd_book_id', $bookId);
  214. }
  215. $count = $table->count();
  216. $table = $table->select(['book', 'paragraph']);
  217. $table->skip($request->input('offset', 0))->take($request->input('limit', 10));
  218. $result = $table->get();
  219. return $this->ok(['rows' => SearchResource::collection($result), 'count' => $count]);
  220. }
  221. public function book_list(SearchRequest $request)
  222. {
  223. $searchChapters = [];
  224. $searchBooks = [];
  225. $queryBookId = '';
  226. $bookId = [];
  227. if ($request->has('tags')) {
  228. // 查询搜索范围
  229. $tagItems = explode(';', $request->input('tags'));
  230. foreach ($tagItems as $tagItem) {
  231. // code...
  232. $bookId = array_merge($bookId, $this->getBookIdByTags(explode(',', $tagItem)));
  233. }
  234. $queryBookId = ' AND pcd_book_id in ('.implode(',', $bookId).') ';
  235. }
  236. $key = $request->input('key');
  237. switch ($request->input('view', 'pali')) {
  238. case 'pali':
  239. // code...
  240. $pageHead = ['M', 'P', 'T', 'V', 'O'];
  241. if (substr($key, 0, 4) === 'para' || in_array(substr($key, 0, 1), $pageHead)) {
  242. $queryWhere = "type='.ctl.' AND word = ?";
  243. $query = "SELECT pcd_book_id, count(*) as co FROM wbw_templates WHERE {$queryWhere} {$queryBookId} GROUP BY pcd_book_id ORDER BY co DESC;";
  244. $result = DB::select($query, [$key]);
  245. } else {
  246. $rpc_result = PaliSearch::book_list(
  247. explode(';', $key),
  248. $bookId,
  249. $request->input('match', 'case')
  250. );
  251. $result = collect($rpc_result['rows']);
  252. /*
  253. $queryWhere = $this->getQueryWhere($key,$request->input('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->input('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. {
  285. $key = explode(';', $key);
  286. $param = [];
  287. $queryWhere = '';
  288. switch ($match) {
  289. case 'complete':
  290. case 'case':
  291. // code...
  292. $queryWhereBase = " full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ";
  293. $queryWhereBody = implode(' or ', array_fill(0, count($key), $queryWhereBase));
  294. $queryWhere = " ({$queryWhereBody}) ";
  295. $param = array_merge($param, $key);
  296. break;
  297. case 'similar':
  298. // 形似,去掉变音符号
  299. $queryWhere = " full_text_search_weighted_unaccent @@ websearch_to_tsquery('pali_unaccent', ?) ";
  300. $key = Tools::getWordEn($key[0]);
  301. $param = [$key];
  302. break;
  303. }
  304. return ['query' => $queryWhere, 'param' => $param];
  305. }
  306. public function getBookIdByTags($tags)
  307. {
  308. $searchBookId = [];
  309. if (empty($tags)) {
  310. return $searchBookId;
  311. }
  312. // 查询搜索范围
  313. $tagIds = Tag::whereIn('name', $tags)->select('id')->get();
  314. $paliTextIds = TagMap::where('table_name', 'pali_texts')->whereIn('tag_id', $tagIds)->select('anchor_id')->get();
  315. $paliPara = [];
  316. foreach ($paliTextIds as $key => $value) {
  317. // code...
  318. if (isset($paliPara[$value->anchor_id])) {
  319. $paliPara[$value->anchor_id]++;
  320. } else {
  321. $paliPara[$value->anchor_id] = 1;
  322. }
  323. }
  324. $paliId = [];
  325. foreach ($paliPara as $key => $value) {
  326. // code...
  327. if ($value === count($tags)) {
  328. $paliId[] = $key;
  329. }
  330. }
  331. $para = PaliText::where('level', 1)->whereIn('uid', $paliId)->get();
  332. if (count($para) > 0) {
  333. foreach ($para as $key => $value) {
  334. // code...
  335. $book_id = BookTitle::where('book', $value['book'])
  336. ->where('paragraph', $value['paragraph'])
  337. ->value('sn');
  338. if (! empty($book_id)) {
  339. $searchBookId[] = $book_id;
  340. }
  341. }
  342. }
  343. return $searchBookId;
  344. }
  345. /**
  346. * Store a newly created resource in storage.
  347. *
  348. * @return Response
  349. */
  350. public function store(Request $request)
  351. {
  352. //
  353. }
  354. /**
  355. * Display the specified resource.
  356. *
  357. * @param int $id
  358. * @return Response
  359. */
  360. public function show($id)
  361. {
  362. //
  363. }
  364. /**
  365. * Update the specified resource in storage.
  366. *
  367. * @param int $id
  368. * @return 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 Response
  379. */
  380. public function destroy($id)
  381. {
  382. //
  383. }
  384. }