SearchService.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Exceptions\BusinessException;
  4. use App\Http\Requests\V3\IndexSearchRequest;
  5. use App\Services\OpenSearchService;
  6. use Closure;
  7. use OpenSearch\Exception\NotFoundHttpException;
  8. use OpenSearch\Exception\RequestTimeoutHttpException;
  9. use OpenSearch\Exception\ServiceUnavailableHttpException;
  10. use OpenSearch\Exception\TooManyRequestsHttpException;
  11. use Psr\Http\Client\ClientExceptionInterface;
  12. /**
  13. * v3 全文检索:把 OpenSearch 的原始命中归一化成对外的扁平结构。
  14. *
  15. * 查询 DSL 仍由共享的 OpenSearchService 构造(Library 检索页与 AITermService 也在用),
  16. * 这里只负责入参映射、异常分类和输出形状——不暴露 `_source` / `hits.hits` 这类 OpenSearch 内部结构。
  17. *
  18. * 异常分两类:
  19. * - 搜索服务不可用(连不上、超时、集群过载)→ 503 `search-unavailable`,文案送达客户端,原异常写日志;
  20. * - 其余(索引映射不对、DSL 写错)是真正的 bug → 原样抛出,兜底处理器回 500 并记日志。
  21. */
  22. class SearchService
  23. {
  24. /** 整章正文文档的 id:tipitaka_chapter_{book}-{para}_{channel uuid},见 IndexTipitaka */
  25. private const CHAPTER_DOC = '/^tipitaka_chapter_\d+-\d+_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i';
  26. public function __construct(
  27. private readonly OpenSearchService $search,
  28. private readonly ChannelAbilityService $abilities,
  29. ) {}
  30. /**
  31. * @param array<string, mixed> $input IndexSearchRequest::validated()
  32. * @return array{
  33. * items: list<array<string, mixed>>,
  34. * meta: array{current_page: int, per_page: int, total: int, total_relation: string, last_page: int, facets?: array<string, list<array{key: string, count: int}>>}
  35. * }
  36. */
  37. public function search(array $input): array
  38. {
  39. $page = (int) ($input['page'] ?? 1);
  40. $perPage = (int) ($input['per_page'] ?? 20);
  41. $withFacets = in_array($input['facets'] ?? 'false', ['true', '1'], true);
  42. $raw = $this->call(fn () => $this->search->search([
  43. 'query' => $input['q'],
  44. 'searchMode' => $input['search_mode'] ?? 'fuzzy',
  45. 'page' => $page,
  46. 'pageSize' => $perPage,
  47. 'resourceType' => $input['resource_type'] ?? null,
  48. 'resourceId' => $input['resource_id'] ?? null,
  49. 'granularity' => $input['granularity'] ?? null,
  50. 'language' => $input['language'] ?? null,
  51. 'category' => self::splitList($input['category'] ?? null) ?: null,
  52. 'tags' => self::splitList($input['tags'] ?? null),
  53. 'pageRefs' => array_values($input['page_refs'] ?? []),
  54. 'relatedId' => array_values($input['related_id'] ?? []) ?: null,
  55. 'author' => $input['author'] ?? null,
  56. 'channel' => $input['channel'] ?? null,
  57. 'aggregations' => $withFacets,
  58. ]));
  59. $total = (int) ($raw['hits']['total']['value'] ?? 0);
  60. $meta = [
  61. 'current_page' => $page,
  62. 'per_page' => $perPage,
  63. 'total' => $total,
  64. // OpenSearch 默认只精确计数到 10000,gte 表示「至少这么多」
  65. 'total_relation' => (string) ($raw['hits']['total']['relation'] ?? 'eq'),
  66. 'last_page' => max(1, min(
  67. (int) ceil($total / $perPage),
  68. intdiv(IndexSearchRequest::MAX_WINDOW, $perPage),
  69. )),
  70. ];
  71. if ($withFacets) {
  72. $meta['facets'] = self::facets($raw['aggregations'] ?? []);
  73. }
  74. return [
  75. 'items' => array_map(self::hit(...), $raw['hits']['hits'] ?? []),
  76. 'meta' => $meta,
  77. ];
  78. }
  79. /**
  80. * 按文档 id 取一条(带阅读用的 display HTML)。文档不存在 → 404。
  81. *
  82. * 整章正文文档按它所属 channel 判权限:索引里收了所有 channel,不在这里挡的话
  83. * 拼出 id 就能读到私有正文。私有 channel 看不见 → 403。
  84. *
  85. * @return array<string, mixed>
  86. */
  87. public function find(string $id, ?string $viewerUid): array
  88. {
  89. if (preg_match(self::CHAPTER_DOC, $id, $m)) {
  90. $this->abilities->assertReadable($m[1], $viewerUid);
  91. }
  92. try {
  93. $raw = $this->call(fn () => $this->search->get($id));
  94. } catch (NotFoundHttpException) {
  95. abort(404, __('site.not_found'));
  96. }
  97. return self::hit($raw);
  98. }
  99. /**
  100. * 调 OpenSearch,把「服务不可用」类异常转成 503。
  101. *
  102. * @template T
  103. *
  104. * @param Closure(): T $callback
  105. * @return T
  106. */
  107. private function call(Closure $callback): mixed
  108. {
  109. try {
  110. return $callback();
  111. } catch (ClientExceptionInterface|ServiceUnavailableHttpException|RequestTimeoutHttpException|TooManyRequestsHttpException $e) {
  112. report($e);
  113. throw new BusinessException(__('site.search_unavailable'), 503, 'search-unavailable');
  114. }
  115. }
  116. /**
  117. * 一条 OpenSearch 命中(search 的 hit 或 get 的文档)→ 对外结构。
  118. *
  119. * 一条文档通常只有一种语言有值,title / content 把 text.* 下的非空值拼起来即可。
  120. * 高亮优先取正文片段,没有再取标题。
  121. *
  122. * @param array<string, mixed> $hit
  123. * @return array<string, mixed>
  124. */
  125. private static function hit(array $hit): array
  126. {
  127. $source = $hit['_source'] ?? [];
  128. $highlight = $hit['highlight'] ?? [];
  129. $fragment = null;
  130. foreach (['content.text.pali', 'content.text.zh', 'summary.text', 'title.text.pali', 'title.text.zh'] as $field) {
  131. if (! empty($highlight[$field][0])) {
  132. $fragment = (string) $highlight[$field][0];
  133. break;
  134. }
  135. }
  136. $bold = $source['bold_single'] ?? [];
  137. $bold = is_array($bold)
  138. ? array_values(array_filter(array_map('strval', $bold), fn (string $v) => trim($v) !== ''))
  139. : preg_split('/\s+/u', trim((string) $bold), -1, PREG_SPLIT_NO_EMPTY);
  140. $item = [
  141. 'id' => (string) ($source['id'] ?? $hit['_id'] ?? ''),
  142. 'resource_type' => (string) ($source['resource_type'] ?? ''),
  143. 'resource_id' => isset($source['resource_id']) ? (string) $source['resource_id'] : null,
  144. 'granularity' => isset($source['granularity']) ? (string) $source['granularity'] : null,
  145. 'language' => isset($source['language']) ? (string) $source['language'] : null,
  146. 'related_id' => isset($source['related_id']) ? (string) $source['related_id'] : null,
  147. 'score' => isset($hit['_score']) ? (float) $hit['_score'] : null,
  148. 'title' => self::joinText($source['title']['text'] ?? []),
  149. 'content' => self::joinText($source['content']['text'] ?? []),
  150. 'highlight' => $fragment,
  151. 'bold' => $bold,
  152. 'path' => isset($source['path']) && $source['path'] !== '' ? (string) $source['path'] : null,
  153. 'category' => array_values((array) ($source['category'] ?? [])),
  154. 'tags' => array_values((array) ($source['tags'] ?? [])),
  155. 'updated_at' => isset($source['updated_at']) ? (string) $source['updated_at'] : null,
  156. ];
  157. if (isset($source['content']['display'])) {
  158. $item['display'] = (string) $source['content']['display'];
  159. }
  160. return $item;
  161. }
  162. /**
  163. * @param mixed $text title.text / content.text(按语言分的对象)
  164. */
  165. private static function joinText(mixed $text): string
  166. {
  167. return implode('', array_filter(
  168. is_array($text) ? array_values($text) : [],
  169. fn ($v) => is_string($v) && $v !== '',
  170. ));
  171. }
  172. /**
  173. * @param array<string, mixed> $aggregations
  174. * @return array<string, list<array{key: string, count: int}>>
  175. */
  176. private static function facets(array $aggregations): array
  177. {
  178. $facets = [];
  179. foreach ($aggregations as $name => $agg) {
  180. $facets[$name] = array_map(fn (array $bucket) => [
  181. 'key' => (string) $bucket['key'],
  182. 'count' => (int) $bucket['doc_count'],
  183. ], $agg['buckets'] ?? []);
  184. }
  185. return $facets;
  186. }
  187. /**
  188. * 逗号分隔的列表(category / tags),去空白、去空项。
  189. *
  190. * @return list<string>
  191. */
  192. private static function splitList(?string $value): array
  193. {
  194. if ($value === null) {
  195. return [];
  196. }
  197. return array_values(array_filter(array_map('trim', explode(',', $value)), fn (string $v) => $v !== ''));
  198. }
  199. }