2
0

SearchPlusController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\DTO\Search\HitItemDTO;
  4. use App\Http\Controllers\Concerns\ResolvesCurrentUser;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Resources\V3\BaseResource;
  7. use App\Services\OpenSearchService;
  8. use App\Services\V3\ChannelAbilityService;
  9. use Illuminate\Http\Request;
  10. class SearchPlusController extends Controller
  11. {
  12. /**
  13. * 构造函数,注入 OpenSearchService
  14. */
  15. use ResolvesCurrentUser;
  16. /** 整章正文文档的 id:tipitaka_chapter_{book}-{para}_{channel uuid},见 IndexTipitaka */
  17. private const CHAPTER_DOC = '/^tipitaka_chapter_\d+-\d+_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i';
  18. public function __construct(
  19. protected OpenSearchService $searchService,
  20. private readonly ChannelAbilityService $abilities,
  21. ) {}
  22. /**
  23. * 全文检索
  24. *
  25. * 支持 fuzzy / exact / semantic / hybrid 四种检索模式,底层走 OpenSearch。
  26. * 同样的参数也可以用 POST /v3/search 提交(参数多、URL 放不下时用)。
  27. *
  28. * @unauthenticated
  29. *
  30. * @queryParam q string required 搜索关键词
  31. * @queryParam search_mode string 检索模式。Enum: fuzzy,exact,semantic,hybrid,definition Default: fuzzy
  32. * @queryParam resource_type string 资源类型。
  33. * Enum: article,term,dictionary,translation,origin_text,nissaya
  34. * @queryParam resource_id string 限定某个资源 id
  35. * @queryParam granularity string 文档颗粒度。
  36. * Enum: book,chapter,sutta,section,paragraph,sentence
  37. * @queryParam language string 语言。Example: zh-Hans
  38. * @queryParam category string 文档分类,逗号分隔。Enum: pali,commentary,subcommentary
  39. * @queryParam tags string 标签过滤,逗号分隔
  40. * @queryParam page_refs array 页码标记。Example: ["V3.81","M3.58"]
  41. * @queryParam related_id array 关联 id。Example: ["chapter_93-5","m.n. 38"]
  42. * @queryParam author string 按作者或译者过滤
  43. * @queryParam channel string 按来源 channel 过滤
  44. * @queryParam page integer 页码。Default: 1
  45. * @queryParam page_size integer 每页数量,上限 100。Default: 20
  46. */
  47. public function index(Request $request)
  48. {
  49. // 获取所有输入参数(自动兼容 GET 和 POST)
  50. $input = $request->all();
  51. // 基础参数 - 使用 $input 或直接用 $request->input() (已兼容 GET/POST)
  52. $query = $request->input('q', '');
  53. $page = max(1, (int) $request->input('page', 1));
  54. $pageSize = min(100, (int) $request->input('page_size', 20));
  55. $searchMode = $request->input('search_mode', 'fuzzy');
  56. $resourceType = $request->input('resource_type'); // 资源类型
  57. $resourceId = $request->input('resource_id'); // 资源类型
  58. $granularity = $request->input('granularity'); // 文档颗粒度
  59. $language = $request->input('language'); // 语言
  60. $category = $request->has('category') ? explode(',', $request->input('category')) : null; // 分类
  61. $tags = $request->has('tags') ? explode(',', $request->input('tags')) : []; // 标签
  62. $pageRefs = $request->input('page_refs', []); // 页码标记
  63. $relatedId = $request->input('related_id'); // 关联 ID
  64. $author = $request->input('author'); // 作者/译者 (metadata.author)
  65. $channel = $request->input('channel'); // 来源渠道 (metadata.channel)
  66. // 确保数组类型参数正确解析(POST 时可能是 JSON 字符串)
  67. $tags = is_array($tags) ? $tags : (is_string($tags) ? json_decode($tags, true) ?? [] : []);
  68. $pageRefs = is_array($pageRefs) ? $pageRefs : (is_string($pageRefs) ? json_decode($pageRefs, true) ?? [] : []);
  69. // 组装搜索参数
  70. $params = [
  71. 'query' => $query,
  72. 'page' => $page,
  73. 'pageSize' => $pageSize,
  74. 'searchMode' => $searchMode,
  75. 'resourceType' => $resourceType,
  76. 'resourceId' => $resourceId,
  77. 'granularity' => $granularity,
  78. 'language' => $language,
  79. 'category' => $category,
  80. 'tags' => $tags,
  81. 'pageRefs' => $pageRefs,
  82. 'relatedId' => $relatedId,
  83. 'author' => $author,
  84. 'channel' => $channel,
  85. ];
  86. try {
  87. // 调用 OpenSearchService 执行搜索
  88. $result = $this->searchService->search($params);
  89. return response()->json([
  90. 'success' => true,
  91. 'data' => $result,
  92. 'query_info' => [
  93. 'original_query' => $query,
  94. 'search_mode' => $searchMode,
  95. 'request_method' => $request->method(), // 可选:返回请求方法
  96. ],
  97. ]);
  98. } catch (\Exception $e) {
  99. return response()->json([
  100. 'success' => false,
  101. 'error' => $e->getMessage(),
  102. ], 500);
  103. }
  104. }
  105. /**
  106. * 全文检索(POST)
  107. *
  108. * 与 `GET /v3/search` 完全等价,只是把参数放在请求体里——参数多、
  109. * 尤其是 page_refs / related_id 这类数组时,URL 放不下。
  110. *
  111. * @unauthenticated
  112. *
  113. * @bodyParam q string required 搜索关键词
  114. * @bodyParam search_mode string 检索模式。Enum: fuzzy,exact,semantic,hybrid,definition Default: fuzzy
  115. * @bodyParam resource_type string 资源类型
  116. * @bodyParam granularity string 文档颗粒度
  117. * @bodyParam language string 语言
  118. * @bodyParam category string 文档分类,逗号分隔
  119. * @bodyParam tags string 标签,逗号分隔
  120. * @bodyParam page_refs array 页码标记
  121. * @bodyParam related_id array 关联 id
  122. * @bodyParam author string 作者或译者
  123. * @bodyParam channel string 来源 channel
  124. * @bodyParam page integer 页码。Default: 1
  125. * @bodyParam page_size integer 每页数量,上限 100。Default: 20
  126. */
  127. public function store(Request $request)
  128. {
  129. // 直接调用 index 方法,复用搜索逻辑
  130. return $this->index($request);
  131. }
  132. /**
  133. * 按 id 取单条检索结果
  134. *
  135. * 直接从 OpenSearch 按文档 id 取回,不走检索。取不到返回 404。
  136. *
  137. * 整章正文文档(`tipitaka_chapter_{book}-{para}_{channel}`)按它所属 channel 判权限:
  138. * 私有 channel 只有拥有者、团队成员、被共享的人能读(要带 token)。索引里收了所有 channel,
  139. * 不在这里挡的话拼出 id 就能读到私有正文。
  140. *
  141. * @unauthenticated
  142. *
  143. * @urlParam search string required OpenSearch 文档 id
  144. *
  145. * @responseStatus 403 整章正文所属的 channel 是私有的,且当前身份看不见
  146. */
  147. public function show(Request $request, $id)
  148. {
  149. if (preg_match(self::CHAPTER_DOC, (string) $id, $m)) {
  150. $this->abilities->assertReadable($m[1], $this->currentUserUid($request));
  151. }
  152. try {
  153. return BaseResource::make(HitItemDTO::fromArray($this->searchService->get($id)));
  154. } catch (\Throwable $th) {
  155. abort(404, __('site.not_found'));
  156. }
  157. }
  158. }