|
|
@@ -2,7 +2,6 @@
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
-use App\Services\ResourceService;
|
|
|
use App\Services\OpenSearchService;
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
@@ -19,23 +18,26 @@ class SearchPlusController extends Controller
|
|
|
{
|
|
|
$this->searchService = $searchService;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* Display a listing of the resource.
|
|
|
*
|
|
|
* 处理搜索请求,支持 fuzzy / exact / semantic / hybrid 四种模式。
|
|
|
* 接收查询参数并调用 OpenSearchService 执行搜索。
|
|
|
*
|
|
|
+ * 支持 GET 和 POST 请求
|
|
|
+ *
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
* - q (string): 搜索关键词
|
|
|
- * - resource_type (string): 资源类型 (article|term|dictionary|translation|origin_text|nissaya) ✅ 已更新
|
|
|
- * - granularity (string): 文档颗粒度 (book|chapter|sutta|section|paragraph|sentence) ✅ 已更新
|
|
|
- * - language (string): 语言,如 pali, zh-Hans, zh-Hant, en-US, my ✅ 已更新
|
|
|
- * - category (string): 文档分类 (pali|commentary|subcommentary) ✅ 已更新
|
|
|
+ * - resource_type (string): 资源类型 (article|term|dictionary|translation|origin_text|nissaya)
|
|
|
+ * - granularity (string): 文档颗粒度 (book|chapter|sutta|section|paragraph|sentence)
|
|
|
+ * - language (string): 语言,如 pali, zh-Hans, zh-Hant, en-US, my
|
|
|
+ * - category (string): 文档分类 (pali|commentary|subcommentary)
|
|
|
* - tags (array): 标签过滤
|
|
|
- * - page_refs (array): 页码标记 ["V3.81","M3.58"] ✅ 已更新
|
|
|
- * - related_id (array): 关联 ID,如 ["chapter_93-5","m.n. 38"] ✅ 新增
|
|
|
- * - author (string): 作者或译者 (metadata.author) ✅ 新增
|
|
|
- * - channel (string): 来源渠道 (metadata.channel) ✅ 新增
|
|
|
+ * - page_refs (array): 页码标记 ["V3.81","M3.58"]
|
|
|
+ * - related_id (array): 关联 ID,如 ["chapter_93-5","m.n. 38"]
|
|
|
+ * - author (string): 作者或译者 (metadata.author)
|
|
|
+ * - channel (string): 来源渠道 (metadata.channel)
|
|
|
* - page (int): 页码,默认 1
|
|
|
* - page_size (int): 每页数量,默认 20,最大 100
|
|
|
* - search_mode (string): fuzzy|exact|semantic|hybrid,默认 fuzzy
|
|
|
@@ -44,21 +46,29 @@ class SearchPlusController extends Controller
|
|
|
*/
|
|
|
public function index(Request $request)
|
|
|
{
|
|
|
- // 基础参数
|
|
|
+ // 获取所有输入参数(自动兼容 GET 和 POST)
|
|
|
+ $input = $request->all();
|
|
|
+
|
|
|
+ // 基础参数 - 使用 $input 或直接用 $request->input() (已兼容 GET/POST)
|
|
|
$query = $request->input('q', '');
|
|
|
$page = max(1, (int) $request->input('page', 1));
|
|
|
$pageSize = min(100, (int) $request->input('page_size', 20));
|
|
|
$searchMode = $request->input('search_mode', 'fuzzy');
|
|
|
$resourceType = $request->input('resource_type'); // 资源类型
|
|
|
+ $resourceId = $request->input('resource_id'); // 资源类型
|
|
|
$granularity = $request->input('granularity'); // 文档颗粒度
|
|
|
$language = $request->input('language'); // 语言
|
|
|
$category = $request->input('category'); // 分类
|
|
|
$tags = $request->input('tags', []); // 标签
|
|
|
$pageRefs = $request->input('page_refs', []); // 页码标记
|
|
|
- $relatedId = $request->input('related_id', []); // 关联 ID
|
|
|
+ $relatedId = $request->input('related_id'); // 关联 ID
|
|
|
$author = $request->input('author'); // 作者/译者 (metadata.author)
|
|
|
$channel = $request->input('channel'); // 来源渠道 (metadata.channel)
|
|
|
|
|
|
+ // 确保数组类型参数正确解析(POST 时可能是 JSON 字符串)
|
|
|
+ $tags = is_array($tags) ? $tags : (is_string($tags) ? json_decode($tags, true) ?? [] : []);
|
|
|
+ $pageRefs = is_array($pageRefs) ? $pageRefs : (is_string($pageRefs) ? json_decode($pageRefs, true) ?? [] : []);
|
|
|
+
|
|
|
// 组装搜索参数
|
|
|
$params = [
|
|
|
'query' => $query,
|
|
|
@@ -66,6 +76,7 @@ class SearchPlusController extends Controller
|
|
|
'pageSize' => $pageSize,
|
|
|
'searchMode' => $searchMode,
|
|
|
'resourceType' => $resourceType,
|
|
|
+ 'resourceId' => $resourceId,
|
|
|
'granularity' => $granularity,
|
|
|
'language' => $language,
|
|
|
'category' => $category,
|
|
|
@@ -86,6 +97,7 @@ class SearchPlusController extends Controller
|
|
|
'query_info' => [
|
|
|
'original_query' => $query,
|
|
|
'search_mode' => $searchMode,
|
|
|
+ 'request_method' => $request->method(), // 可选:返回请求方法
|
|
|
],
|
|
|
]);
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -98,13 +110,19 @@ class SearchPlusController extends Controller
|
|
|
|
|
|
/**
|
|
|
* Store a newly created resource in storage.
|
|
|
- * * 添加资源
|
|
|
+ *
|
|
|
+ * POST 方式调用搜索接口(与 index 方法功能相同)
|
|
|
+ *
|
|
|
* @route POST /api/search
|
|
|
- * @param JSON: OpenSearch 格式数据 (e.g., {type, title, content, path_full, ...})
|
|
|
+ * @param JSON: 搜索参数(与 index 方法参数相同)
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
- * @return \Illuminate\Http\Response
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
*/
|
|
|
- public function store(Request $request) {}
|
|
|
+ public function store(Request $request)
|
|
|
+ {
|
|
|
+ // 直接调用 index 方法,复用搜索逻辑
|
|
|
+ return $this->index($request);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Display the specified resource.
|