2
0

SearchPaliWbwController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\SearchRequest;
  4. use App\Http\Resources\SearchBookResource;
  5. use App\Http\Resources\SearchPaliWbwResource;
  6. use App\Models\WbwTemplate;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\DB;
  10. class SearchPaliWbwController extends Controller
  11. {
  12. /**
  13. * 把 key 拆成词表,丢掉切出来的空串。
  14. *
  15. * SearchRequest 只保证 key 里有内容,`dhammo,,` 照样能通过;而空串一旦进了
  16. * whereIn('real', ...),就会命中 real 为空的那四百多万行、覆盖 49 万个段落,
  17. * 把无关结果混进命中里。
  18. *
  19. * @return string[]
  20. */
  21. private function keywords(?string $key): array
  22. {
  23. $words = array_map('trim', explode(',', (string) $key));
  24. return array_values(array_filter($words, fn (string $word): bool => $word !== ''));
  25. }
  26. /**
  27. * Display a listing of the resource.
  28. *
  29. * @return Response
  30. */
  31. public function index(SearchRequest $request)
  32. {
  33. // 获取书的范围
  34. $bookId = [];
  35. $search = new SearchController;
  36. if ($request->has('book')) {
  37. foreach (explode(',', $request->input('book')) as $key => $id) {
  38. $bookId[] = (int) $id;
  39. }
  40. } elseif ($request->has('tags')) {
  41. // 查询搜索范围
  42. // 查询搜索范围
  43. $tagItems = explode(';', $request->input('tags'));
  44. foreach ($tagItems as $tagItem) {
  45. $bookId = array_merge($bookId, $search->getBookIdByTags(explode(',', $tagItem)));
  46. }
  47. }
  48. $keyWords = $this->keywords($request->input('key'));
  49. $table = WbwTemplate::whereIn('real', $keyWords)
  50. ->groupBy(['book', 'paragraph'])
  51. ->selectRaw('book,paragraph,sum(weight) as rank');
  52. $whereBold = '';
  53. if ($request->input('bold') === 'on') {
  54. $table = $table->where('style', 'bld');
  55. $whereBold = " and style='bld'";
  56. } elseif ($request->input('bold') === 'off') {
  57. $table = $table->where('style', '<>', 'bld');
  58. $whereBold = " and style <> 'bld'";
  59. }
  60. $placeholderWord = implode(',', array_fill(0, count($keyWords), '?'));
  61. $whereWord = "real in ({$placeholderWord})";
  62. $whereBookId = '';
  63. if (count($bookId) > 0) {
  64. $table = $table->whereIn('pcd_book_id', $bookId);
  65. $placeholderBookId = implode(',', array_fill(0, count($bookId), '?'));
  66. $whereBookId = " and pcd_book_id in ({$placeholderBookId}) ";
  67. }
  68. $queryCount = "SELECT count(*) FROM ( SELECT book,paragraph FROM wbw_templates WHERE $whereWord $whereBookId $whereBold GROUP BY book,paragraph) T;";
  69. $count = DB::select($queryCount, array_merge($keyWords, $bookId));
  70. $table = $table->orderBy('rank', 'desc');
  71. $table = $table->skip($request->input('offset', 0))
  72. ->take($request->input('limit', 10));
  73. $result = $table->get();
  74. return $this->ok([
  75. 'rows' => SearchPaliWbwResource::collection($result),
  76. 'count' => $count[0]->count,
  77. ]);
  78. }
  79. public function book_list(SearchRequest $request)
  80. {
  81. // 获取书的范围
  82. $bookId = [];
  83. $search = new SearchController;
  84. if ($request->has('tags')) {
  85. // 查询搜索范围
  86. // 查询搜索范围
  87. $tagItems = explode(';', $request->input('tags'));
  88. foreach ($tagItems as $tagItem) {
  89. $bookId = array_merge($bookId, $search->getBookIdByTags(explode(',', $tagItem)));
  90. }
  91. }
  92. $keyWords = $this->keywords($request->input('key'));
  93. $table = WbwTemplate::whereIn('real', $keyWords);
  94. if (count($bookId) > 0) {
  95. $table = $table->whereIn('pcd_book_id', $bookId);
  96. }
  97. $table = $table->groupBy('pcd_book_id')
  98. ->selectRaw('pcd_book_id,count(*) as co')
  99. ->orderBy('co', 'desc');
  100. $result = $table->get();
  101. return $this->ok(['rows' => SearchBookResource::collection($result), 'count' => count($result)]);
  102. }
  103. /**
  104. * Store a newly created resource in storage.
  105. *
  106. * @return Response
  107. */
  108. public function store(Request $request)
  109. {
  110. return $this->index($request);
  111. }
  112. /**
  113. * Display the specified resource.
  114. *
  115. * @return Response
  116. */
  117. public function show(WbwTemplate $wbwTemplate)
  118. {
  119. //
  120. }
  121. /**
  122. * Update the specified resource in storage.
  123. *
  124. * @return Response
  125. */
  126. public function update(Request $request, WbwTemplate $wbwTemplate)
  127. {
  128. //
  129. }
  130. /**
  131. * Remove the specified resource from storage.
  132. *
  133. * @return Response
  134. */
  135. public function destroy(WbwTemplate $wbwTemplate)
  136. {
  137. //
  138. }
  139. }