SearchPlusController.php 4.8 KB

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