SearchPlusController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\OpenSearchService;
  4. use Illuminate\Http\Request;
  5. use App\DTO\Search\HitItemDTO;
  6. class SearchPlusController extends Controller
  7. {
  8. /**
  9. * 构造函数,注入 OpenSearchService
  10. *
  11. * @param \App\Services\OpenSearchService $searchService
  12. */
  13. public function __construct(protected OpenSearchService $searchService) {}
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * 处理搜索请求,支持 fuzzy / exact / semantic / hybrid 四种模式。
  18. * 接收查询参数并调用 OpenSearchService 执行搜索。
  19. *
  20. * 支持 GET 和 POST 请求
  21. *
  22. * @param \Illuminate\Http\Request $request
  23. * - q (string): 搜索关键词
  24. * - resource_type (string): 资源类型 (article|term|dictionary|translation|origin_text|nissaya)
  25. * - granularity (string): 文档颗粒度 (book|chapter|sutta|section|paragraph|sentence)
  26. * - language (string): 语言,如 pali, zh-Hans, zh-Hant, en-US, my
  27. * - category (string): 文档分类 (pali|commentary|subcommentary)
  28. * - tags (array): 标签过滤
  29. * - page_refs (array): 页码标记 ["V3.81","M3.58"]
  30. * - related_id (array): 关联 ID,如 ["chapter_93-5","m.n. 38"]
  31. * - author (string): 作者或译者 (metadata.author)
  32. * - channel (string): 来源渠道 (metadata.channel)
  33. * - page (int): 页码,默认 1
  34. * - page_size (int): 每页数量,默认 20,最大 100
  35. * - search_mode (string): fuzzy|exact|semantic|hybrid,默认 fuzzy
  36. *
  37. * @return \Illuminate\Http\JsonResponse
  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->input('category'); // 分类
  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. * Store a newly created resource in storage.
  99. *
  100. * POST 方式调用搜索接口(与 index 方法功能相同)
  101. *
  102. * @route POST /api/search
  103. * @param JSON: 搜索参数(与 index 方法参数相同)
  104. * @param \Illuminate\Http\Request $request
  105. * @return \Illuminate\Http\JsonResponse
  106. */
  107. public function store(Request $request)
  108. {
  109. // 直接调用 index 方法,复用搜索逻辑
  110. return $this->index($request);
  111. }
  112. /**
  113. * Display the specified resource.
  114. *
  115. * @param int $id
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function show($id)
  119. {
  120. //
  121. return $this->ok(HitItemDTO::fromArray($this->searchService->get($id)));
  122. }
  123. /**
  124. * 更新资源
  125. * @route PUT /api/search/{uid}
  126. * @param JSON: OpenSearch 格式数据
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function update(Request $request, $uid) {}
  130. /**
  131. * Remove the specified resource from storage.
  132. *
  133. * 删除资源
  134. * @route DELETE /api/search/{uid}
  135. * @return \Illuminate\Http\Response
  136. */
  137. public function destroy($uid) {}
  138. }