-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.js
164 lines (125 loc) · 3.02 KB
/
prepare.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
// Prepare Elasticsearch index
(function () {
const elastic = require('elasticsearch');
// Configuration
let host = 'localhost:9200';
let index = 'my-index';
let type = 'doc';
let client;
// Execute script if not used as a module
if (!module.parent) {
init(
process.argv[2],
process.argv[3],
process.argv[4]
);
}
function init(_host, _index, _type) {
// Overwrite default configuration with arguments
// from module or command line interface
host = _host || host;
index = _index || index;
type = _type || type;
// Initialize Elasticsearch client
client = new elastic.Client({ host: host });
Promise.resolve()
.then(deleteIndex, handleError)
.then(createIndex, handleError)
.then(checkStatus, handleError)
.then(closeIndex, handleError)
.then(putSettings, handleError)
.then(putMapping, handleError)
.then(openIndex, handleError);
}
function deleteIndex() {
console.log('Deleting old index ...');
return client.indices.delete({
index,
ignore: [404]
}).then(handleResolve);
}
function createIndex() {
console.log('Creating new index ...');
return client.indices.create({
index,
body: {
settings: {
index: {
number_of_replicas: 0
}
}
}
}).then(handleResolve);
}
function checkStatus() {
console.log('Checking status ...');
return client.cluster.health({
index
}).then(handleResolve);
}
function closeIndex() {
console.log('Closing index ...');
return client.indices.close({
index
}).then(handleResolve);
}
function putSettings() {
console.log('Put settings ...');
return client.indices.putSettings({
index,
type,
body: {
settings: {
analysis: {
analyzer: {
folding: {
tokenizer: 'standard',
filter: ['lowercase', 'asciifolding']
}
}
}
}
}
}).then(handleResolve);
}
function putMapping() {
console.log('Put mapping ...');
return client.indices.putMapping({
index,
type,
body: {
properties: {
body: {
type: 'string',
analyzer: 'standard',
fields: {
folded: {
type: 'string',
analyzer: 'folding'
}
}
}
}
}
}).then(handleResolve);
}
function openIndex() {
console.log('Open index ...');
return client.indices.open({
index
}).then(handleResolve);
}
function handleResolve(body) {
if (!body.error) {
console.log('\x1b[32m' + 'Success' + '\x1b[0m');
} else {
console.log('\x1b[33m' + 'Failed' + '\x1b[0m');
}
return Promise.resolve();
}
function handleError(err) {
console.error(JSON.stringify(err.body, null, 2));
return Promise.reject();
}
module.exports = { init };
})();