arocapi 5.0.0-beta.1 → 5.0.0-beta.10
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 +37 -802
- package/dist/app.d.ts +38 -20
- package/dist/app.js +62 -40
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +37 -57
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +13 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +37 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +11 -5
- package/dist/routes/metadata.js +24 -68
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +90 -119
- 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/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +82 -40
- 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 +168 -95
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +48 -67
- package/src/routes/entity.ts +21 -35
- package/src/routes/file.ts +53 -103
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +42 -98
- package/src/routes/search.ts +131 -151
- 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/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -82
- package/dist/app.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- 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 -273
- 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 -130
- 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 -315
- 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 -222
- 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 -780
- 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 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -187
- 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 -396
- 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 -163
- 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 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -112
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- 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/crate.test.ts +0 -423
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -209
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { type AdvancedQuery, type SearchQuery, SearchQueryError } from './backend.js';
|
|
2
|
+
import { dateSpan } from './fields.js';
|
|
3
|
+
|
|
4
|
+
/** In code points, for basic and advanced queries */
|
|
5
|
+
const MAX_QUERY_LENGTH = 1024;
|
|
6
|
+
const MAX_DEPTH = 10;
|
|
7
|
+
/** Leaf clauses, counted after field groups are rewritten */
|
|
8
|
+
const MAX_CLAUSES = 64;
|
|
9
|
+
|
|
10
|
+
class InvalidQueryError extends Error {
|
|
11
|
+
override name = 'InvalidQueryError';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Token = {
|
|
15
|
+
kind: '(' | ')' | ':' | 'phrase' | 'word' | 'AND' | 'OR' | 'NOT' | 'end';
|
|
16
|
+
text: string;
|
|
17
|
+
/** 1-based, in code points */
|
|
18
|
+
position: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const OPERATORS: readonly string[] = ['AND', 'OR', 'NOT'];
|
|
22
|
+
const PUNCTUATION: readonly string[] = ['(', ')', ':'];
|
|
23
|
+
|
|
24
|
+
const isSpace = (char: string) => /\s/u.test(char);
|
|
25
|
+
const endsWord = (char: string) => isSpace(char) || PUNCTUATION.includes(char) || char === '"';
|
|
26
|
+
|
|
27
|
+
const tokenise = (chars: string[]) => {
|
|
28
|
+
const tokens: Token[] = [];
|
|
29
|
+
let at = 0;
|
|
30
|
+
|
|
31
|
+
while (at < chars.length) {
|
|
32
|
+
const char = chars[at];
|
|
33
|
+
const position = at + 1;
|
|
34
|
+
|
|
35
|
+
if (isSpace(char)) {
|
|
36
|
+
at++;
|
|
37
|
+
} else if (PUNCTUATION.includes(char)) {
|
|
38
|
+
tokens.push({ kind: char as Token['kind'], text: char, position });
|
|
39
|
+
at++;
|
|
40
|
+
} else if (char === '"') {
|
|
41
|
+
const close = chars.indexOf('"', at + 1);
|
|
42
|
+
if (close === -1) {
|
|
43
|
+
throw new InvalidQueryError(
|
|
44
|
+
`Expected a closing " for the phrase at position ${position}, found the end of the query`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
tokens.push({ kind: 'phrase', text: chars.slice(at + 1, close).join(''), position });
|
|
48
|
+
at = close + 1;
|
|
49
|
+
} else {
|
|
50
|
+
const start = at;
|
|
51
|
+
while (at < chars.length && !endsWord(chars[at])) {
|
|
52
|
+
at++;
|
|
53
|
+
}
|
|
54
|
+
const text = chars.slice(start, at).join('');
|
|
55
|
+
tokens.push({ kind: OPERATORS.includes(text) ? (text as Token['kind']) : 'word', text, position });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
tokens.push({ kind: 'end', text: '', position: chars.length + 1 });
|
|
60
|
+
|
|
61
|
+
return tokens;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const describe = ({ kind, text }: Token) => {
|
|
65
|
+
switch (kind) {
|
|
66
|
+
case 'end':
|
|
67
|
+
return 'the end of the query';
|
|
68
|
+
case 'phrase':
|
|
69
|
+
return `the phrase "${text}"`;
|
|
70
|
+
case 'word':
|
|
71
|
+
return `"${text}"`;
|
|
72
|
+
default:
|
|
73
|
+
return kind.length === 1 ? `"${kind}"` : kind;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const combine = (type: 'and' | 'or', parts: AdvancedQuery[]): AdvancedQuery =>
|
|
78
|
+
parts.length === 1
|
|
79
|
+
? parts[0]
|
|
80
|
+
: { type, clauses: parts.flatMap((part) => (part.type === type ? part.clauses : [part])) };
|
|
81
|
+
|
|
82
|
+
const parse = (query: string, fields: Record<string, unknown>): AdvancedQuery => {
|
|
83
|
+
const tokens = tokenise([...query]);
|
|
84
|
+
let at = 0;
|
|
85
|
+
|
|
86
|
+
const peek = (offset = 0) => tokens[at + offset];
|
|
87
|
+
const fail = (expected: string): never => {
|
|
88
|
+
throw new InvalidQueryError(`Expected ${expected} at position ${peek().position}, found ${describe(peek())}`);
|
|
89
|
+
};
|
|
90
|
+
const expect = (kind: Token['kind'], expected: string) => {
|
|
91
|
+
if (peek().kind !== kind) {
|
|
92
|
+
fail(expected);
|
|
93
|
+
}
|
|
94
|
+
at++;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// `field` is set inside a field group, where clauses are values rather than field matches
|
|
98
|
+
const parseOr = (field?: string): AdvancedQuery => {
|
|
99
|
+
const parts = [parseAnd(field)];
|
|
100
|
+
while (peek().kind === 'OR') {
|
|
101
|
+
at++;
|
|
102
|
+
parts.push(parseAnd(field));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return combine('or', parts);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const parseAnd = (field?: string): AdvancedQuery => {
|
|
109
|
+
const parts = [parseUnary(field)];
|
|
110
|
+
while (peek().kind === 'AND') {
|
|
111
|
+
at++;
|
|
112
|
+
parts.push(parseUnary(field));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return combine('and', parts);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const parseUnary = (field?: string): AdvancedQuery => {
|
|
119
|
+
if (peek().kind === 'NOT') {
|
|
120
|
+
at++;
|
|
121
|
+
|
|
122
|
+
return { type: 'not', clause: parseUnary(field) };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return parsePrimary(field);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const parseBracketed = (field?: string) => {
|
|
129
|
+
at++;
|
|
130
|
+
const clause = parseOr(field);
|
|
131
|
+
expect(')', 'AND, OR or ")"');
|
|
132
|
+
|
|
133
|
+
return clause;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const parsePrimary = (field?: string): AdvancedQuery => {
|
|
137
|
+
if (peek().kind === '(') {
|
|
138
|
+
return parseBracketed(field);
|
|
139
|
+
}
|
|
140
|
+
if (field !== undefined) {
|
|
141
|
+
return parseValue(field);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const name = peek();
|
|
145
|
+
if (name.kind !== 'word') {
|
|
146
|
+
fail('a field name');
|
|
147
|
+
}
|
|
148
|
+
at++;
|
|
149
|
+
expect(':', `":" after the field name "${name.text}"`);
|
|
150
|
+
if (!Object.hasOwn(fields, name.text)) {
|
|
151
|
+
throw new InvalidQueryError(`"${name.text}" at position ${name.position} isn't a declared advanced field`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return peek().kind === '(' ? parseBracketed(name.text) : parseValue(name.text);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const startsValueWord = () => peek().kind === 'word' && peek(1).kind !== ':';
|
|
158
|
+
|
|
159
|
+
const parseValue = (field: string): AdvancedQuery => {
|
|
160
|
+
if (peek().kind === 'phrase') {
|
|
161
|
+
const { text } = peek();
|
|
162
|
+
at++;
|
|
163
|
+
|
|
164
|
+
return { type: 'match', field, value: text, phrase: true };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!startsValueWord()) {
|
|
168
|
+
fail(`a value for "${field}"`);
|
|
169
|
+
}
|
|
170
|
+
const words: string[] = [];
|
|
171
|
+
while (startsValueWord()) {
|
|
172
|
+
words.push(peek().text);
|
|
173
|
+
at++;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return { type: 'match', field, value: words.join(' '), phrase: false };
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const tree = parseOr();
|
|
180
|
+
expect('end', 'AND, OR or the end of the query');
|
|
181
|
+
|
|
182
|
+
return tree;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const depthOf = (query: AdvancedQuery): number => {
|
|
186
|
+
switch (query.type) {
|
|
187
|
+
case 'and':
|
|
188
|
+
case 'or':
|
|
189
|
+
return 1 + Math.max(...query.clauses.map(depthOf));
|
|
190
|
+
case 'not':
|
|
191
|
+
return 1 + depthOf(query.clause);
|
|
192
|
+
default:
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const clausesIn = (query: AdvancedQuery): number => {
|
|
198
|
+
switch (query.type) {
|
|
199
|
+
case 'and':
|
|
200
|
+
case 'or':
|
|
201
|
+
return query.clauses.reduce((sum, clause) => sum + clausesIn(clause), 0);
|
|
202
|
+
case 'not':
|
|
203
|
+
return clausesIn(query.clause);
|
|
204
|
+
default:
|
|
205
|
+
return 1;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const parseWithinCaps = (query: string, advancedFields: Record<string, unknown>): AdvancedQuery => {
|
|
210
|
+
const tree = parse(query, advancedFields);
|
|
211
|
+
if (depthOf(tree) > MAX_DEPTH) {
|
|
212
|
+
throw new InvalidQueryError(`The query nests deeper than the limit of ${MAX_DEPTH} levels`);
|
|
213
|
+
}
|
|
214
|
+
if (clausesIn(tree) > MAX_CLAUSES) {
|
|
215
|
+
throw new InvalidQueryError(`The query has more than the limit of ${MAX_CLAUSES} clauses`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return tree;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Check a query against the caps and parse an advanced one into a tree over `advancedFields`
|
|
223
|
+
* A query arocapi refuses gives the problem instead
|
|
224
|
+
*/
|
|
225
|
+
export const normaliseQuery = (
|
|
226
|
+
searchType: 'basic' | 'advanced',
|
|
227
|
+
query: string,
|
|
228
|
+
advancedFields: Record<string, unknown>,
|
|
229
|
+
): SearchQuery | { problem: string } => {
|
|
230
|
+
if ([...query].length > MAX_QUERY_LENGTH) {
|
|
231
|
+
return { problem: `The query is longer than the limit of ${MAX_QUERY_LENGTH} characters` };
|
|
232
|
+
}
|
|
233
|
+
if (searchType === 'basic') {
|
|
234
|
+
return { searchType, query };
|
|
235
|
+
}
|
|
236
|
+
if (query.trim() === '') {
|
|
237
|
+
return { searchType, query: { type: 'all' } };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
return { searchType, query: parseWithinCaps(query, advancedFields) };
|
|
242
|
+
} catch (error) {
|
|
243
|
+
// Only InvalidQueryError is thrown: the parser is pure and its recursion is bounded by the length cap
|
|
244
|
+
return { problem: (error as InvalidQueryError).message };
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The UTC instants an advanced match on a date field covers, throwing a SearchQueryError for a value that isn't a date
|
|
250
|
+
*/
|
|
251
|
+
export const matchedDateSpan = (field: string, value: string) => {
|
|
252
|
+
const span = dateSpan(value);
|
|
253
|
+
if (!span) {
|
|
254
|
+
throw new SearchQueryError(`"${field}" is a date field: "${value}" isn't a date`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return span;
|
|
258
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { RequestContext } from '../types/context.js';
|
|
2
|
+
import type { GeoPoint, SearchDocument } from './fields.js';
|
|
3
|
+
import type { SearchVisibilityWhere } from './visibility.js';
|
|
4
|
+
|
|
5
|
+
type Labelled = { label?: string };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What a search backend publishes in `/capabilities` and arocapi validates requests against
|
|
9
|
+
*/
|
|
10
|
+
export type BackendSearchCapability = {
|
|
11
|
+
filters: Record<string, Labelled & { type: 'string' | 'date' | 'number' | 'boolean' }>;
|
|
12
|
+
facets: Record<string, Labelled>;
|
|
13
|
+
sorts: Record<string, Labelled>;
|
|
14
|
+
searchTypes: { basic: Record<string, never>; advanced?: { fields: Record<string, Labelled> } };
|
|
15
|
+
geo: { supported: boolean };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A parsed advanced query
|
|
20
|
+
*/
|
|
21
|
+
export type AdvancedQuery =
|
|
22
|
+
| { type: 'and' | 'or'; clauses: AdvancedQuery[] }
|
|
23
|
+
| { type: 'not'; clause: AdvancedQuery }
|
|
24
|
+
| { type: 'match'; field: string; value: string; phrase: boolean }
|
|
25
|
+
| { type: 'all' };
|
|
26
|
+
|
|
27
|
+
/** Date bounds are UTC instants */
|
|
28
|
+
export type RangeBound = number | Date;
|
|
29
|
+
|
|
30
|
+
export type RangeFilter = { gte?: RangeBound; lte?: RangeBound };
|
|
31
|
+
|
|
32
|
+
export type BoundingBox = { topRight: GeoPoint; bottomLeft: GeoPoint };
|
|
33
|
+
|
|
34
|
+
export type SearchQuery = { searchType: 'basic'; query: string } | { searchType: 'advanced'; query: AdvancedQuery };
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A validated, normalised search, as arocapi hands it to a search backend
|
|
38
|
+
*/
|
|
39
|
+
export type SearchRequest = SearchQuery & {
|
|
40
|
+
filters: {
|
|
41
|
+
/** Field name → values, any of which matches */
|
|
42
|
+
terms: Record<string, string[]>;
|
|
43
|
+
/** Field name → ranges, any of which matches; date and number filters always arrive here, exact values as ranges */
|
|
44
|
+
ranges: Record<string, RangeFilter[]>;
|
|
45
|
+
};
|
|
46
|
+
boundingBox?: BoundingBox;
|
|
47
|
+
geohashPrecision?: number;
|
|
48
|
+
limit: number;
|
|
49
|
+
offset: number;
|
|
50
|
+
/** A declared sort, or `relevance`: best match first, stable for an empty query */
|
|
51
|
+
sort: string;
|
|
52
|
+
order: 'asc' | 'desc';
|
|
53
|
+
/** The resolved `searchVisibilityFilter`, which the backend checks and applies to hits, total and facets */
|
|
54
|
+
visibility: SearchVisibilityWhere;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type SearchExtra = { score?: number; highlight?: Record<string, string[]> };
|
|
58
|
+
|
|
59
|
+
export type FacetBucket = { name: string; count: number };
|
|
60
|
+
|
|
61
|
+
/** The most geohash cells a reference backend returns: those with the most hits */
|
|
62
|
+
export const GEOHASH_GRID_CELLS = 10_000;
|
|
63
|
+
|
|
64
|
+
export type SearchResult = {
|
|
65
|
+
/** Exact */
|
|
66
|
+
total: number;
|
|
67
|
+
/** Ordered */
|
|
68
|
+
hits: { id: string; searchExtra?: SearchExtra }[];
|
|
69
|
+
facets?: Record<string, FacetBucket[]>;
|
|
70
|
+
geohashGrid?: Record<string, number>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A search backend: arocapi validates each request against `capabilities`, the backend only searches
|
|
75
|
+
*/
|
|
76
|
+
export interface SearchBackend {
|
|
77
|
+
/** What `/capabilities` publishes and arocapi validates requests against */
|
|
78
|
+
readonly capabilities: BackendSearchCapability;
|
|
79
|
+
search(request: SearchRequest, context: RequestContext): Promise<SearchResult>;
|
|
80
|
+
/** Upsert search documents; each entity must exist first */
|
|
81
|
+
index(docs: SearchDocument[], options?: { tx?: unknown }): Promise<void>;
|
|
82
|
+
/** Remove search documents, ignoring ids that aren't indexed */
|
|
83
|
+
remove(ids: string[]): Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Thrown by a search backend for a query it can't run; arocapi answers 422 with its message
|
|
88
|
+
*/
|
|
89
|
+
export class SearchQueryError extends Error {
|
|
90
|
+
override name = 'SearchQueryError';
|
|
91
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import type { BackendSearchCapability } from './backend.js';
|
|
2
|
+
|
|
3
|
+
export type FieldType = 'text' | 'keyword' | 'date' | 'number' | 'boolean' | 'geo';
|
|
4
|
+
|
|
5
|
+
export type FieldDeclaration = {
|
|
6
|
+
type: FieldType;
|
|
7
|
+
label?: string;
|
|
8
|
+
/** Text fields only: 1–4, default 1 */
|
|
9
|
+
weight?: number;
|
|
10
|
+
/** Default true for text fields */
|
|
11
|
+
basic?: boolean;
|
|
12
|
+
advanced?: boolean;
|
|
13
|
+
filter?: boolean;
|
|
14
|
+
/** Implies `filter`. Default size 20 */
|
|
15
|
+
facet?: boolean | { size: number };
|
|
16
|
+
sort?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type FieldDeclarations = Record<string, FieldDeclaration>;
|
|
20
|
+
|
|
21
|
+
const FIELD_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
22
|
+
const RESERVED: readonly string[] = ['id', 'AND', 'OR', 'NOT'];
|
|
23
|
+
const FIELD_TYPES: readonly string[] = ['text', 'keyword', 'date', 'number', 'boolean', 'geo'];
|
|
24
|
+
|
|
25
|
+
const isPositiveInteger = (value: number) => Number.isInteger(value) && value > 0;
|
|
26
|
+
|
|
27
|
+
const checkDeclaration = (name: string, declaration: FieldDeclaration) => {
|
|
28
|
+
if (!FIELD_NAME.test(name)) {
|
|
29
|
+
return `must match ${FIELD_NAME}`;
|
|
30
|
+
}
|
|
31
|
+
if (RESERVED.includes(name)) {
|
|
32
|
+
return 'is reserved';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { type, weight, facet } = declaration;
|
|
36
|
+
if (!FIELD_TYPES.includes(type)) {
|
|
37
|
+
return `has unknown type "${type}"`;
|
|
38
|
+
}
|
|
39
|
+
if (type === 'geo') {
|
|
40
|
+
const role = (['basic', 'advanced', 'filter', 'facet', 'sort'] as const).find((r) => declaration[r]);
|
|
41
|
+
if (role) {
|
|
42
|
+
return `is a geo field, which can't have the ${role} role`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (name === 'relevance' && declaration.sort) {
|
|
46
|
+
return "can't be a sort field, as relevance is always a sort";
|
|
47
|
+
}
|
|
48
|
+
if (weight !== undefined && (type !== 'text' || !isPositiveInteger(weight) || weight > 4)) {
|
|
49
|
+
return 'has a bad weight: only a text field has one, from 1 to 4';
|
|
50
|
+
}
|
|
51
|
+
if (typeof facet === 'object' && !isPositiveInteger(facet.size)) {
|
|
52
|
+
return 'has a bad facet size: it must be a positive integer';
|
|
53
|
+
}
|
|
54
|
+
if (facet && type === 'number') {
|
|
55
|
+
return "is a number field, which can't be a facet";
|
|
56
|
+
}
|
|
57
|
+
if (declaration.basic && type !== 'text' && type !== 'keyword') {
|
|
58
|
+
return `is a ${type} field, which can't be in basic search`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Throw on a field declaration a search backend can't hold
|
|
66
|
+
*/
|
|
67
|
+
const validateFields = (fields: FieldDeclarations) => {
|
|
68
|
+
let geoField: string | undefined;
|
|
69
|
+
|
|
70
|
+
for (const [name, declaration] of Object.entries(fields)) {
|
|
71
|
+
const problem = checkDeclaration(name, declaration);
|
|
72
|
+
if (problem) {
|
|
73
|
+
throw new Error(`Search field "${name}" ${problem}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (declaration.type === 'geo') {
|
|
77
|
+
if (geoField) {
|
|
78
|
+
throw new Error(`Search field "${name}" is a second geo field, after "${geoField}": only one is allowed`);
|
|
79
|
+
}
|
|
80
|
+
geoField = name;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Derive a search backend's `/capabilities`-shaped capabilities from its field declarations
|
|
87
|
+
*/
|
|
88
|
+
export const capabilitiesFromFields = (fields: FieldDeclarations): BackendSearchCapability => {
|
|
89
|
+
validateFields(fields);
|
|
90
|
+
|
|
91
|
+
const declared = Object.entries(fields);
|
|
92
|
+
const publish = <Published extends object>(
|
|
93
|
+
hasRole: (declaration: FieldDeclaration) => unknown,
|
|
94
|
+
describe: (declaration: FieldDeclaration) => Published = () => ({}) as Published,
|
|
95
|
+
) =>
|
|
96
|
+
Object.fromEntries(
|
|
97
|
+
declared
|
|
98
|
+
.filter(([, declaration]) => hasRole(declaration))
|
|
99
|
+
.map(([name, declaration]) => [
|
|
100
|
+
name,
|
|
101
|
+
{ ...describe(declaration), ...(declaration.label !== undefined && { label: declaration.label }) },
|
|
102
|
+
]),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const advanced = publish(({ advanced }) => advanced);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
filters: publish(
|
|
109
|
+
({ filter, facet }) => filter || facet,
|
|
110
|
+
({ type }) => ({
|
|
111
|
+
type: type === 'text' || type === 'keyword' ? 'string' : (type as 'date' | 'number' | 'boolean'),
|
|
112
|
+
}),
|
|
113
|
+
),
|
|
114
|
+
facets: publish(({ facet }) => facet),
|
|
115
|
+
sorts: publish(({ sort }) => sort),
|
|
116
|
+
searchTypes: { basic: {}, ...(Object.keys(advanced).length > 0 && { advanced: { fields: advanced } }) },
|
|
117
|
+
geo: { supported: declared.some(([, { type }]) => type === 'geo') },
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const DEFAULT_FACET_SIZE = 20;
|
|
122
|
+
|
|
123
|
+
export const isBasic = ({ type, basic }: FieldDeclaration) => (type === 'text' ? basic !== false : Boolean(basic));
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The declarations with their defaults applied, as a search backend builds its queries from them
|
|
127
|
+
*/
|
|
128
|
+
export const resolveFields = (fields: FieldDeclarations) => {
|
|
129
|
+
const declared = Object.entries(fields);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
basic: declared
|
|
133
|
+
.filter(([, declaration]) => isBasic(declaration))
|
|
134
|
+
.map(([name, { type, weight = 1 }]) => ({ name, type, weight })),
|
|
135
|
+
facets: declared
|
|
136
|
+
.filter(([, { facet }]) => facet)
|
|
137
|
+
.map(([name, { type, facet }]) => ({
|
|
138
|
+
name,
|
|
139
|
+
type,
|
|
140
|
+
size: typeof facet === 'object' ? facet.size : DEFAULT_FACET_SIZE,
|
|
141
|
+
})),
|
|
142
|
+
geoField: declared.find(([, { type }]) => type === 'geo')?.[0],
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The declarations with the implicit `id` field, which a search visibility filter can also use
|
|
148
|
+
*/
|
|
149
|
+
export const withId = (fields: FieldDeclarations): FieldDeclarations => ({ ...fields, id: { type: 'keyword' } });
|
|
150
|
+
|
|
151
|
+
export type GeoPoint = { lat: number; lng: number };
|
|
152
|
+
|
|
153
|
+
type ScalarOf<Type extends FieldType> = Type extends 'number'
|
|
154
|
+
? number
|
|
155
|
+
: Type extends 'boolean'
|
|
156
|
+
? boolean
|
|
157
|
+
: Type extends 'geo'
|
|
158
|
+
? GeoPoint
|
|
159
|
+
: string;
|
|
160
|
+
|
|
161
|
+
type ValueOf<Declaration extends FieldDeclaration> = Declaration['type'] extends 'geo'
|
|
162
|
+
? GeoPoint
|
|
163
|
+
: Declaration extends { sort: true }
|
|
164
|
+
? ScalarOf<Declaration['type']>
|
|
165
|
+
: ScalarOf<Declaration['type']> | ScalarOf<Declaration['type']>[];
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* One entity's values for the declared fields, as a consumer supplies it to a search backend
|
|
169
|
+
*/
|
|
170
|
+
export type SearchDocument<Fields extends FieldDeclarations = FieldDeclarations> = { id: string } & {
|
|
171
|
+
-readonly [Name in keyof Fields]?: ValueOf<Fields[Name]>;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export type Scalar = string | number | boolean | Date;
|
|
175
|
+
|
|
176
|
+
/** A value as JSON, with dates as ISO strings */
|
|
177
|
+
export const toWire = (value: Scalar) => (value instanceof Date ? value.toISOString() : value);
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* A validated search document: dates are their first UTC instant, missing fields are left out and the geo field's
|
|
181
|
+
* value is the point
|
|
182
|
+
*/
|
|
183
|
+
type StoredSearchDocument = { id: string; values: Record<string, Scalar | Scalar[]>; point?: GeoPoint };
|
|
184
|
+
|
|
185
|
+
const DATE = /^(\d{4})(?:-(\d{2})(?:-(\d{2})(?:[Tt ](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?([Zz]|[+-]\d{2}:\d{2})?)?)?)?$/;
|
|
186
|
+
|
|
187
|
+
const parseDate = (value: string) => {
|
|
188
|
+
const match = DATE.exec(value);
|
|
189
|
+
if (!match) {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const [, year, month = '01', day = '01', hour = '00', minute = '00', second = '00', fraction = '', offset = 'Z'] =
|
|
194
|
+
match;
|
|
195
|
+
const parts = [year, month, day, hour, minute, second].map(Number);
|
|
196
|
+
|
|
197
|
+
const date = new Date(0);
|
|
198
|
+
date.setUTCFullYear(parts[0], parts[1] - 1, parts[2]);
|
|
199
|
+
date.setUTCHours(parts[3], parts[4], parts[5], Number(fraction.padEnd(3, '0').slice(0, 3)));
|
|
200
|
+
|
|
201
|
+
const stored = [
|
|
202
|
+
date.getUTCFullYear(),
|
|
203
|
+
date.getUTCMonth() + 1,
|
|
204
|
+
date.getUTCDate(),
|
|
205
|
+
date.getUTCHours(),
|
|
206
|
+
date.getUTCMinutes(),
|
|
207
|
+
date.getUTCSeconds(),
|
|
208
|
+
];
|
|
209
|
+
if (stored.some((part, index) => part !== parts[index])) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (offset.toUpperCase() !== 'Z') {
|
|
214
|
+
const [offsetHours, offsetMinutes] = offset.slice(1).split(':').map(Number);
|
|
215
|
+
if (offsetHours > 23 || offsetMinutes > 59) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
const sign = offset.startsWith('-') ? -1 : 1;
|
|
219
|
+
date.setTime(date.getTime() - sign * (offsetHours * 60 + offsetMinutes) * 60_000);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return date;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The UTC instants a date covers, as a match value: a year, month or day is the whole period, a date-time its instant
|
|
227
|
+
*/
|
|
228
|
+
export const dateSpan = (value: string) => {
|
|
229
|
+
const first = parseDate(value);
|
|
230
|
+
if (!first) {
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const [, , month, day, hour] = DATE.exec(value) as RegExpExecArray;
|
|
235
|
+
if (hour !== undefined) {
|
|
236
|
+
return { first, last: first };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const next = new Date(first);
|
|
240
|
+
if (day !== undefined) {
|
|
241
|
+
next.setUTCDate(next.getUTCDate() + 1);
|
|
242
|
+
} else if (month !== undefined) {
|
|
243
|
+
next.setUTCMonth(next.getUTCMonth() + 1);
|
|
244
|
+
} else {
|
|
245
|
+
next.setUTCFullYear(next.getUTCFullYear() + 1);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { first, last: new Date(next.getTime() - 1) };
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const isGeoPoint = (value: unknown): value is GeoPoint => {
|
|
252
|
+
if (typeof value !== 'object' || value === null || Object.keys(value).length !== 2) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
const { lat, lng } = value as Partial<GeoPoint>;
|
|
256
|
+
|
|
257
|
+
return typeof lat === 'number' && typeof lng === 'number' && Math.abs(lat) <= 90 && Math.abs(lng) <= 180;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export const DATE_FORMS = 'a YYYY, YYYY-MM, YYYY-MM-DD or RFC 3339 date';
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Check a value against a field type, with a date as its first UTC instant
|
|
264
|
+
*/
|
|
265
|
+
export const storeScalar = (type: FieldType, value: unknown): { value: Scalar | GeoPoint } | { problem: string } => {
|
|
266
|
+
switch (type) {
|
|
267
|
+
case 'text':
|
|
268
|
+
case 'keyword':
|
|
269
|
+
return typeof value === 'string' ? { value } : { problem: 'must be a string' };
|
|
270
|
+
case 'date': {
|
|
271
|
+
const date = typeof value === 'string' ? parseDate(value) : undefined;
|
|
272
|
+
return date ? { value: date } : { problem: `must be ${DATE_FORMS}` };
|
|
273
|
+
}
|
|
274
|
+
case 'number':
|
|
275
|
+
return typeof value === 'number' && Number.isFinite(value) ? { value } : { problem: 'must be a finite number' };
|
|
276
|
+
case 'boolean':
|
|
277
|
+
return typeof value === 'boolean' ? { value } : { problem: 'must be a boolean' };
|
|
278
|
+
case 'geo':
|
|
279
|
+
return isGeoPoint(value)
|
|
280
|
+
? { value }
|
|
281
|
+
: { problem: 'must be { lat, lng } with lat from -90 to 90 and lng from -180 to 180' };
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Validate a batch of search documents against the field declarations, throwing on the first bad one
|
|
287
|
+
*/
|
|
288
|
+
export const storeDocuments = (fields: FieldDeclarations, docs: SearchDocument[]): StoredSearchDocument[] =>
|
|
289
|
+
docs.map(({ id, ...rest }) => {
|
|
290
|
+
if (typeof id !== 'string' || id === '') {
|
|
291
|
+
throw new Error('Search document has no id');
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const fail = (key: string, problem: string) => {
|
|
295
|
+
throw new Error(`Search document "${id}": "${key}" ${problem}`);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const stored: StoredSearchDocument = { id, values: {} };
|
|
299
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
300
|
+
const declaration = Object.hasOwn(fields, key) ? fields[key] : undefined;
|
|
301
|
+
if (!declaration) {
|
|
302
|
+
return fail(key, "isn't a declared field");
|
|
303
|
+
}
|
|
304
|
+
if (Array.isArray(value) && (declaration.sort || declaration.type === 'geo')) {
|
|
305
|
+
return fail(key, 'takes a single value, not an array');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const items = (Array.isArray(value) ? value : [value]).map((item) => {
|
|
309
|
+
const result = storeScalar(declaration.type, item);
|
|
310
|
+
return 'value' in result ? result.value : fail(key, `${result.problem}, not ${JSON.stringify(item)}`);
|
|
311
|
+
});
|
|
312
|
+
if (declaration.type === 'geo') {
|
|
313
|
+
stored.point = items[0] as GeoPoint;
|
|
314
|
+
} else {
|
|
315
|
+
stored.values[key] = Array.isArray(value) ? (items as Scalar[]) : (items[0] as Scalar);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return stored;
|
|
320
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { QueryContainer } from '@opensearch-project/opensearch/api/_types/_common.query_dsl.js';
|
|
2
|
+
import { matchedDateSpan } from '../advanced.js';
|
|
3
|
+
import type { AdvancedQuery } from '../backend.js';
|
|
4
|
+
import type { FieldDeclarations } from '../fields.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Translate an advanced query tree to OpenSearch DSL: text matches all words or the phrase, other types match exactly
|
|
8
|
+
*/
|
|
9
|
+
export const advancedQueryFor = (query: AdvancedQuery, fields: FieldDeclarations): QueryContainer => {
|
|
10
|
+
switch (query.type) {
|
|
11
|
+
case 'and':
|
|
12
|
+
return { bool: { must: query.clauses.map((clause) => advancedQueryFor(clause, fields)) } };
|
|
13
|
+
case 'or':
|
|
14
|
+
return {
|
|
15
|
+
bool: { should: query.clauses.map((clause) => advancedQueryFor(clause, fields)), minimum_should_match: 1 },
|
|
16
|
+
};
|
|
17
|
+
case 'not':
|
|
18
|
+
return { bool: { must_not: [advancedQueryFor(query.clause, fields)] } };
|
|
19
|
+
case 'match': {
|
|
20
|
+
const { field, value, phrase } = query;
|
|
21
|
+
const { type } = fields[field];
|
|
22
|
+
if (type === 'date') {
|
|
23
|
+
const { first, last } = matchedDateSpan(field, value);
|
|
24
|
+
|
|
25
|
+
return { range: { [field]: { gte: first.toISOString(), lte: last.toISOString() } } };
|
|
26
|
+
}
|
|
27
|
+
if (type !== 'text') {
|
|
28
|
+
return { term: { [field]: value } };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return phrase ? { match_phrase: { [field]: value } } : { match: { [field]: { query: value, operator: 'and' } } };
|
|
32
|
+
}
|
|
33
|
+
default:
|
|
34
|
+
return { match_all: {} };
|
|
35
|
+
}
|
|
36
|
+
};
|