02_search.php 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. use \WikipaliApi\Api\SearchApi;
  3. use \WikipaliApi\Api\SearchSuggestApi;
  4. use \WikipaliApi\Model\PostApiV3SearchRequest;
  5. /** 一条命中必须有的字段(v3 归一化后的形状,不再透出 OpenSearch 的 _source) */
  6. const SEARCH_HIT_KEYS = ['id', 'resource_type', 'resource_id', 'granularity', 'language', 'related_id',
  7. 'score', 'title', 'content', 'highlight', 'bold', 'path', 'category', 'tags', 'updated_at'];
  8. function assert_search_page(array $res, string $what): void
  9. {
  10. assert_has_key('data', $res['body'], $what);
  11. assert_has_key('meta', $res['body'], $what);
  12. foreach (['current_page', 'per_page', 'total', 'total_relation', 'last_page'] as $key) {
  13. assert_has_key($key, $res['body']['meta'], "{$what} meta");
  14. }
  15. if (! is_array($res['body']['data']) || $res['body']['data'] === []) {
  16. fail("{$what}:data 应是非空列表");
  17. }
  18. foreach (SEARCH_HIT_KEYS as $key) {
  19. assert_has_key($key, $res['body']['data'][0], "{$what} data[0]");
  20. }
  21. if (array_key_exists('_source', $res['body']['data'][0])) {
  22. fail("{$what}:不应再透出 _source");
  23. }
  24. }
  25. test('GET /v3/search?q=dukkha → 200 {data, meta}', function () {
  26. $res = request(fn () => api(SearchApi::class)->getApiV3SearchWithHttpInfo(
  27. fixtures()['search_q'], 'fuzzy', 'tipitaka', null, 'paragraph', null, null, null, null, null, null, null, false, 1, 5));
  28. assert_status(200, $res, 'search GET');
  29. assert_search_page($res, 'search GET');
  30. assert_eq(5, $res['body']['meta']['per_page'], 'search GET per_page');
  31. if (array_key_exists('facets', $res['body']['meta'])) {
  32. fail('search GET:没要 facets 就不应返回');
  33. }
  34. });
  35. test('GET /v3/search?facets=true → meta.facets 分面计数', function () {
  36. $res = raw('GET', '/v3/search', ['q' => fixtures()['search_q'], 'per_page' => 1, 'facets' => 'true']);
  37. assert_status(200, $res, 'search facets');
  38. assert_has_key('facets', $res['body']['meta'], 'search facets');
  39. assert_has_key('resource_type', $res['body']['meta']['facets'], 'search facets');
  40. });
  41. test('POST /v3/search → 200(与 GET 等价,参数在 body)', function () {
  42. $body = new PostApiV3SearchRequest();
  43. $body->setQ(fixtures()['search_q']);
  44. $body->setPerPage(3);
  45. $res = request(fn () => api(SearchApi::class)->postApiV3SearchWithHttpInfo($body));
  46. assert_status(200, $res, 'search POST');
  47. assert_search_page($res, 'search POST');
  48. });
  49. test('GET /v3/search(缺 q / 非法模式 / 翻页过深)→ 422', function () {
  50. assert_field_error('q', raw('GET', '/v3/search'), 'search 缺 q');
  51. assert_field_error('search_mode', raw('GET', '/v3/search', ['q' => 'x', 'search_mode' => 'bogus']), 'search 非法模式');
  52. assert_field_error('page', raw('GET', '/v3/search', ['q' => 'x', 'per_page' => 100, 'page' => 101]), 'search 翻页过深');
  53. });
  54. test('GET /v3/search/{id} → 200(取检索结果里的第一条)', function () {
  55. // 文档 id 从检索结果里拿,不依赖各环境的种子数据
  56. $hits = raw('GET', '/v3/search', ['q' => fixtures()['search_q'], 'per_page' => 1]);
  57. assert_status(200, $hits, 'search show 前置检索');
  58. $id = $hits['body']['data'][0]['id'] ?? null;
  59. if (! is_string($id)) {
  60. fail('search show:检索结果为空,拿不到文档 id');
  61. }
  62. $res = request(fn () => api(SearchApi::class)->getApiV3SearchSearchWithHttpInfo($id));
  63. assert_status(200, $res, 'search show');
  64. assert_eq($id, $res['body']['data']['id'] ?? null, 'search show id');
  65. });
  66. test('GET /v3/search/{id} → 404(不存在的文档)', function () {
  67. $res = request(fn () => api(SearchApi::class)->getApiV3SearchSearchWithHttpInfo('no_such_doc_xyz'));
  68. assert_status(404, $res, 'search show 404');
  69. assert_problem($res, 'search show 404');
  70. });
  71. test('GET /v3/search-suggest?q=dukkha → 200', function () {
  72. $res = request(fn () => api(SearchSuggestApi::class)->getApiV3SearchSuggestWithHttpInfo(fixtures()['search_q']));
  73. assert_status(200, $res, 'search-suggest');
  74. assert_has_key('data', $res['body'], 'search-suggest');
  75. });
  76. test('GET /v3/search-suggest?q= → 400(空查询)', function () {
  77. // 描述里写明「q 为空返回 400」;spec 只登记了 422,这里按实际 400 断言。
  78. $res = raw('GET', '/v3/search-suggest', ['q' => '']);
  79. assert_status(400, $res, 'search-suggest 空 q');
  80. });