-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
192 lines (180 loc) · 4.32 KB
/
utils.js
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
import { crocks, HyperErr, isHyperErr, R } from './deps.js'
const { Async } = crocks
const {
assoc,
dissoc,
ifElse,
defaultTo,
identity,
compose,
has,
evolve,
applyTo,
propOr,
always,
path,
flip,
join,
reduce,
map,
concat,
set,
lensProp,
toPairs,
} = R
export const underscoreIdAlias = '__movedUnderscoreId63__'
export const handleHyperErr = ifElse(
isHyperErr,
Async.Resolved,
Async.Rejected,
)
export const toEsErr = (err, fallbackStatus = 500) => ({
err, // backreference
/**
* used caused_by
* fallback to top-lvl reason
* fallback to generic error message
*/
reason: compose(
defaultTo(propOr('an error occurred', 'reason', err)),
path(['caused_by', 'reason']),
)(err),
type: propOr('unknown', 'type', err),
/**
* use body status
* fallback to response status
*/
status: propOr(fallbackStatus, 'status', err),
})
/**
* Generate string templates
*/
const template = (strings, ...keys) => (dict) => {
const result = [strings[0]]
keys.forEach((key, i) => {
result.push(dict[key], strings[i + 1])
})
return result.join('')
}
export const esErrToHyperErr = (context) =>
compose(
HyperErr,
({ status, type, reason }) =>
evolve(
{ msg: applyTo({ reason, ...context }) }, // populate the msg template
/**
* Set status and msg template
*/
propOr(
{ status, msg: always(reason) },
type,
{
resource_already_exists_exception: {
status: 409,
msg: template`${'subject'} already exists`,
},
mapper_parsing_exception: {
status: 422,
msg: template`failed to parse mapping for ${'subject'}: ${'reason'}`,
},
index_not_found_exception: {
status: 404,
msg: template`${'index'} not found`,
},
resource_not_found_exception: {
status: 404,
msg: template`${'subject'} not found`,
},
not_found: {
status: 404,
msg: template`${'subject'} not found`,
},
},
),
),
)
const swap = (old, cur) =>
compose(
dissoc(old),
(o) => assoc(cur, o[old], o),
)
export const moveUnderscoreId = ifElse(
has('_id'),
swap('_id', underscoreIdAlias),
identity,
)
export const toUnderscoreId = ifElse(
has(underscoreIdAlias),
swap(underscoreIdAlias, '_id'),
identity,
)
/**
* Create an elasticsearch _bulk index payload
*
* See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk-api-example
*
* @param {string} index
* @param {object[]} docs
* @returns {string} - the bulk payload to send to elasticsearch
*/
export const bulkToEsBulk = (index, docs) =>
compose(
// Bulk payload must end with a newline
flip(concat)('\n'),
join('\n'),
// stringify each object in arr
map(JSON.stringify.bind(JSON)),
reduce(
(
arr,
doc,
) => [
...arr,
{ index: { _index: index, _id: doc._id || doc.id } },
moveUnderscoreId(doc),
],
[],
),
)(docs)
/**
* @param {mappings} - hyper index mappings { fields }
* @returns {object} -
*/
export const mappingsToEsMappings = compose(
(properties) => ({
mappings: { properties },
}),
/**
* _id is automatically mapped, and will produce an error if included,
* so rename id
*/
moveUnderscoreId,
(mappings) =>
mappings.fields.reduce(
(a, f) => set(lensProp(f), { type: 'text' }, a),
{},
),
defaultTo({ fields: [] }),
)
export const queryToEsQuery = ({ query, fields, filter }) => ({
query: {
bool: {
must: {
multi_match: {
query,
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-fuzziness
fuzziness: 'AUTO',
// map _id => underscoreIdAlias
fields: fields
? fields.map((field) => field === '_id' ? underscoreIdAlias : field)
: undefined,
},
},
filter: toPairs(filter).map(
// map _id => underscoreIdAlias
([key, value]) =>
key === '_id' ? ({ term: { [underscoreIdAlias]: value } }) : ({ term: { [key]: value } }),
),
},
},
})