|
11 | 11 | * node src/init-es
|
12 | 12 | * node src/init-es force
|
13 | 13 | */
|
14 |
| - |
15 |
| -const _ = require('lodash') |
16 | 14 | const logger = require('../../src/common/logger')
|
17 | 15 | const helper = require('../../src/common/helper')
|
18 |
| -const { topResources, userResources } = require('../../src/common/constants') |
| 16 | +const { topResources } = require('../../src/common/constants') |
19 | 17 |
|
20 | 18 | let client
|
21 | 19 |
|
22 |
| -let needsNestedTypes = ['user'] |
23 |
| -const enrichedFields = ['attributegroup', 'skillprovider'] |
24 |
| - |
25 |
| -/** |
26 |
| - * Initialize elastic search index |
27 |
| - * @param {Boolean} isForce boolean flag indicate it is forced operation |
28 |
| - */ |
29 |
| -const init = async (isForce) => { |
30 |
| - if (!client) { |
31 |
| - client = await helper.getESClient() |
32 |
| - } |
33 |
| - if (isForce) { |
34 |
| - await clearES() |
35 |
| - } |
36 |
| - const processors = {} |
37 |
| - for (const key in topResources) { |
38 |
| - const exists = await client.indices.exists({ index: topResources[key].index }) |
39 |
| - const top = topResources[key] |
40 |
| - if (exists.body) { |
41 |
| - logger.info(`The index ${top.index} exists.`) |
42 |
| - } else { |
43 |
| - logger.info(`The index ${top.index} will be created.`) |
44 |
| - await client.indices.create({ |
45 |
| - index: top.index, |
46 |
| - body: { |
47 |
| - mappings: { |
48 |
| - properties: _(_.get(top, 'enrich.enrichFields', [])).map(p => [p, { type: _.includes(enrichedFields, p) ? 'nested' : 'keyword' }]).fromPairs() |
49 |
| - } |
50 |
| - } |
51 |
| - }) |
52 |
| - if (needsNestedTypes.includes(key)) { |
53 |
| - for (const childKey in userResources) { |
54 |
| - if (userResources[childKey].isNested) { |
55 |
| - await client.indices.putMapping({ |
56 |
| - index: topResources[key].index, |
57 |
| - type: topResources[key].type, |
58 |
| - include_type_name: true, |
59 |
| - body: { |
60 |
| - properties: { |
61 |
| - [userResources[childKey].propertyName]: { |
62 |
| - type: 'nested' |
63 |
| - } |
64 |
| - } |
65 |
| - } |
66 |
| - }) |
67 |
| - } |
68 |
| - } |
69 |
| - } |
70 |
| - } |
71 |
| - if (top['enrich']) { |
72 |
| - logger.info(`The enrich policy ${top.enrich.policyName} will be created.`) |
73 |
| - await client.enrich.putPolicy({ |
74 |
| - name: top.enrich.policyName, |
75 |
| - body: { |
76 |
| - match: { |
77 |
| - indices: top.index, |
78 |
| - match_field: top.enrich.matchField, |
79 |
| - enrich_fields: top.enrich.enrichFields |
80 |
| - } |
81 |
| - } |
82 |
| - }) |
83 |
| - await client.enrich.executePolicy({ name: top.enrich.policyName }) |
84 |
| - } |
85 |
| - if (top.pipeline) { |
86 |
| - if (top.pipeline.processors) { |
87 |
| - processors[top.pipeline.id] = [] |
88 |
| - _.each(top.pipeline.processors, processor => { |
89 |
| - processors[top.pipeline.id].push({ |
90 |
| - foreach: { |
91 |
| - field: processor.referenceField, |
92 |
| - ignore_missing: true, |
93 |
| - processor: { |
94 |
| - enrich: { |
95 |
| - policy_name: processor.enrichPolicyName, |
96 |
| - ignore_missing: true, |
97 |
| - field: processor.field, |
98 |
| - target_field: processor.targetField, |
99 |
| - max_matches: processor.maxMatches |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - }) |
104 |
| - }) |
105 |
| - } else { |
106 |
| - processors[top.pipeline.id] = [{ |
107 |
| - enrich: { |
108 |
| - policy_name: top.enrich.policyName, |
109 |
| - ignore_missing: true, |
110 |
| - field: top.pipeline.field, |
111 |
| - target_field: top.pipeline.targetField, |
112 |
| - max_matches: top.pipeline.maxMatches |
113 |
| - } |
114 |
| - }] |
115 |
| - } |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - for (const key in processors) { |
120 |
| - logger.info(`The pipeline ${key} will be created.`) |
121 |
| - await client.ingest.putPipeline({ |
122 |
| - id: key, |
123 |
| - body: { |
124 |
| - processors: processors[key] |
125 |
| - } |
126 |
| - }) |
127 |
| - } |
128 |
| -} |
129 |
| - |
130 |
| -/** |
131 |
| - * Delete elastic search index |
132 |
| - */ |
133 |
| -const clearES = async () => { |
134 |
| - for (const key in topResources) { |
135 |
| - if (topResources[key].pipeline) { |
136 |
| - try { |
137 |
| - logger.info(`Delete pipeline ${topResources[key].pipeline.id} if any.`) |
138 |
| - await client.ingest.deletePipeline({ id: topResources[key].pipeline.id }) |
139 |
| - } catch (err) { |
140 |
| - // ignore |
141 |
| - } |
142 |
| - } |
143 |
| - } |
144 |
| - for (const key in topResources) { |
145 |
| - if (topResources[key].enrich) { |
146 |
| - try { |
147 |
| - const policyName = topResources[key].enrich.policyName |
148 |
| - logger.info(`Delete enrich policy ${policyName} if any.`) |
149 |
| - await client.enrich.deletePolicy({ |
150 |
| - name: policyName |
151 |
| - }) |
152 |
| - } catch (err) { |
153 |
| - // ignore |
154 |
| - } |
155 |
| - } |
156 |
| - } |
157 |
| - for (const key in topResources) { |
158 |
| - logger.info(`Delete index ${topResources[key].index} if any.`) |
159 |
| - try { |
160 |
| - await client.indices.delete({ index: topResources[key].index }) |
161 |
| - } catch (err) { |
162 |
| - // ignore |
163 |
| - } |
164 |
| - } |
165 |
| -} |
166 |
| - |
167 | 20 | /**
|
168 | 21 | * Check if elastic search is empty
|
169 | 22 | */
|
@@ -202,21 +55,7 @@ const clearData = async () => {
|
202 | 55 | }
|
203 | 56 | }
|
204 | 57 |
|
205 |
| -if (!module.parent) { |
206 |
| - const isForce = process.argv.length === 3 && process.argv[2] === 'force' |
207 |
| - |
208 |
| - init(isForce).then(() => { |
209 |
| - logger.info('done') |
210 |
| - process.exit() |
211 |
| - }).catch((e) => { |
212 |
| - logger.logFullError(e) |
213 |
| - process.exit() |
214 |
| - }) |
215 |
| -} |
216 |
| - |
217 | 58 | module.exports = {
|
218 |
| - init, |
219 |
| - clearES, |
220 | 59 | checkEmpty,
|
221 | 60 | clearData
|
222 | 61 | }
|
0 commit comments