forked from shelfio/es-painless-fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (114 loc) · 3.71 KB
/
index.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
'use strict';
const {unflatten} = require('flat');
module.exports = {
/**
* Generates a script object which sets fields on the _source document
* @param {Object} fieldsMap Object with fields to set
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
set(fieldsMap = {}) {
const source = Object.keys(fieldsMap)
.map(key => `ctx._source.${key} = params.${key};`)
.join(' ');
return {
lang: 'painless',
source,
params: unflatten(fieldsMap)
};
},
/**
* Generates a script object which unsets fields on the _source document
* @param {String[]} fields Arrray of field names to unset
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
unset(fields = []) {
const source = fields.map(key => `ctx._source.remove('${key}')`).join('; ');
return {
lang: 'painless',
source
};
},
/**
* Generates a script object which replaces fields on the _source document
* @param {Object[]} fieldsReplacements Array of objects describing what to replace
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
replace(fieldsReplacements = []) {
const source = fieldsReplacements
.map((replaceRule, i) => {
const sourceField = `ctx._source.${replaceRule.field}`;
const pattern = `params.patterns[${i}]`;
const substring = `params.substrings[${i}]`;
return `${sourceField} = ${sourceField}.replace(${pattern}, ${substring});`;
})
.join(' ');
return {
lang: 'painless',
source,
params: {
patterns: fieldsReplacements.map(i => i.pattern),
substrings: fieldsReplacements.map(i => i.substring)
}
};
},
/**
* Generates a script object which increments fields on the _source document
* @param {Object} fieldsMap Object with fields to increment
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
increment(fieldsMap = {}) {
const source = Object.keys(fieldsMap)
.map(key => `ctx._source.${key} += params._inc.${key};`)
.join(' ');
return {
lang: 'painless',
source,
params: source ? {_inc: unflatten(fieldsMap)} : {}
};
},
/**
* Generates a script object which decrements fields on the _source document
* @param {Object} fieldsMap Object with fields to decrement
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
decrement(fieldsMap = {}) {
const source = Object.keys(fieldsMap)
.map(key => `ctx._source.${key} -= params._dec.${key};`)
.join(' ');
return {
lang: 'painless',
source,
params: source ? {_dec: unflatten(fieldsMap)} : {}
};
},
/**
* Generates a script object which multiplies fields on the _source document
* @param {Object} fieldsMap Object with fields to multiply
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
multiply(fieldsMap = {}) {
const source = Object.keys(fieldsMap)
.map(key => `ctx._source.${key} *= params._mul.${key};`)
.join(' ');
return {
lang: 'painless',
source,
params: source ? {_mul: unflatten(fieldsMap)} : {}
};
},
/**
* Generates a script object which divides fields on the _source document
* @param {Object} fieldsMap Object with fields to divide
* @return {{lang: string, source: string, params: *}} Painless Script Object
*/
divide(fieldsMap = {}) {
const source = Object.keys(fieldsMap)
.map(key => `ctx._source.${key} /= params._div.${key};`)
.join(' ');
return {
lang: 'painless',
source,
params: source ? {_div: unflatten(fieldsMap)} : {}
};
}
};