-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
364 lines (313 loc) · 8.99 KB
/
helpers.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
/* eslint-disable no-redeclare */
// @ts-ignore
import semi from 'semi'
import { BooleanQuery, Query } from '../types/queries'
import { SearchOptions, SortOptions } from '../types/client'
import { ElasticError } from '../classes'
import { config } from '../config'
import _ from './queries'
// ---------------------------------------------------------------------------------------------------------------------
// request
// ---------------------------------------------------------------------------------------------------------------------
/**
* Build a query based on a hash of values
*/
export function $query (fields: Record<string, any>, options: { type?: 'and' | 'or', exact?: boolean } = {}): BooleanQuery | Query {
// ensure all keys have values
const keys = Object.keys(fields).filter(key => fields[key] !== '')
// if no keys have values, return a match all
if (keys.length === 0) {
return _.all()
}
// functions
const bool = options.type === 'or'
? _.should
: _.must
const match = options.exact
? _.term
: _.matchPhrasePrefix
// otherwise, build clauses
const clauses = keys
.map((key) => {
const value = fields[key]
// value is array
if (Array.isArray(value)) {
return bool(value.map(value => match(key, value)))
}
// wildcard key
if (key === '_all' || key === '*') {
return _.multiMatch(['*'], value) // used to be matchAny
}
// normal match
return typeof value === 'string' && value.includes('*')
? _.wildcard(key, value.trim().replace(/ +/g, '*'))
: match(key, value)
})
// return query
return clauses.length > 1
? bool(clauses)
: clauses[0]
}
/**
* Helper to build a query based on the request
*/
$query.request = function (req: { params?: Record<string, any>, query?: Record<string, any> }): BooleanQuery | Query {
return $query({ ...req.params, ...req.query })
}
/**
* Helper to build a script object from a string or JavaScript function
*/
export function $script (source: Function | string, params?: Record<string, any>) {
return {
lang: 'painless',
source: $painless(source),
...params ? { params } : {},
}
}
/**
* Helper to build a Painless script from a JavaScript function
*/
export function $painless (source: Function | string): string {
// convert functions
if (typeof source === 'function') {
// remove function wrapper
source = String(source)
.replace(/^function\s+.+?{|}$/g, '')
.trim()
}
// add semicolons
if (source.includes('\n')) {
source = String(semi.add(source)).trim()
}
// convert to painless
source = source
// convert declarations
.replace(/\b(var|let|const)\b/g, 'def')
// optimise declarations
.replace(/for\s*\(\s*def/g, 'for (int')
// remove strict equality
.replace(/([!=]=)(=)/g, '$1')
// compact whitespace
if (source.includes('\n')) {
source = source
.replace(/([;{}])[\n\r]\s*/g, '$1 ')
.trim()
}
// return
return source
}
/**
* Build sort options from string, array or object
*
* @param field
* @param keyword
* @see https://www.elastic.co/guide/en/elasticsearch/reference/7.15/sort-search-results.html
*/
export function $sort (field: string, keyword?: boolean): SortOptions
export function $sort (fields: string[], keyword?: boolean): SortOptions
export function $sort (fields: Record<string, string | number | boolean>, keyword?: boolean): SortOptions
export function $sort (value: any, keyword = true): SortOptions {
// helper
const makeKey = (value: string) => keyword ? `${value}.keyword` : value
const ASC = 'asc'
const DESC = 'desc'
// string: sort by that key, ascending
if (typeof value === 'string') {
return { [makeKey(value)]: ASC }
}
// array: sort by those keys, ascending
if (Array.isArray(value)) {
return (value as string[]).reduce((output, value) => {
output[makeKey(value)] = ASC
return output
}, {} as any)
}
// object: sort by key => values, depending on value
return Object.keys(value).reduce((output, key) => {
const v = value[key]
output[makeKey(key)] = typeof v === 'string'
? v.toLowerCase()
: typeof v === 'number'
? v < 0 ? DESC : ASC
: v ? ASC : DESC
return output
}, {} as any)
}
/**
* Helper to build search options
*/
export function $options (options: SearchOptions) {
const defaults = {
size: 50,
}
return { ...defaults, ...options }
}
// ---------------------------------------------------------------------------------------------------------------------
// bulk
// ---------------------------------------------------------------------------------------------------------------------
/**
* Helpers to build bulk operations
*/
export const $bulk = {
index (index: string, docs: any[], idField?: string) {
return docs.flatMap(doc => {
const _id = idField
? doc[idField]
: undefined
return [
{ index: { _index: index }, _id },
doc,
]
})
},
search (index: string, queries: Query[], options: SearchOptions = {}) {
return queries.flatMap(query => [
{ index },
{ query, ...options },
])
},
}
// ---------------------------------------------------------------------------------------------------------------------
// response
// ---------------------------------------------------------------------------------------------------------------------
/**
* Helper to build the final returned document from hit and / or source data
*/
export function $doc (hit: any, source: any = {}) {
return { _id: hit._id, ...source }
}
/**
* Helper to paginate search results, returning `meta` and `hits` components
*/
export function $paginate (res: any, options: SearchOptions) {
const hits = $extract.search(res, options)
const meta = res.body.hits
return {
meta: {
total: meta.total.value,
size: options.size || 10,
from: options.from || 0,
},
hits,
}
}
/**
* Helper to remap a document's fields
*/
export function $fields (doc: Record<string, any>, fields: Record<string, string> | Function) {
if (typeof fields === 'function') {
try {
return fields(doc)
}
catch (err: any) {
return err.message
}
}
return Object.keys(fields).reduce((output, to) => {
const from = fields[to]
output[to] = doc[from]
return output
}, {} as any)
}
// ---------------------------------------------------------------------------------------------------------------------
// results
// ---------------------------------------------------------------------------------------------------------------------
/**
* Helper to process results into a simple consumable format
*/
export function $results (res: any, req: any, type?: string) {
const { result } = res.body
// create
if (result === 'created') {
return $doc(res.body, req.body)
}
// get
if (result === 'get') {
return $doc(res.body)
}
// search
if (res.body.hits) {
return res.body.hits.hits.map((hit: any) => {
return $doc(hit, hit._source)
})
}
// update
if (result === 'updated') {
return $doc(res.body, req.body)
}
// anything else
return $doc(res.body)
}
/**
* Helpers to extract search results
*/
export const $extract = {
create (res: any, doc: any = {}) {
return $doc(res.body, doc)
},
get (res: any) {
},
search (res: any, options: SearchOptions) {
return (res.body || res).hits.hits.map((hit: any) => {
const doc = $doc(hit, hit._source)
return options._fields
? $fields(doc, options._fields)
: doc
})
},
update (res: any, doc = {}) {
const result = res.body.result
if (result === 'updated') {
return $doc(res.body, doc)
}
return {}
},
delete (res: any) {
const result = res.body.result
if (result === 'deleted') {
return $doc(res.body)
}
return {}
}
}
// ---------------------------------------------------------------------------------------------------------------------
// debugging
// ---------------------------------------------------------------------------------------------------------------------
/**
* Helper to log data
*/
export function $log (label: string, data: any = null) {
if (config.debug) {
console.log(label + ': ' + JSON.stringify(data, null, ' ')) // ?
}
}
/**
* Helper to generate (and optionally log) an error
*/
export function $error (info: any) {
const error = new ElasticError(info)
$log('error', error)
return error
}
// ---------------------------------------------------------------------------------------------------------------------
// export
// ---------------------------------------------------------------------------------------------------------------------
export default {
// request
sort: $sort,
painless: $painless,
script: $script,
query: $query,
options: $options,
// response
paginate: $paginate,
results: $results,
fields: $fields,
doc: $doc,
// bulk
bulk: $bulk,
extract: $extract,
// debugging
log: $log,
error: $error,
}