-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixin_es.js
345 lines (311 loc) · 10.5 KB
/
mixin_es.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
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
import elasticsearch from 'elasticsearch';
import config from 'config';
import _ from 'lodash';
import Promise from 'bluebird';
import { Model, DataTypes } from 'sequelize';
import logger from '../../config/logger';
let esClient;
if (config.eshost) {
esClient = new elasticsearch.Client({
host: config.eshost,
log: 'trace',
});
}
export default (model, opts) => {
if (!esClient) {
logger.warn('config.eshost does not exist or esClient is not set. Model.esSearch will return empty array');
model.esSearch = (originalQuery, searchOpts) => {
logger.warn('Since esClient is not set, this method just return empty array!');
if (searchOpts.paginate) {
return Promise.resolve({ count: 0, rows: [] });
}
return Promise.resolve([]);
};
return;
}
const defaultOpts = { propagateUpdate: [], include: [] };
opts = _.extend(defaultOpts, opts);
const indexName = `${opts.indexPrefix || ''}:` + model.tableName;
esClient.indices
.exists({
index: indexName,
})
.then(
exists =>
!exists &&
esClient.indices.create({
index: indexName,
})
);
model.prototype._toESDoc = function() {
return this.toESDoc ? this.toESDoc() : this.get({ role: 'admin' });
};
model.prototype.addES = function() {
return Promise.fromCallback(cb => {
return Promise.resolve(this._toESDoc()) // admin role이 가장 많은 field를 갖고 있으므로.
.then(doc =>
esClient.index(
{
index: indexName,
type: 'default',
id: this.id,
body: doc,
},
cb
)
);
});
};
model.prototype.removeES = function() {
return Promise.fromCallback(cb => {
return esClient.delete(
{
index: indexName,
type: 'default',
id: this.id,
},
cb
);
});
};
model.hook('afterCreate', 'insertToES', (instance, opts) => {
const { transaction } = opts;
const cb = () => instance.addES();
transaction ? transaction.afterCommit(cb, true) : setTimeout(cb);
});
model.hook('afterUpdate', 'updateToES', (instance, opts) => {
const { transaction } = opts;
const cb = () => instance.addES();
transaction ? transaction.afterCommit(cb, true) : setTimeout(cb);
});
model.hook('afterDestroy', 'deleteFromES', (instance, opts) => {
const { transaction } = opts;
const cb = () => instance.removeES();
transaction ? transaction.afterCommit(cb, true) : setTimeout(cb);
});
// opts.propagateUpdate에 등록된 모델이 association에 있는 애들이면 현재 모델이 업데이트 됐을때 해당 모델에 대한 인덱스를 다시 수행
opts.propagateUpdate &&
_.castArray(opts.propagateUpdate).forEach(targetName => {
_.forEach(model.associations, association =>
(function(association) {
if (association.target.name !== targetName) return;
const targetModel = association.target;
if (association.associationType === 'HasMany' || association.associationType === 'BelongsTo') {
const fname = association.accessors.get;
model.hook(
'afterUpdate',
`update${targetModel.name}ToES`,
(function(fname) {
return (instance, opts) => {
const { transaction } = opts;
const cb = () =>
instance[fname]().then(targets => {
return Promise.map(_.castArray(targets), target => target.addES && target.addES());
});
transaction ? transaction.afterCommit(cb, true) : setTimeout(cb);
};
})(fname)
);
} else {
throw new Error('only HasMany or BelongsTo relation target can be auto re-indexed');
}
})(association)
);
});
model._indexAll2ES = function() {
return model.findAll().then(allInstances => {
return Promise.reduce(
allInstances,
(acc, instance) =>
Promise.resolve(instance._toESDoc()).then(doc => [
...acc,
{
index: { _index: indexName, _type: 'default', _id: instance.id },
},
doc,
]),
[]
).then((
body // then of Promise.reduce
) => Promise.fromCallback(cb => esClient.bulk({ body }, cb)));
});
};
model._getESQuery = function(originalQuery, opts = { paginate: true, page: 1, limit: 20, operator: '$and' }) {
originalQuery = _.reduce(
originalQuery,
(acc, v, k) => {
if (!!v) {
acc = { ...acc, [k]: v };
}
return acc;
},
{}
);
const mustConditions = [];
if (originalQuery.company_id) {
mustConditions.push({ term: { company_id: originalQuery.company_id } });
}
const query = _.omit(originalQuery, 'company_id');
const specialChars =
'([' +
'+ - = & | ! ( ) { } [ ] ^ " ~ * ? : \\ /'
.split(' ')
.map(x => `\\${x}`)
.join('') +
'])';
const regex = new RegExp(specialChars, 'g');
const regex2 = /[<>]/g;
const escapeString = str => str.replace(regex2, '').replace(regex, '\\$1');
function obj2esquery(queryObj) {
/*
queryObj는 키가 한개밖에 없는 object로써 다음의 경우 중 하나다
<queryObj> := | {<op>: [<queryObj>]
| {field: q}
<op>는 $and 또는 $or 이다
{field: q}는 다음과 같이 변환한다.
1) q.type == 'text'
* q.wildcard == 'both'
"field: *{q.value}*"
* q.wildcard == 'prefix'
"field: {q.value}*"
* q.wildcard == 'postfix'
"field: *{q.value}" // 용례: 전화번호 뒷자리
* q.wildcard == false
"field: q" // exact match
2) q.type == 'range'
* "field: [ ${q.value[0]}, ${q.value[1]} ]"
op로 연결된 query들은 op를 infix로 넣어서 결합한다.
*/
const key = Object.keys(queryObj)[0];
if (key === '$or' || key === '$and') {
const arr = queryObj[key];
return '(' + arr.map(q => obj2esquery(q)).join(` ${key.replace('$', '').toUpperCase()} `) + ')';
} else {
const q = queryObj[key];
if (q.type === 'text') {
const v = q.value;
return (
`(${key}: ${q.wildcard === 'both' || q.wildcard === 'postfix' ? '*' : ''}` +
(typeof v === 'string'
? v
.split(/[\s-]+/)
.map(s => escapeString(s))
.join(' AND ')
: v) +
`${q.wildcard === 'both' || q.wildcard === 'prefix' ? '*' : ''})`
);
} else if (q.type === 'range') {
const [from, to] = q.value;
return `(${key}: [${from} TO ${to}])`;
}
}
}
function convertQueryObj(queryObj) {
/*
queryObj의 각 k,v 쌍은 다음의 경우 중 하나다
<elem> := | {<op>: [...] | {}}
| {field: q} => q가 스트링인 경우. value, wildcard, type 형태로 변환함
| {field: {value, wildcard, type}
이 함수는 queryObj의 각 object 형태를 키가 한개인 형태로 변환한다
op가 없이 그냥 연결하면 $and op로 취급한다.
*/
// trivial case
if (_.isEmpty(queryObj)) {
return queryObj;
}
if (Object.keys(queryObj).length === 1) {
const key = Object.keys(queryObj)[0];
if (key === '$or' || key === '$and') {
let subQuery = queryObj[key];
if (_.isObject(subQuery)) {
if (!_.isArray(subQuery)) {
subQuery = _.reduce(
subQuery,
(acc, v, k) => {
return [...acc, { [k]: v }];
},
[]
);
}
} else {
throw new Error('plain value with $or or $and operator is not supported');
}
return { [key]: subQuery.map(s => convertQueryObj(s)) };
} else {
const key = Object.keys(queryObj)[0];
let value = queryObj[key];
if (typeof value !== 'object') {
value = { value: value, type: 'text', wildcard: 'both' };
} else {
if (!value.value) {
throw new Error('invalid search query. value must be defined');
}
if (value.type === 'text' || !value.type) {
value = { type: 'text', wildcard: 'both', ...value };
} else if (value.type === 'range') {
if (!(_.isArray(value.value) && value.value.length === 2)) {
throw new Error('invalid search value. should be an array having length of 2');
}
} else {
throw new Error('invalid search query type. only support term and range but got ' + value.type);
}
}
return { [key]: value };
}
}
return {
$and: _.reduce(queryObj, (acc, v, k) => [...acc, convertQueryObj({ [k]: v })], []),
};
}
if (!_.isEmpty(query)) {
const querystring = obj2esquery(convertQueryObj({ [opts.operator]: query }));
mustConditions.push({ query_string: { query: querystring } });
}
return {
query: {
constant_score: {
filter: {
bool: {
must: mustConditions,
},
},
},
},
};
};
const includeOptions = opts.include;
model.esSearch = function(originalQuery, searchOpts) {
let defaultOpts = {
paginate: true,
page: 1,
limit: 20,
operator: '$and',
sort: [['id', 'desc']],
};
searchOpts = _.assign(defaultOpts, searchOpts);
const esquery = model._getESQuery(originalQuery, searchOpts);
const searchParam = {
index: indexName,
body: esquery,
sort: searchOpts.sort.map(s => `${s[0]}:${s[1]}`),
size: 100,
}; // ES search 결과는 100개 제한
if (searchOpts.paginate) {
_.extend(searchParam, {
from: (searchOpts.page - 1) * searchOpts.limit,
size: searchOpts.limit,
});
}
return Promise.fromCallback(cb => esClient.search(searchParam, cb)).then(result => {
const results = _.map(_.get(result, 'hits.hits') || [], '_source').map(doc =>
// try to build an instance from doc
model.build(doc, { include: includeOptions })
);
if (searchOpts.paginate) {
return { count: result.hits.total, rows: results };
} else {
return results;
}
});
};
};