SearchSuggestController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // api-v8/app/Http/Controllers/SearchSuggestController.php
  3. namespace App\Http\Controllers;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6. use App\Services\OpenSearchService;
  7. /**
  8. * 搜索自动建议控制器
  9. *
  10. * 返回示例:
  11. *
  12. * 请求:GET /api/v3/search-suggest?q=dhamma&fields=title,content&limit=10
  13. *
  14. * 返回:
  15. * {
  16. * "success": true,
  17. * "data": {
  18. * "query": "dhamma",
  19. * "suggestions": [
  20. {
  21. "text": "dhammacakkapavattanasutta",
  22. "source": "content",
  23. "score": 1,
  24. "resource_type": "term",
  25. "language": "zh",
  26. "doc_id": "term_69258244-bccd-40ed-bfaa-ddef4ae5ae4c",
  27. "category": [],
  28. "granularity": null
  29. },
  30. {
  31. "text": "dhammacakkappavattanasutta",
  32. "source": "content",
  33. "score": 1,
  34. "resource_type": "term",
  35. "language": "zh-hans",
  36. "doc_id": "term_bcb14399-ea80-4a8a-aeab-a4c927e45fdd",
  37. "category": [],
  38. "granularity": null
  39. }
  40. * ],
  41. * "total": 2
  42. * }
  43. * }
  44. */
  45. class SearchSuggestController extends Controller
  46. {
  47. protected $searchService;
  48. /**
  49. * 构造函数,注入 OpenSearchService
  50. *
  51. * @param \App\Services\OpenSearchService $searchService
  52. */
  53. public function __construct(OpenSearchService $searchService)
  54. {
  55. $this->searchService = $searchService;
  56. }
  57. /**
  58. * # 1. 查询所有字段(默认)
  59. GET /api/v2/suggest?q=dhamma&limit=10
  60. # 2. 只查询标题
  61. GET /api/v2/suggest?q=dhamma&fields=title&limit=10
  62. # 3. 查询标题和内容
  63. GET /api/v2/suggest?q=dhamma&fields=title,content&limit=10
  64. # 4. 查询页面引用,带语言过滤
  65. GET /api/v2/suggest?q=M.1&fields=page_refs&language=pali&limit=5
  66. # 5. 数组形式传递多个字段
  67. GET /api/v2/suggest?q=dhamma&fields[]=title&fields[]=content&limit=10
  68. */
  69. /**
  70. * 自动建议接口
  71. *
  72. * 基于 OpenSearch completion suggester,支持从不同字段获取建议。
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * - q (string): 输入的部分文本(必填)
  76. * - fields (string|array): 要查询的字段,可选值:
  77. * - 不传:查询所有字段 (title, content, page_refs)
  78. * - 单个字段:'title' | 'content' | 'page_refs'
  79. * - 多个字段:'title,content' 或 ['title', 'content']
  80. * - language (string): 语言过滤,可选(如:pali, zh, en)
  81. * - limit (int): 每个字段返回的建议数量,默认 10,最大 50
  82. *
  83. * @return \Illuminate\Http\JsonResponse
  84. */
  85. public function index(Request $request)
  86. {
  87. // 验证必填参数
  88. $query = $request->input('q', '');
  89. if (empty($query)) {
  90. return response()->json([
  91. 'success' => false,
  92. 'error' => '缺少参数 q(查询文本)'
  93. ], 400);
  94. }
  95. // 解析 fields 参数
  96. $fields = $this->parseFields($request->input('fields'));
  97. // 获取其他参数
  98. $language = $request->input('language', null);
  99. $limit = min(50, max(1, (int) $request->input('limit', 10)));
  100. try {
  101. // 调用搜索服务
  102. $rawSuggestions = $this->searchService->suggest(
  103. $query,
  104. $fields,
  105. $language,
  106. $limit
  107. );
  108. // 格式化返回结果
  109. $suggestions = $this->formatSuggestions($rawSuggestions);
  110. return response()->json([
  111. 'success' => true,
  112. 'data' => [
  113. 'query' => $query,
  114. 'suggestions' => $suggestions,
  115. 'total' => count($suggestions)
  116. ]
  117. ]);
  118. } catch (\InvalidArgumentException $e) {
  119. return response()->json([
  120. 'success' => false,
  121. 'error' => '无效的字段参数:' . $e->getMessage(),
  122. 'hint' => '有效的字段值:title, content, page_refs'
  123. ], 400);
  124. } catch (\Exception $e) {
  125. return response()->json([
  126. 'success' => false,
  127. 'error' => '搜索建议失败:' . $e->getMessage(),
  128. ], 500);
  129. }
  130. }
  131. /**
  132. * 解析 fields 参数
  133. *
  134. * @param mixed $fields
  135. * @return string|array|null
  136. */
  137. protected function parseFields($fields)
  138. {
  139. if ($fields === null) {
  140. return null; // 查询所有字段
  141. }
  142. if (is_string($fields)) {
  143. // 如果是逗号分隔的字符串,转换为数组
  144. if (strpos($fields, ',') !== false) {
  145. $fieldsArray = array_map('trim', explode(',', $fields));
  146. return $fieldsArray;
  147. }
  148. // 单个字段
  149. return $fields;
  150. }
  151. if (is_array($fields)) {
  152. return $fields;
  153. }
  154. return null;
  155. }
  156. /**
  157. * 格式化建议结果
  158. *
  159. * @param array $rawSuggestions
  160. * @return array
  161. */
  162. protected function formatSuggestions(array $rawSuggestions): array
  163. {
  164. return collect($rawSuggestions)->map(function ($item) {
  165. $docSource = $item['doc_source'] ?? [];
  166. return [
  167. 'text' => $item['text'] ?? '',
  168. 'source' => $item['source'] ?? null,
  169. 'score' => round($item['score'] ?? 0, 2),
  170. 'resource_type' => $docSource['resource_type'] ?? null,
  171. 'language' => $docSource['language'] ?? null,
  172. 'doc_id' => $item['doc_id'] ?? null,
  173. // 可选:添加更多元数据
  174. 'category' => $docSource['category'] ?? null,
  175. 'granularity' => $docSource['granularity'] ?? null,
  176. ];
  177. })->all();
  178. }
  179. }