SearchSuggestController.php 5.7 KB

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