-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Documents.ts
404 lines (368 loc) · 12.1 KB
/
Documents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import type { ReadStream } from "fs";
import ApiCall from "./ApiCall";
import Configuration from "./Configuration";
import { ImportError } from "./Errors";
import { SearchOnlyDocuments } from "./SearchOnlyDocuments";
// Todo: use generic to extract filter_by values
export interface DeleteQuery {
filter_by: string;
batch_size?: number;
ignore_not_found?: boolean;
}
export interface DeleteResponse {
num_deleted: number;
}
interface ImportResponseSuccess {
success: true;
}
export interface ImportResponseFail {
success: false;
error: string;
document: DocumentSchema;
code: number;
}
export type ImportResponse = ImportResponseSuccess | ImportResponseFail;
export type DocumentSchema = Record<string, any>;
export interface SearchParamsWithPreset extends Partial<SearchParams> {
preset: string;
}
type OperationMode = "off" | "always" | "fallback";
export interface SearchParams {
// From https://typesense.org/docs/latest/api/documents.html#arguments
q?: string;
query_by?: string | string[];
query_by_weights?: string | number[];
prefix?: string | boolean | boolean[]; // default: true
filter_by?: string;
enable_lazy_filter?: boolean; // default: false
sort_by?: string | string[]; // default: text match desc
facet_by?: string | string[];
max_facet_values?: number;
facet_sample_threshold?: number;
facet_sample_percent?: number;
facet_query?: string;
facet_query_num_typos?: number;
facet_return_parent?: string;
page?: number; // default: 1
per_page?: number; // default: 10, max 250
group_by?: string | string[];
group_limit?: number; // default:
group_missing_values?: boolean;
include_fields?: string | string[];
exclude_fields?: string | string[];
highlight_fields?: string | string[]; // default: all queried fields
highlight_full_fields?: string | string[]; // default: all fields
highlight_affix_num_tokens?: number; // default: 4
highlight_start_tag?: string; // default: <mark>
highlight_end_tag?: string; // default: </mark>
enable_highlight_v1?: boolean;
snippet_threshold?: number; // default: 30
num_typos?: string | number | number[]; // default: 2
min_len_1typo?: number;
min_len_2typo?: number;
split_join_tokens?: OperationMode;
exhaustive_search?: boolean;
drop_tokens_threshold?: number; // default: 10
typo_tokens_threshold?: number; // default: 100
pinned_hits?: string | string[];
hidden_hits?: string | string[];
limit_hits?: number; // default: no limit
pre_segmented_query?: boolean;
enable_overrides?: boolean;
prioritize_exact_match?: boolean; // default: true
prioritize_token_position?: boolean;
prioritize_num_matching_fields?: boolean;
search_cutoff_ms?: number;
use_cache?: boolean;
max_candidates?: number;
infix?: OperationMode | OperationMode[];
preset?: string;
text_match_type?: "max_score" | "max_weight";
vector_query?: string;
"x-typesense-api-key"?: string;
"x-typesense-user-id"?: string;
offset?: number;
limit?: number;
stopwords?: string;
conversation?: boolean;
conversation_model_id?: string;
conversation_id?: string;
voice_query?: string;
}
type SearchResponseHighlightObject = {
matched_tokens?: string[];
snippet?: string;
value?: string;
};
export type SearchResponseHighlight<T> = T extends string | number
? SearchResponseHighlightObject
: {
[TAttribute in keyof T]?: SearchResponseHighlight<T[TAttribute]>;
};
export interface SearchResponseHit<T extends DocumentSchema> {
curated?: true;
highlights?: [
{
field: keyof T;
snippet?: string;
value?: string;
snippets?: string[];
indices?: number[];
matched_tokens: string[][] | string[];
},
];
highlight: SearchResponseHighlight<T>;
document: T;
text_match: number;
text_match_info?: {
best_field_score: string; // To prevent scores from being truncated by JSON spec
best_field_weight: number;
fields_matched: number;
score: string; // To prevent scores from being truncated by JSON spec
tokens_matched: number;
};
}
export interface SearchResponseFacetCountSchema<T extends DocumentSchema> {
counts: {
count: number;
highlighted: string;
value: string;
}[];
field_name: keyof T;
stats: {
avg?: number;
max?: number;
min?: number;
sum?: number;
};
}
export interface SearchResponseRequestParams {
collection_name?: string;
q?: string;
page?: number;
per_page?: number;
first_q?: string;
voice_query?: {
transcribed_query?: string;
};
}
// Todo: we could infer whether this is a grouped response by adding the search params as a generic
export interface SearchResponse<T extends DocumentSchema> {
facet_counts?: SearchResponseFacetCountSchema<T>[];
found: number;
found_docs?: number;
out_of: number;
page: number;
request_params: SearchResponseRequestParams;
search_time_ms: number;
search_cutoff?: boolean;
hits?: SearchResponseHit<T>[];
grouped_hits?: {
group_key: string[];
hits: SearchResponseHit<T>[];
found?: number;
}[];
conversation?: {
answer: string;
conversation_history: {
conversation: object[];
id: string;
last_updated: number;
ttl: number;
};
conversation_id: string;
query: string;
};
}
export interface DocumentWriteParameters {
dirty_values?: "coerce_or_reject" | "coerce_or_drop" | "drop" | "reject";
action?: "create" | "update" | "upsert" | "emplace";
}
export interface UpdateByFilterParameters {
filter_by?: string;
}
export interface UpdateByFilterResponse {
num_updated: number;
}
export interface DocumentImportParameters extends DocumentWriteParameters {
batch_size?: number;
return_doc?: boolean;
return_id?: boolean;
}
export interface DocumentsExportParameters {
filter_by?: string;
include_fields?: string;
exclude_fields?: string;
}
export interface SearchableDocuments<T extends DocumentSchema> {
search(
searchParameters: SearchParams | SearchParamsWithPreset,
options: SearchOptions,
): Promise<SearchResponse<T>>;
clearCache(): void;
}
export interface WriteableDocuments<T> {
create(document: T, options: DocumentWriteParameters): Promise<T>;
upsert(document: T, options: DocumentWriteParameters): Promise<T>;
update(document: T, options: DocumentWriteParameters): Promise<T>;
delete(query: DeleteQuery): Promise<DeleteResponse>;
import(
documents: T[] | string,
options: DocumentWriteParameters,
): Promise<string | ImportResponse[]>;
export(options: DocumentsExportParameters): Promise<string>;
}
export interface SearchOptions {
cacheSearchResultsForSeconds?: number;
abortSignal?: AbortSignal | null;
}
export default class Documents<T extends DocumentSchema = object>
extends SearchOnlyDocuments<T>
implements WriteableDocuments<T>
{
constructor(
collectionName: string,
apiCall: ApiCall,
configuration: Configuration,
) {
super(collectionName, apiCall, configuration);
}
async create(document: T, options: DocumentWriteParameters = {}): Promise<T> {
if (!document) throw new Error("No document provided");
return this.apiCall.post<T>(this.endpointPath(), document, options);
}
async upsert(document: T, options: DocumentWriteParameters = {}): Promise<T> {
if (!document) throw new Error("No document provided");
return this.apiCall.post<T>(
this.endpointPath(),
document,
Object.assign({}, options, { action: "upsert" }),
);
}
async update(
document: T,
options: UpdateByFilterParameters,
): Promise<UpdateByFilterResponse>;
async update(document: T, options: DocumentWriteParameters): Promise<T>;
async update(
document: T,
options: DocumentWriteParameters | UpdateByFilterParameters = {},
): Promise<UpdateByFilterResponse | T> {
if (!document) throw new Error("No document provided");
if (options["filter_by"] != null) {
return this.apiCall.patch<T>(
this.endpointPath(),
document,
Object.assign({}, options),
);
} else {
return this.apiCall.post<T>(
this.endpointPath(),
document,
Object.assign({}, options, { action: "update" }),
);
}
}
async delete(
query: DeleteQuery = {} as DeleteQuery,
): Promise<DeleteResponse> {
return this.apiCall.delete<DeleteResponse>(this.endpointPath(), query);
}
async createMany(documents: T[], options: DocumentImportParameters = {}) {
this.configuration.logger.warn(
"createMany is deprecated and will be removed in a future version. Use import instead, which now takes both an array of documents or a JSONL string of documents",
);
return this.import(documents, options);
}
/**
* Import a set of documents in a batch.
* @param {string|Array} documents - Can be a JSONL string of documents or an array of document objects.
* @param options
* @return {string|Array} Returns a JSONL string if the input was a JSONL string, otherwise it returns an array of results.
*/
async import(
documents: string,
options?: DocumentImportParameters,
): Promise<string>;
async import(
documents: T[],
options?: DocumentImportParameters,
): Promise<ImportResponse[]>;
async import(
documents: T[] | string,
options: DocumentImportParameters = {},
): Promise<string | ImportResponse[]> {
let documentsInJSONLFormat;
if (Array.isArray(documents)) {
try {
documentsInJSONLFormat = documents
.map((document) => JSON.stringify(document))
.join("\n");
} catch (error: any) {
// if rangeerror, throw custom error message
if (
error instanceof RangeError &&
error.message.includes("Too many properties to enumerate")
) {
throw new Error(`${error}
It looks like you have reached a Node.js limit that restricts the number of keys in an Object: https://stackoverflow.com/questions/9282869/are-there-limits-to-the-number-of-properties-in-a-javascript-object
Please try reducing the number of keys in your document, or using CURL to import your data.
`);
}
// else, throw the non-range error anyways
throw new Error(error);
}
} else {
documentsInJSONLFormat = documents;
}
const resultsInJSONLFormat = await this.apiCall.performRequest<string>(
"post",
this.endpointPath("import"),
{
queryParameters: options,
bodyParameters: documentsInJSONLFormat,
additionalHeaders: { "Content-Type": "text/plain" },
skipConnectionTimeout: true, // We never want to client-side-timeout on an import and retry, since imports are syncronous and we want to let them take as long as it takes to complete fully
enableKeepAlive: true, // This is to prevent ECONNRESET socket hang up errors. Reference: https://github.com/axios/axios/issues/2936#issuecomment-779439991
},
);
if (Array.isArray(documents)) {
const resultsInJSONFormat = resultsInJSONLFormat
.split("\n")
.map((r) => JSON.parse(r)) as ImportResponse[];
const failedItems = resultsInJSONFormat.filter(
(r) => r.success === false,
);
if (failedItems.length > 0) {
throw new ImportError(
`${
resultsInJSONFormat.length - failedItems.length
} documents imported successfully, ${
failedItems.length
} documents failed during import. Use \`error.importResults\` from the raised exception to get a detailed error reason for each document.`,
resultsInJSONFormat,
);
} else {
return resultsInJSONFormat;
}
} else {
return resultsInJSONLFormat as string;
}
}
/**
* Returns a JSONL string for all the documents in this collection
*/
async export(options: DocumentsExportParameters = {}): Promise<string> {
return this.apiCall.get<string>(this.endpointPath("export"), options);
}
/**
* Returns a NodeJS readable stream of JSONL for all the documents in this collection.
*/
async exportStream(
options: DocumentsExportParameters = {},
): Promise<ReadStream> {
return this.apiCall.get<ReadStream>(this.endpointPath("export"), options, {
responseType: "stream",
});
}
}