arocapi 5.0.0-beta.5 → 5.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/dist/app.d.ts +19 -13
  4. package/dist/app.js +13 -10
  5. package/dist/app.js.map +1 -1
  6. package/dist/routes/capabilities.d.ts +2 -2
  7. package/dist/routes/capabilities.js +2 -2
  8. package/dist/routes/capabilities.js.map +1 -1
  9. package/dist/routes/entities.js +8 -9
  10. package/dist/routes/entities.js.map +1 -1
  11. package/dist/routes/search.d.ts +2 -4
  12. package/dist/routes/search.js +75 -97
  13. package/dist/routes/search.js.map +1 -1
  14. package/dist/search/advanced.d.ts +8 -0
  15. package/dist/search/advanced.js +198 -0
  16. package/dist/search/advanced.js.map +1 -0
  17. package/dist/search/backend.d.ts +95 -0
  18. package/dist/search/backend.js +5 -0
  19. package/dist/search/backend.js.map +1 -0
  20. package/dist/search/fields.d.ts +63 -0
  21. package/dist/search/fields.js +203 -0
  22. package/dist/search/fields.js.map +1 -0
  23. package/dist/search/opensearch/advanced.d.ts +4 -0
  24. package/dist/search/opensearch/advanced.js +28 -0
  25. package/dist/search/opensearch/advanced.js.map +1 -0
  26. package/dist/search/opensearch/opensearch.d.ts +16 -0
  27. package/dist/search/opensearch/opensearch.js +187 -0
  28. package/dist/search/opensearch/opensearch.js.map +1 -0
  29. package/dist/search/opensearch/visibility.d.ts +3 -0
  30. package/dist/search/opensearch/visibility.js +27 -0
  31. package/dist/search/opensearch/visibility.js.map +1 -0
  32. package/dist/search/postgres/advanced.d.ts +7 -0
  33. package/dist/search/postgres/advanced.js +54 -0
  34. package/dist/search/postgres/advanced.js.map +1 -0
  35. package/dist/search/postgres/facets.d.ts +2 -0
  36. package/dist/search/postgres/facets.js +27 -0
  37. package/dist/search/postgres/facets.js.map +1 -0
  38. package/dist/search/postgres/geo.d.ts +6 -0
  39. package/dist/search/postgres/geo.js +42 -0
  40. package/dist/search/postgres/geo.js.map +1 -0
  41. package/dist/search/postgres/postgres.d.ts +19 -0
  42. package/dist/search/postgres/postgres.js +157 -0
  43. package/dist/search/postgres/postgres.js.map +1 -0
  44. package/dist/search/postgres/values.d.ts +8 -0
  45. package/dist/search/postgres/values.js +20 -0
  46. package/dist/search/postgres/values.js.map +1 -0
  47. package/dist/search/postgres/visibility.d.ts +4 -0
  48. package/dist/search/postgres/visibility.js +38 -0
  49. package/dist/search/postgres/visibility.js.map +1 -0
  50. package/dist/search/requestFilters.d.ts +39 -0
  51. package/dist/search/requestFilters.js +125 -0
  52. package/dist/search/requestFilters.js.map +1 -0
  53. package/dist/search/visibility.d.ts +51 -0
  54. package/dist/search/visibility.js +100 -0
  55. package/dist/search/visibility.js.map +1 -0
  56. package/dist/types/visibility.d.ts +3 -2
  57. package/dist/utils/visibility.js +1 -1
  58. package/dist/utils/visibility.js.map +1 -1
  59. package/package.json +46 -26
  60. package/prisma/models/entity.prisma +9 -8
  61. package/prisma/models/entitySearch.prisma +21 -0
  62. package/prisma/models/file.prisma +3 -3
  63. package/src/app.ts +53 -24
  64. package/src/routes/capabilities.ts +4 -4
  65. package/src/routes/entities.ts +10 -9
  66. package/src/routes/search.ts +104 -118
  67. package/src/search/advanced.ts +258 -0
  68. package/src/search/backend.ts +91 -0
  69. package/src/search/fields.ts +320 -0
  70. package/src/search/opensearch/advanced.ts +36 -0
  71. package/src/search/opensearch/opensearch.ts +290 -0
  72. package/src/search/opensearch/visibility.ts +38 -0
  73. package/src/search/postgres/advanced.ts +76 -0
  74. package/src/search/postgres/facets.ts +34 -0
  75. package/src/search/postgres/geo.ts +58 -0
  76. package/src/search/postgres/postgres.ts +232 -0
  77. package/src/search/postgres/values.ts +33 -0
  78. package/src/search/postgres/visibility.ts +54 -0
  79. package/src/search/requestFilters.ts +178 -0
  80. package/src/search/visibility.ts +193 -0
  81. package/src/types/visibility.ts +6 -3
  82. package/src/utils/visibility.ts +1 -1
  83. package/dist/app.test.d.ts +0 -1
  84. package/dist/app.test.js +0 -67
  85. package/dist/app.test.js.map +0 -1
  86. package/dist/errorHandling.test.d.ts +0 -1
  87. package/dist/errorHandling.test.js +0 -222
  88. package/dist/errorHandling.test.js.map +0 -1
  89. package/dist/index.dev.d.ts +0 -1
  90. package/dist/index.dev.js +0 -157
  91. package/dist/index.dev.js.map +0 -1
  92. package/dist/routes/capabilities.test.d.ts +0 -1
  93. package/dist/routes/capabilities.test.js +0 -81
  94. package/dist/routes/capabilities.test.js.map +0 -1
  95. package/dist/routes/crate.test.d.ts +0 -1
  96. package/dist/routes/crate.test.js +0 -390
  97. package/dist/routes/crate.test.js.map +0 -1
  98. package/dist/routes/entities.test.d.ts +0 -1
  99. package/dist/routes/entities.test.js +0 -277
  100. package/dist/routes/entities.test.js.map +0 -1
  101. package/dist/routes/entity.test.d.ts +0 -1
  102. package/dist/routes/entity.test.js +0 -136
  103. package/dist/routes/entity.test.js.map +0 -1
  104. package/dist/routes/file.test.d.ts +0 -1
  105. package/dist/routes/file.test.js +0 -435
  106. package/dist/routes/file.test.js.map +0 -1
  107. package/dist/routes/files.test.d.ts +0 -1
  108. package/dist/routes/files.test.js +0 -245
  109. package/dist/routes/files.test.js.map +0 -1
  110. package/dist/routes/search.test.d.ts +0 -1
  111. package/dist/routes/search.test.js +0 -833
  112. package/dist/routes/search.test.js.map +0 -1
  113. package/dist/test/helpers/fastify.d.ts +0 -9
  114. package/dist/test/helpers/fastify.js +0 -17
  115. package/dist/test/helpers/fastify.js.map +0 -1
  116. package/dist/test/integration.setup.d.ts +0 -47
  117. package/dist/test/integration.setup.js +0 -195
  118. package/dist/test/integration.setup.js.map +0 -1
  119. package/dist/test/integration.test.d.ts +0 -1
  120. package/dist/test/integration.test.js +0 -762
  121. package/dist/test/integration.test.js.map +0 -1
  122. package/dist/transformers/default.test.d.ts +0 -1
  123. package/dist/transformers/default.test.js +0 -225
  124. package/dist/transformers/default.test.js.map +0 -1
  125. package/dist/transformers/transformer.test.d.ts +0 -1
  126. package/dist/transformers/transformer.test.js +0 -165
  127. package/dist/transformers/transformer.test.js.map +0 -1
  128. package/dist/utils/errors.test.d.ts +0 -1
  129. package/dist/utils/errors.test.js +0 -79
  130. package/dist/utils/errors.test.js.map +0 -1
  131. package/dist/utils/queryBuilder.d.ts +0 -47
  132. package/dist/utils/queryBuilder.js +0 -107
  133. package/dist/utils/queryBuilder.js.map +0 -1
  134. package/prisma.config.ts +0 -9
  135. package/src/app.test.ts +0 -91
  136. package/src/errorHandling.test.ts +0 -288
  137. package/src/index.dev.ts +0 -202
  138. package/src/reset.d.ts +0 -1
  139. package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
  140. package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
  141. package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
  142. package/src/routes/__snapshots__/search.test.ts.snap +0 -165
  143. package/src/routes/capabilities.test.ts +0 -102
  144. package/src/routes/crate.test.ts +0 -484
  145. package/src/routes/entities.test.ts +0 -324
  146. package/src/routes/entity.test.ts +0 -166
  147. package/src/routes/file.test.ts +0 -538
  148. package/src/routes/files.test.ts +0 -304
  149. package/src/routes/search.test.ts +0 -959
  150. package/src/test/__snapshots__/integration.test.ts.snap +0 -18
  151. package/src/test/helpers/fastify.ts +0 -26
  152. package/src/test/integration.setup.ts +0 -220
  153. package/src/test/integration.test.ts +0 -953
  154. package/src/transformers/default.test.ts +0 -276
  155. package/src/transformers/transformer.test.ts +0 -226
  156. package/src/utils/errors.test.ts +0 -90
  157. package/src/utils/queryBuilder.ts +0 -157
package/src/app.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import sensible from '@fastify/sensible';
2
- import type { Client } from '@opensearch-project/opensearch';
3
2
  import type { FastifyPluginAsync } from 'fastify';
4
3
  import type { PrismaClient } from './generated/prisma/client.js';
5
4
  import capabilities from './routes/capabilities.js';
@@ -9,16 +8,39 @@ import file from './routes/file.js';
9
8
  import files from './routes/files.js';
10
9
  import metadata from './routes/metadata.js';
11
10
  import search from './routes/search.js';
11
+ import type { SearchBackend } from './search/backend.js';
12
+ import type { SearchVisibilityWhere } from './search/visibility.js';
12
13
  import type { AccessFunction } from './types/access.js';
13
- import type { IsAuthenticated } from './types/context.js';
14
+ import type { IsAuthenticated, RequestContext } from './types/context.js';
14
15
  import type { FileHandler, RoCrateHandler } from './types/fileHandlers.js';
15
16
  import type { EntityTransformer, FileTransformer } from './types/transformers.js';
16
- import type { EntityVisibilityFilter, SearchVisibilityFilter } from './types/visibility.js';
17
+ import type { EntityVisibilityFilter } from './types/visibility.js';
17
18
  import { notFoundHandler } from './utils/errors.js';
18
- import type { FacetCapability, FilterCapability, QueryBuilderOptions, SearchCapability } from './utils/queryBuilder.js';
19
- import { OpensearchQueryBuilder } from './utils/queryBuilder.js';
20
19
  import { setupValidation } from './utils/validation.js';
21
20
 
21
+ export type {
22
+ AdvancedQuery,
23
+ BackendSearchCapability,
24
+ BoundingBox,
25
+ FacetBucket,
26
+ RangeBound,
27
+ RangeFilter,
28
+ SearchBackend,
29
+ SearchExtra,
30
+ SearchRequest,
31
+ SearchResult,
32
+ } from './search/backend.js';
33
+ export { SearchQueryError } from './search/backend.js';
34
+ export type {
35
+ FieldDeclaration,
36
+ FieldDeclarations,
37
+ FieldType,
38
+ GeoPoint,
39
+ SearchDocument,
40
+ } from './search/fields.js';
41
+ export { capabilitiesFromFields } from './search/fields.js';
42
+ export type { SearchVisibilityWhere, VisibilityCondition } from './search/visibility.js';
43
+ export { checkSearchVisibility } from './search/visibility.js';
22
44
  export type {
23
45
  AuthorisedEntity,
24
46
  AuthorisedFile,
@@ -58,8 +80,6 @@ export {
58
80
  notFoundHandler,
59
81
  } from './utils/errors.js';
60
82
  export { AllVisibleEntityFilter, AllVisibleSearchFilter } from './utils/visibility.js';
61
- export type { FacetCapability, FilterCapability, QueryBuilderOptions, SearchCapability };
62
- export { OpensearchQueryBuilder };
63
83
 
64
84
  const DOCS_URL = 'https://arocapi.crate-works.org';
65
85
 
@@ -72,27 +92,32 @@ const REMOVED_OPTIONS: Record<string, { replacement: string; docs: string }> = {
72
92
  },
73
93
  };
74
94
 
95
+ type PrismaMethod = (args: never) => PromiseLike<unknown>;
96
+ type PrismaDelegate = Record<'findMany' | 'findFirst' | 'count', PrismaMethod>;
97
+
98
+ /**
99
+ * The Prisma client arocapi needs: any Prisma 7 client generated from a schema that includes the arocapi models,
100
+ * including one with extra models, a `$extends` client or the client inside `$transaction`
101
+ */
102
+ export type ArocapiPrismaClient = { entity: PrismaDelegate; file: PrismaDelegate };
103
+
75
104
  export type Options = {
76
- prisma: PrismaClient;
77
- opensearch: Client;
78
- queryBuilderClass?: typeof OpensearchQueryBuilder;
79
- queryBuilderOptions?: QueryBuilderOptions;
105
+ prisma: ArocapiPrismaClient;
106
+ search: SearchBackend;
80
107
  access: AccessFunction;
81
108
  entityTransformers?: EntityTransformer[];
82
109
  fileTransformers?: FileTransformer[];
83
110
  fileHandler: FileHandler;
84
111
  roCrateHandler: RoCrateHandler;
85
112
  entityVisibilityFilter: EntityVisibilityFilter;
86
- searchVisibilityFilter: SearchVisibilityFilter;
113
+ /** A `SearchVisibilityFilter<typeof fields>` for the search backend's declared fields */
114
+ searchVisibilityFilter: (context: RequestContext) => Promise<SearchVisibilityWhere> | SearchVisibilityWhere;
87
115
  isAuthenticated?: IsAuthenticated;
88
116
  promptLoginForHidden?: boolean;
89
117
  };
90
118
  const app: FastifyPluginAsync<Options> = async (fastify, options) => {
91
119
  const {
92
- prisma,
93
- opensearch,
94
- queryBuilderClass,
95
- queryBuilderOptions,
120
+ search: searchBackend,
96
121
  access,
97
122
  entityTransformers,
98
123
  fileTransformers,
@@ -104,12 +129,19 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
104
129
  promptLoginForHidden = false,
105
130
  } = options;
106
131
 
107
- if (!prisma) {
132
+ if (!options.prisma) {
108
133
  throw new Error('Prisma client is required');
109
134
  }
110
135
 
111
- if (!opensearch) {
112
- throw new Error('OpenSearch client is required');
136
+ if (typeof options.prisma.entity?.findMany !== 'function' || typeof options.prisma.file?.findMany !== 'function') {
137
+ throw new Error('prisma must be a Prisma 7 client generated from a schema that includes the arocapi models');
138
+ }
139
+
140
+ // Consumers generate their own client, whose delegate generics never match ours; the routes need the full type
141
+ const prisma = options.prisma as PrismaClient;
142
+
143
+ if (!searchBackend) {
144
+ throw new Error('search is required');
113
145
  }
114
146
 
115
147
  for (const [removed, { replacement, docs }] of Object.entries(REMOVED_OPTIONS)) {
@@ -153,9 +185,7 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
153
185
  fastify.setNotFoundHandler(notFoundHandler);
154
186
  }
155
187
 
156
- const queryBuilder = new (queryBuilderClass ?? OpensearchQueryBuilder)(queryBuilderOptions);
157
-
158
- fastify.register(capabilities, { queryBuilder });
188
+ fastify.register(capabilities, { search: searchBackend });
159
189
  fastify.register(entities, { prisma, access, entityTransformers, entityVisibilityFilter });
160
190
  fastify.register(entity, { prisma, access, entityTransformers, entityVisibilityFilter, refusal });
161
191
  fastify.register(files, { prisma, access, fileTransformers, entityVisibilityFilter });
@@ -163,10 +193,9 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
163
193
  fastify.register(metadata, { prisma, roCrateHandler, access, entityVisibilityFilter, refusal });
164
194
  fastify.register(search, {
165
195
  prisma,
166
- opensearch,
196
+ search: searchBackend,
167
197
  access,
168
198
  entityTransformers,
169
- queryBuilder,
170
199
  entityVisibilityFilter,
171
200
  searchVisibilityFilter,
172
201
  });
@@ -1,10 +1,10 @@
1
1
  import type { FastifyPluginAsync } from 'fastify';
2
- import type { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
2
+ import type { SearchBackend } from '../search/backend.js';
3
3
 
4
- const SPEC_VERSION = '0.4.0';
4
+ const SPEC_VERSION = '0.5.0';
5
5
 
6
6
  type CapabilitiesRouteOptions = {
7
- queryBuilder: OpensearchQueryBuilder;
7
+ search: SearchBackend;
8
8
  };
9
9
 
10
10
  const capabilities: FastifyPluginAsync<CapabilitiesRouteOptions> = async (fastify, opts) => {
@@ -13,7 +13,7 @@ const capabilities: FastifyPluginAsync<CapabilitiesRouteOptions> = async (fastif
13
13
  deposit: { supported: false },
14
14
  tombstonePolicy: '404',
15
15
  extensions: {},
16
- search: opts.queryBuilder.searchCapabilities(),
16
+ search: opts.search.capabilities,
17
17
  };
18
18
 
19
19
  fastify.get('/capabilities', async () => document);
@@ -10,14 +10,15 @@ import type { EntityVisibilityFilter } from '../types/visibility.js';
10
10
  const querySchema = z.object({
11
11
  memberOf: z.url().optional(),
12
12
  entityType: z
13
- .string()
14
- .optional()
15
- .transform((val) => {
16
- if (!val) return undefined;
17
-
18
- return val.split(',').map((item) => item.trim());
19
- })
20
- .pipe(z.array(z.string()).optional()),
13
+ .union([z.string(), z.array(z.string())])
14
+ .default([])
15
+ .transform((val) =>
16
+ [val]
17
+ .flat()
18
+ .map((value) => value.trim())
19
+ .filter(Boolean),
20
+ )
21
+ .pipe(z.array(z.url())),
21
22
  limit: z.coerce.number().int().min(1).max(1000).default(100),
22
23
  offset: z.coerce.number().int().min(0).default(0),
23
24
  sort: z.enum(['id', 'name', 'createdAt', 'updatedAt']).default('id'),
@@ -49,7 +50,7 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
49
50
  where.memberOf = memberOf;
50
51
  }
51
52
 
52
- if (entityType && entityType.length > 0) {
53
+ if (entityType.length > 0) {
53
54
  where.entityType = {
54
55
  in: entityType,
55
56
  };
@@ -1,60 +1,86 @@
1
- import type { Client } from '@opensearch-project/opensearch';
2
- import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
3
- import type { Search_Request, Search_Response } from '@opensearch-project/opensearch/api/index.js';
4
1
  import type { FastifyPluginAsync } from 'fastify';
5
2
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
6
3
  import { z } from 'zod';
7
4
  import type { PrismaClient } from '../generated/prisma/client.js';
5
+ import { normaliseQuery } from '../search/advanced.js';
6
+ import {
7
+ type SearchBackend,
8
+ type SearchExtra,
9
+ SearchQueryError,
10
+ type SearchRequest,
11
+ type SearchResult,
12
+ } from '../search/backend.js';
13
+ import {
14
+ boundingBoxSchema,
15
+ filterValueSchema,
16
+ geoViolations,
17
+ splitFilters,
18
+ type Violation,
19
+ } from '../search/requestFilters.js';
8
20
  import { applyTransformers, authoriseEntities } from '../transformers/default.js';
9
- import type { AccessFunction } from '../types/access.js';
21
+ import type { AccessFunction, AccessInfo } from '../types/access.js';
10
22
  import type { EntityTransformer } from '../types/transformers.js';
11
23
  import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
12
- import { createError, ERROR_CODES } from '../utils/errors.js';
13
- import type { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
14
-
15
- const boundingBoxSchema = z.object({
16
- topRight: z.object({
17
- lat: z.number(),
18
- lng: z.number(),
19
- }),
20
- bottomLeft: z.object({
21
- lat: z.number(),
22
- lng: z.number(),
23
- }),
24
- });
24
+ import { createError, createValidationError, ERROR_CODES } from '../utils/errors.js';
25
25
 
26
26
  const searchParamsSchema = z.object({
27
27
  searchType: z.enum(['basic', 'advanced']).default('basic'),
28
28
  query: z.string(),
29
- filters: z.record(z.string(), z.array(z.string())).optional(),
29
+ filters: z.record(z.string(), filterValueSchema).default({}),
30
30
  boundingBox: boundingBoxSchema.optional(),
31
31
  geohashPrecision: z.number().int().min(1).max(12).optional(),
32
32
  limit: z.number().int().min(1).max(1000).default(100),
33
33
  offset: z.number().int().min(0).default(0),
34
- sort: z.enum(['id', 'name', 'createdAt', 'updatedAt', 'relevance']).default('relevance'),
34
+ sort: z.string().default('relevance'),
35
35
  order: z.enum(['asc', 'desc']).default('asc'),
36
36
  });
37
37
 
38
+ type SearchParams = z.infer<typeof searchParamsSchema>;
39
+
38
40
  type SearchRouteOptions = {
39
41
  prisma: PrismaClient;
40
- opensearch: Client;
42
+ search: SearchBackend;
41
43
  access: AccessFunction;
42
44
  entityTransformers?: EntityTransformer[];
43
- queryBuilder: OpensearchQueryBuilder;
44
45
  entityVisibilityFilter: EntityVisibilityFilter;
45
46
  searchVisibilityFilter: SearchVisibilityFilter;
46
47
  };
47
48
 
49
+ // Highlights quote the Record's text, so a hit denied either part keeps only its score
50
+ const visibleExtra = (searchExtra: SearchExtra | undefined, { metadata, content }: AccessInfo) => {
51
+ if (!searchExtra || (metadata && content)) {
52
+ return searchExtra;
53
+ }
54
+
55
+ const { highlight: _, ...rest } = searchExtra;
56
+
57
+ return Object.keys(rest).length > 0 ? rest : undefined;
58
+ };
59
+
48
60
  const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
49
61
  const {
50
62
  prisma,
51
- opensearch,
63
+ search: backend,
52
64
  access,
53
65
  entityTransformers = [],
54
- queryBuilder,
55
66
  entityVisibilityFilter,
56
67
  searchVisibilityFilter,
57
68
  } = opts;
69
+ const { capabilities } = backend;
70
+ const noFacets = Object.fromEntries(Object.keys(capabilities.facets).map((name) => [name, []]));
71
+
72
+ const violations = ({ searchType, sort }: SearchParams) => {
73
+ const found: Violation[] = [];
74
+ if (!Object.hasOwn(capabilities.searchTypes, searchType)) {
75
+ found.push({ field: '/searchType', message: `${searchType} search isn't supported`, value: searchType });
76
+ }
77
+ if (sort !== 'relevance' && !Object.hasOwn(capabilities.sorts, sort)) {
78
+ found.push({ field: '/sort', message: `${sort} isn't a declared sort`, value: sort });
79
+ }
80
+
81
+ return found;
82
+ };
83
+
58
84
  fastify.withTypeProvider<ZodTypeProvider>().post(
59
85
  '/search',
60
86
  {
@@ -63,60 +89,62 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
63
89
  },
64
90
  },
65
91
  async (request, reply) => {
66
- const { searchType, query, filters, boundingBox, geohashPrecision, limit, offset, sort, order } = request.body;
92
+ const { filters, violations: filterProblems } = splitFilters(capabilities.filters, request.body.filters);
93
+ const problems = [
94
+ ...violations(request.body),
95
+ ...filterProblems,
96
+ ...geoViolations(capabilities.geo, request.body),
97
+ ];
98
+ if (problems.length > 0) {
99
+ return reply.code(400).send(createValidationError('The request parameters are invalid', problems));
100
+ }
101
+
102
+ const { searchType, query, filters: _, boundingBox, geohashPrecision, ...paging } = request.body;
103
+
104
+ const searchQuery = normaliseQuery(searchType, query, capabilities.searchTypes.advanced?.fields ?? {});
105
+ if ('problem' in searchQuery) {
106
+ const { problem } = searchQuery;
107
+
108
+ return reply.code(422).send(createValidationError(problem, [{ field: 'query', message: problem }]));
109
+ }
67
110
 
68
111
  const context = { request, fastify };
69
- const [visibilityWhere, visibilityQuery] = await Promise.all([
70
- entityVisibilityFilter(context),
71
- searchVisibilityFilter(context),
72
- ]);
73
-
74
- const opensearchQuery: Search_Request = {
75
- index: 'entities',
76
- body: {
77
- query: {
78
- bool: {
79
- must: [queryBuilder.buildQuery(searchType, query, filters, boundingBox)],
80
- filter: [visibilityQuery],
81
- },
82
- },
83
- aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
84
- highlight: {
85
- fields: {
86
- name: {},
87
- description: {},
88
- },
89
- },
90
- sort: queryBuilder.buildSort(sort, order),
91
- from: offset,
92
- size: limit,
93
- },
112
+ // Only the database query needs it, so the search needn't wait; awaited before any reply so its error wins
113
+ const visibilityWhere = Promise.resolve(entityVisibilityFilter(context));
114
+ visibilityWhere.catch(() => undefined);
115
+ const visibility = await searchVisibilityFilter(context);
116
+
117
+ const searchRequest: SearchRequest = {
118
+ ...searchQuery,
119
+ filters,
120
+ ...(boundingBox && { boundingBox }),
121
+ ...(geohashPrecision !== undefined && { geohashPrecision }),
122
+ ...paging,
123
+ visibility,
94
124
  };
95
- fastify.log.debug(opensearchQuery);
96
- let response: Search_Response;
125
+
126
+ const start = performance.now();
127
+ let result: SearchResult;
97
128
  try {
98
- response = await opensearch.search(opensearchQuery);
129
+ result = await backend.search(searchRequest, context);
99
130
  } catch (error) {
100
- const { statusCode, message } = error as Error & { statusCode?: number };
101
- // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
102
- if (statusCode !== 400) {
131
+ await visibilityWhere.catch((visibilityError: unknown) => {
132
+ fastify.log.error({ err: error }, 'Search backend failed');
133
+ throw visibilityError;
134
+ });
135
+ if (!(error instanceof SearchQueryError)) {
103
136
  throw error;
104
137
  }
105
138
 
106
- fastify.log.warn(`Invalid search query: ${message}`);
107
-
108
- return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, message));
109
- }
139
+ fastify.log.warn(`Invalid search query: ${error.message}`);
110
140
 
111
- if (!response.body?.hits?.hits) {
112
- throw new Error('Invalid search response: missing hits data');
141
+ return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, error.message));
113
142
  }
114
-
115
- const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
143
+ const searchTime = Math.round(performance.now() - start);
116
144
 
117
145
  const dbEntities = await prisma.entity.findMany({
118
146
  where: {
119
- AND: [{ id: { in: entityIds } }, visibilityWhere],
147
+ AND: [{ id: { in: result.hits.map(({ id }) => id) } }, await visibilityWhere],
120
148
  },
121
149
  include: { file: { select: { id: true } } },
122
150
  });
@@ -125,70 +153,28 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
125
153
  const entityMap = new Map(authorisedEntities.map((entity) => [entity.id, entity]));
126
154
 
127
155
  const entities = await Promise.all(
128
- response.body.hits.hits.map(async (hit) => {
129
- if (!hit._source?.id) {
130
- throw new Error('Missing id in search hit');
131
- }
132
-
133
- const authorisedEntity = entityMap.get(hit._source.id);
156
+ result.hits.map(async ({ id, searchExtra }) => {
157
+ const authorisedEntity = entityMap.get(id);
134
158
  if (!authorisedEntity) {
135
- fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but missing or not visible in database`);
159
+ fastify.log.warn(`Entity ${id} found by the search backend but missing or not visible in database`);
136
160
 
137
161
  return null;
138
162
  }
139
163
 
140
- const result = await applyTransformers(authorisedEntity, entityTransformers, context);
164
+ const transformed = await applyTransformers(authorisedEntity, entityTransformers, context);
165
+ const extra = visibleExtra(searchExtra, authorisedEntity.access);
141
166
 
142
- // Add search-specific metadata
143
- return {
144
- ...(result as Record<string, unknown>),
145
- searchExtra: {
146
- score: hit._score,
147
- highlight: hit.highlight,
148
- },
149
- };
167
+ return extra === undefined ? transformed : { ...(transformed as object), searchExtra: extra };
150
168
  }),
151
- ).then((results) => results.filter(Boolean));
152
-
153
- const facets: Record<string, Array<{ name: string; count: number }>> = {};
154
- if (response.body.aggregations) {
155
- Object.keys(response.body.aggregations).forEach((key) => {
156
- if (key !== 'geohash_grid') {
157
- const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
158
- if (agg?.buckets && Array.isArray(agg.buckets)) {
159
- facets[key] = agg.buckets.map((bucket) => ({
160
- name: bucket.key,
161
- count: bucket.doc_count,
162
- }));
163
- }
164
- }
165
- });
166
- }
169
+ ).then((results) => results.filter((entity) => entity !== null));
167
170
 
168
- let geohashGrid: Record<string, number> | undefined;
169
- if (response.body.aggregations?.geohash_grid) {
170
- const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
171
- if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
172
- geohashAgg.buckets.forEach((bucket) => {
173
- geohashGrid ||= {};
174
- geohashGrid[bucket.key] = bucket.doc_count;
175
- });
176
- }
177
- }
178
-
179
- /* v8 ignore next 2 -- Not sure how to force opensearch to hit the other path -- @preserve */
180
- const total =
181
- typeof response.body.hits.total === 'number' ? response.body.hits.total : response.body.hits.total?.value || 0;
182
-
183
- const result = {
184
- total,
185
- searchTime: response.body.took,
171
+ return {
172
+ total: result.total,
173
+ searchTime,
186
174
  entities,
187
- facets: Object.keys(facets).length > 0 ? facets : undefined,
188
- geohashGrid,
175
+ facets: { ...noFacets, ...result.facets },
176
+ ...(result.geohashGrid && { geohashGrid: result.geohashGrid }),
189
177
  };
190
-
191
- return result;
192
178
  },
193
179
  );
194
180
  };