SearchPlusController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\OpenSearchService;
  4. use Illuminate\Http\Request;
  5. class SearchPlusController extends Controller
  6. {
  7. protected $searchService;
  8. /**
  9. * 构造函数,注入 OpenSearchService
  10. *
  11. * @param \App\Services\OpenSearchService $searchService
  12. */
  13. public function __construct(OpenSearchService $searchService)
  14. {
  15. $this->searchService = $searchService;
  16. }
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * 处理搜索请求,支持 fuzzy / exact / semantic / hybrid 四种模式。
  21. * 接收查询参数并调用 OpenSearchService 执行搜索。
  22. *
  23. * 支持 GET 和 POST 请求
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * - q (string): 搜索关键词
  27. * - resource_type (string): 资源类型 (article|term|dictionary|translation|origin_text|nissaya)
  28. * - granularity (string): 文档颗粒度 (book|chapter|sutta|section|paragraph|sentence)
  29. * - language (string): 语言,如 pali, zh-Hans, zh-Hant, en-US, my
  30. * - category (string): 文档分类 (pali|commentary|subcommentary)
  31. * - tags (array): 标签过滤
  32. * - page_refs (array): 页码标记 ["V3.81","M3.58"]
  33. * - related_id (array): 关联 ID,如 ["chapter_93-5","m.n. 38"]
  34. * - author (string): 作者或译者 (metadata.author)
  35. * - channel (string): 来源渠道 (metadata.channel)
  36. * - page (int): 页码,默认 1
  37. * - page_size (int): 每页数量,默认 20,最大 100
  38. * - search_mode (string): fuzzy|exact|semantic|hybrid,默认 fuzzy
  39. *
  40. * @return \Illuminate\Http\JsonResponse
  41. */
  42. public function index(Request $request)
  43. {
  44. // 获取所有输入参数(自动兼容 GET 和 POST)
  45. $input = $request->all();
  46. // 基础参数 - 使用 $input 或直接用 $request->input() (已兼容 GET/POST)
  47. $query = $request->input('q', '');
  48. $page = max(1, (int) $request->input('page', 1));
  49. $pageSize = min(100, (int) $request->input('page_size', 20));
  50. $searchMode = $request->input('search_mode', 'fuzzy');
  51. $resourceType = $request->input('resource_type'); // 资源类型
  52. $resourceId = $request->input('resource_id'); // 资源类型
  53. $granularity = $request->input('granularity'); // 文档颗粒度
  54. $language = $request->input('language'); // 语言
  55. $category = $request->input('category'); // 分类
  56. $tags = $request->input('tags', []); // 标签
  57. $pageRefs = $request->input('page_refs', []); // 页码标记
  58. $relatedId = $request->input('related_id'); // 关联 ID
  59. $author = $request->input('author'); // 作者/译者 (metadata.author)
  60. $channel = $request->input('channel'); // 来源渠道 (metadata.channel)
  61. // 确保数组类型参数正确解析(POST 时可能是 JSON 字符串)
  62. $tags = is_array($tags) ? $tags : (is_string($tags) ? json_decode($tags, true) ?? [] : []);
  63. $pageRefs = is_array($pageRefs) ? $pageRefs : (is_string($pageRefs) ? json_decode($pageRefs, true) ?? [] : []);
  64. // 组装搜索参数
  65. $params = [
  66. 'query' => $query,
  67. 'page' => $page,
  68. 'pageSize' => $pageSize,
  69. 'searchMode' => $searchMode,
  70. 'resourceType' => $resourceType,
  71. 'resourceId' => $resourceId,
  72. 'granularity' => $granularity,
  73. 'language' => $language,
  74. 'category' => $category,
  75. 'tags' => $tags,
  76. 'pageRefs' => $pageRefs,
  77. 'relatedId' => $relatedId,
  78. 'author' => $author,
  79. 'channel' => $channel,
  80. ];
  81. try {
  82. // 调用 OpenSearchService 执行搜索
  83. $result = $this->searchService->search($params);
  84. return response()->json([
  85. 'success' => true,
  86. 'data' => $result,
  87. 'query_info' => [
  88. 'original_query' => $query,
  89. 'search_mode' => $searchMode,
  90. 'request_method' => $request->method(), // 可选:返回请求方法
  91. ],
  92. ]);
  93. } catch (\Exception $e) {
  94. return response()->json([
  95. 'success' => false,
  96. 'error' => $e->getMessage(),
  97. ], 500);
  98. }
  99. }
  100. /**
  101. * Store a newly created resource in storage.
  102. *
  103. * POST 方式调用搜索接口(与 index 方法功能相同)
  104. *
  105. * @route POST /api/search
  106. * @param JSON: 搜索参数(与 index 方法参数相同)
  107. * @param \Illuminate\Http\Request $request
  108. * @return \Illuminate\Http\JsonResponse
  109. */
  110. public function store(Request $request)
  111. {
  112. // 直接调用 index 方法,复用搜索逻辑
  113. return $this->index($request);
  114. }
  115. /**
  116. * Display the specified resource.
  117. *
  118. * @param int $id
  119. * @return \Illuminate\Http\Response
  120. */
  121. public function show($id)
  122. {
  123. //
  124. }
  125. /**
  126. * 更新资源
  127. * @route PUT /api/search/{uid}
  128. * @param JSON: OpenSearch 格式数据
  129. * @return \Illuminate\Http\Response
  130. */
  131. public function update(Request $request, $uid) {}
  132. /**
  133. * Remove the specified resource from storage.
  134. *
  135. * 删除资源
  136. * @route DELETE /api/search/{uid}
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function destroy($uid) {}
  140. }