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.
- package/LICENSE.md +1 -1
- package/README.md +2 -2
- package/dist/app.d.ts +19 -13
- package/dist/app.js +13 -10
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +2 -2
- package/dist/routes/capabilities.js +2 -2
- package/dist/routes/capabilities.js.map +1 -1
- package/dist/routes/entities.js +8 -9
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/search.d.ts +2 -4
- package/dist/routes/search.js +75 -97
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/types/visibility.d.ts +3 -2
- package/dist/utils/visibility.js +1 -1
- package/dist/utils/visibility.js.map +1 -1
- package/package.json +46 -26
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +53 -24
- package/src/routes/capabilities.ts +4 -4
- package/src/routes/entities.ts +10 -9
- package/src/routes/search.ts +104 -118
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/types/visibility.ts +6 -3
- package/src/utils/visibility.ts +1 -1
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -67
- package/dist/app.test.js.map +0 -1
- package/dist/errorHandling.test.d.ts +0 -1
- package/dist/errorHandling.test.js +0 -222
- package/dist/errorHandling.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -157
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/capabilities.test.d.ts +0 -1
- package/dist/routes/capabilities.test.js +0 -81
- package/dist/routes/capabilities.test.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -390
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -277
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -136
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -435
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -245
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -833
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -17
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -47
- package/dist/test/integration.setup.js +0 -195
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -762
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -225
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -165
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -79
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -47
- package/dist/utils/queryBuilder.js +0 -107
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -91
- package/src/errorHandling.test.ts +0 -288
- package/src/index.dev.ts +0 -202
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/capabilities.test.ts +0 -102
- package/src/routes/crate.test.ts +0 -484
- package/src/routes/entities.test.ts +0 -324
- package/src/routes/entity.test.ts +0 -166
- package/src/routes/file.test.ts +0 -538
- package/src/routes/files.test.ts +0 -304
- package/src/routes/search.test.ts +0 -959
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -26
- package/src/test/integration.setup.ts +0 -220
- package/src/test/integration.test.ts +0 -953
- package/src/transformers/default.test.ts +0 -276
- package/src/transformers/transformer.test.ts +0 -226
- package/src/utils/errors.test.ts +0 -90
- 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
|
|
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:
|
|
77
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
112
|
-
throw new Error('
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
2
|
+
import type { SearchBackend } from '../search/backend.js';
|
|
3
3
|
|
|
4
|
-
const SPEC_VERSION = '0.
|
|
4
|
+
const SPEC_VERSION = '0.5.0';
|
|
5
5
|
|
|
6
6
|
type CapabilitiesRouteOptions = {
|
|
7
|
-
|
|
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.
|
|
16
|
+
search: opts.search.capabilities,
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
fastify.get('/capabilities', async () => document);
|
package/src/routes/entities.ts
CHANGED
|
@@ -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
|
-
.
|
|
15
|
-
.transform((val) =>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
53
|
+
if (entityType.length > 0) {
|
|
53
54
|
where.entityType = {
|
|
54
55
|
in: entityType,
|
|
55
56
|
};
|
package/src/routes/search.ts
CHANGED
|
@@ -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(),
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
96
|
-
|
|
125
|
+
|
|
126
|
+
const start = performance.now();
|
|
127
|
+
let result: SearchResult;
|
|
97
128
|
try {
|
|
98
|
-
|
|
129
|
+
result = await backend.search(searchRequest, context);
|
|
99
130
|
} catch (error) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
129
|
-
|
|
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 ${
|
|
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
|
|
164
|
+
const transformed = await applyTransformers(authorisedEntity, entityTransformers, context);
|
|
165
|
+
const extra = visibleExtra(searchExtra, authorisedEntity.access);
|
|
141
166
|
|
|
142
|
-
|
|
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(
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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:
|
|
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
|
};
|