forked from elastic/makelogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_createIndex.js
161 lines (153 loc) · 3.68 KB
/
_createIndex.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
'use strict';
var argv = require('./argv');
var client = require('./_client');
var omitFields = require('./_omitFields');
var confirmReset = require('./_confirmReset');
var argv = require('./argv');
module.exports = function createIndex() {
var indexTemplate = argv.indexPrefix + '*';
var indexTemplateName = 'makelogs_index_template__' + argv.indexPrefix;
var body = {
...(
argv.indexTemplatesV1
? { template: indexTemplate }
: { index_patterns: indexTemplate }
),
settings: {
index: {
number_of_shards: argv.shards,
number_of_replicas: argv.replicas,
},
analysis: {
analyzer: {
makelogs_url: {
type: 'standard',
tokenizer: 'uax_url_email',
max_token_length: 1000
}
}
}
},
mappings: {
// properties
properties: omitFields({
'@timestamp': {
type: 'date'
},
id: {
type: 'integer',
index: 'true',
},
clientip: {
type: 'ip'
},
ip: {
type: 'ip'
},
memory: {
type: 'double'
},
referer: {
type: 'keyword',
},
geo: {
properties: {
srcdest: {
type: 'keyword',
},
dest: {
type: 'keyword',
},
src: {
type: 'keyword',
},
coordinates: {
type: 'geo_point'
}
}
},
meta: {
properties: {
related: {
type: 'text',
},
char: {
type: 'keyword',
},
user: {
properties: {
firstname: {
type: 'text',
},
lastname: {
type: 'integer',
index: 'true'
}
}
}
}
}
}, true)
}
};
if (argv.types) {
body.mappings = {
_doc: body.mappings
}
}
return client.usable
.then(async () => ({
template: await client.indices.existsTemplate({
name: indexTemplateName
}),
indices: await client.indices.exists({
index: indexTemplate,
allowNoIndices: false
})
}))
.then(async function (exists) {
async function clearExisting() {
console.log('clearing existing "%s" index templates and indices', indexTemplate);
const existingIndicesRsp = await client.indices.getAlias({index: indexTemplate, ignore: 404});
const existingIndices = Object.keys(existingIndicesRsp || {});
return Promise.all([
client.indices.deleteTemplate({
ignore: 404,
name: indexTemplateName
}),
...existingIndices.map(indexName => {
console.log(`deleting ${indexName}`);
return client.indices.delete({
ignore: 404,
index: indexName
})
})
]);
}
function create() {
console.log('creating index template for "%s"', indexTemplate);
return client.indices.putTemplate({
name: indexTemplateName,
body: body
});
}
function maybeReset(reset) {
switch (reset) {
case true:
return clearExisting().then(create);
case false:
if (!exists.indices) {
return create();
}
return; // do nothing, index template exists
default:
return confirmReset(argv).then(maybeReset);
}
}
if (exists.template || exists.indices) {
return maybeReset(argv.reset);
} else {
return create();
}
});
};