SearchService.php 11 KB

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