SearchPlusController.php 6.1 KB

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