IndexSearchRequest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Requests\V3;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Validator;
  5. /**
  6. * `GET /v3/search` 与 `POST /v3/search`(两者等价,POST 把同样的参数放进请求体)。
  7. *
  8. * 枚举取的是索引里真实存在的值(IndexTipitaka / TermIndexService 写入的),
  9. * 不是旧文档里那套对不上号的名字。
  10. */
  11. class IndexSearchRequest extends FormRequest
  12. {
  13. public const MODES = ['fuzzy', 'exact', 'semantic', 'hybrid', 'definition'];
  14. public const RESOURCE_TYPES = ['tipitaka', 'original_text', 'term'];
  15. public const GRANULARITIES = ['book', 'chapter', 'session', 'paragraph'];
  16. /** OpenSearch 的 index.max_result_window:from + size 超过它直接报错 */
  17. public const MAX_WINDOW = 10000;
  18. public function authorize(): bool
  19. {
  20. return true;
  21. }
  22. /**
  23. * JSON 请求体里的 true / false 是布尔值,查询串里是字符串,统一成字符串再校验。
  24. */
  25. protected function prepareForValidation(): void
  26. {
  27. if (is_bool($this->input('facets'))) {
  28. $this->merge(['facets' => $this->input('facets') ? 'true' : 'false']);
  29. }
  30. }
  31. /**
  32. * @return array<string, array<int, mixed>>
  33. */
  34. public function rules(): array
  35. {
  36. return [
  37. 'q' => ['required', 'string', 'max:500'],
  38. 'search_mode' => ['sometimes', 'string', 'in:'.implode(',', self::MODES)],
  39. 'resource_type' => ['sometimes', 'string', 'in:'.implode(',', self::RESOURCE_TYPES)],
  40. 'resource_id' => ['sometimes', 'string', 'max:255'],
  41. 'granularity' => ['sometimes', 'string', 'in:'.implode(',', self::GRANULARITIES)],
  42. 'language' => ['sometimes', 'string', 'max:16'],
  43. 'category' => ['sometimes', 'string', 'max:500'],
  44. 'tags' => ['sometimes', 'string', 'max:500'],
  45. 'page_refs' => ['sometimes', 'array', 'max:50'],
  46. 'page_refs.*' => ['string', 'max:64'],
  47. 'related_id' => ['sometimes', 'array', 'max:50'],
  48. 'related_id.*' => ['string', 'max:255'],
  49. 'author' => ['sometimes', 'string', 'max:255'],
  50. 'channel' => ['sometimes', 'uuid'],
  51. 'facets' => ['sometimes', 'in:true,false,1,0'],
  52. 'page' => ['sometimes', 'integer', 'min:1'],
  53. 'per_page' => ['sometimes', 'integer', 'min:1', 'max:100'],
  54. ];
  55. }
  56. /**
  57. * 翻页深度受 OpenSearch 限制:超出时在这里给 422,而不是让 OpenSearch 报错变成 500。
  58. *
  59. * @return array<int, callable>
  60. */
  61. public function after(): array
  62. {
  63. return [
  64. function (Validator $validator): void {
  65. if ($validator->errors()->isNotEmpty()) {
  66. return;
  67. }
  68. $page = (int) $this->input('page', 1);
  69. $perPage = (int) $this->input('per_page', 20);
  70. if ($page * $perPage > self::MAX_WINDOW) {
  71. $validator->errors()->add('page', __('validation.max.numeric', [
  72. 'attribute' => 'page',
  73. 'max' => intdiv(self::MAX_WINDOW, $perPage),
  74. ]));
  75. }
  76. },
  77. ];
  78. }
  79. }