-
Notifications
You must be signed in to change notification settings - Fork 981
/
Copy pathmappings.js
354 lines (305 loc) · 10.3 KB
/
mappings.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
346
347
348
349
350
351
352
353
354
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import $ from 'jquery';
import _ from 'lodash';
import * as opensearch from '../opensearch/opensearch';
// NOTE: If this value ever changes to be a few seconds or less, it might introduce flakiness
// due to timing issues in our app.js tests.
const POLL_INTERVAL = 60000;
let pollTimeoutId;
let perIndexTypes = {};
let perAliasIndexes = [];
let templates = [];
const mappingObj = {};
export function expandAliases(indicesOrAliases) {
// takes a list of indices or aliases or a string which may be either and returns a list of indices
// returns a list for multiple values or a string for a single.
if (!indicesOrAliases) {
return indicesOrAliases;
}
if (typeof indicesOrAliases === 'string') {
indicesOrAliases = [indicesOrAliases];
}
indicesOrAliases = $.map(indicesOrAliases, function (iOrA) {
if (perAliasIndexes[iOrA]) {
return perAliasIndexes[iOrA];
}
return [iOrA];
});
let ret = [].concat.apply([], indicesOrAliases);
ret.sort();
let last;
ret = $.map(ret, function (v) {
const r = last === v ? null : v;
last = v;
return r;
});
return ret.length > 1 ? ret : ret[0];
}
export function getTemplates() {
return [...templates];
}
export function getFields(indices, types) {
// get fields for indices and types. Both can be a list, a string or null (meaning all).
let ret = [];
indices = expandAliases(indices);
if (typeof indices === 'string') {
const typeDict = perIndexTypes[indices];
if (!typeDict) {
return [];
}
if (typeof types === 'string') {
const f = typeDict[types];
ret = f ? f : [];
} else {
// filter what we need
$.each(typeDict, function (type, fields) {
if (!types || types.length === 0 || $.inArray(type, types) !== -1) {
ret.push(fields);
}
});
ret = [].concat.apply([], ret);
}
} else {
// multi index mode.
$.each(perIndexTypes, function (index) {
if (!indices || indices.length === 0 || $.inArray(index, indices) !== -1) {
ret.push(getFields(index, types));
}
});
ret = [].concat.apply([], ret);
}
return _.uniqBy(ret, function (f) {
return f.name + ':' + f.type;
});
}
export function getTypes(indices) {
let ret = [];
indices = expandAliases(indices);
if (typeof indices === 'string') {
const typeDict = perIndexTypes[indices];
if (!typeDict) {
return [];
}
// filter what we need
$.each(typeDict, function (type) {
ret.push(type);
});
} else {
// multi index mode.
$.each(perIndexTypes, function (index) {
if (!indices || $.inArray(index, indices) !== -1) {
ret.push(getTypes(index));
}
});
ret = [].concat.apply([], ret);
}
return _.uniq(ret);
}
export function getIndices(includeAliases) {
const ret = [];
$.each(perIndexTypes, function (index) {
ret.push(index);
});
if (typeof includeAliases === 'undefined' ? true : includeAliases) {
$.each(perAliasIndexes, function (alias) {
ret.push(alias);
});
}
return ret;
}
function getFieldNamesFromFieldMapping(fieldName, fieldMapping) {
if (fieldMapping.enabled === false) {
return [];
}
let nestedFields;
function applyPathSettings(nestedFieldNames) {
const pathType = fieldMapping.path || 'full';
if (pathType === 'full') {
return $.map(nestedFieldNames, function (f) {
f.name = fieldName + '.' + f.name;
return f;
});
}
return nestedFieldNames;
}
if (fieldMapping.properties) {
// derived object type
nestedFields = getFieldNamesFromProperties(fieldMapping.properties);
return applyPathSettings(nestedFields);
}
const fieldType = fieldMapping.type;
const ret = { name: fieldName, type: fieldType };
if (fieldMapping.index_name) {
ret.name = fieldMapping.index_name;
}
if (fieldMapping.fields) {
nestedFields = $.map(fieldMapping.fields, function (fieldMapping, fieldName) {
return getFieldNamesFromFieldMapping(fieldName, fieldMapping);
});
nestedFields = applyPathSettings(nestedFields);
nestedFields.unshift(ret);
return nestedFields;
}
return [ret];
}
function getFieldNamesFromProperties(properties = {}) {
const fieldList = $.map(properties, function (fieldMapping, fieldName) {
return getFieldNamesFromFieldMapping(fieldName, fieldMapping);
});
// deduping
return _.uniqBy(fieldList, function (f) {
return f.name + ':' + f.type;
});
}
function loadTemplates(templatesObject = {}) {
templates = Object.keys(templatesObject);
}
export function loadMappings(mappings) {
perIndexTypes = {};
$.each(mappings, function (index, indexMapping) {
const normalizedIndexMappings = {};
// Migrate 1.0.0 mappings. This format has changed, so we need to extract the underlying mapping.
if (indexMapping.mappings && _.keys(indexMapping).length === 1) {
indexMapping = indexMapping.mappings;
}
$.each(indexMapping, function (typeName, typeMapping) {
if (typeName === 'properties') {
const fieldList = getFieldNamesFromProperties(typeMapping);
normalizedIndexMappings[typeName] = fieldList;
} else {
normalizedIndexMappings[typeName] = [];
}
});
perIndexTypes[index] = normalizedIndexMappings;
});
}
export function loadAliases(aliases) {
perAliasIndexes = {};
$.each(aliases || {}, function (index, omdexAliases) {
// verify we have an index defined. useful when mapping loading is disabled
perIndexTypes[index] = perIndexTypes[index] || {};
$.each(omdexAliases.aliases || {}, function (alias) {
if (alias === index) {
return;
} // alias which is identical to index means no index.
let curAliases = perAliasIndexes[alias];
if (!curAliases) {
curAliases = [];
perAliasIndexes[alias] = curAliases;
}
curAliases.push(index);
});
});
perAliasIndexes._all = getIndices(false);
}
export function clear() {
perIndexTypes = {};
perAliasIndexes = {};
templates = [];
}
function retrieveSettings(settingsKey, settingsToRetrieve) {
const settingKeyToPathMap = {
fields: '_mapping',
indices: '_aliases',
templates: '_template',
};
// Fetch autocomplete info if setting is set to true, and if user has made changes.
if (settingsToRetrieve[settingsKey] === true) {
return opensearch.send('GET', settingKeyToPathMap[settingsKey], null);
} else {
const settingsPromise = new $.Deferred();
if (settingsToRetrieve[settingsKey] === false) {
// If the user doesn't want autocomplete suggestions, then clear any that exist
return settingsPromise.resolveWith(this, [[JSON.stringify({})]]);
} else {
// If the user doesn't want autocomplete suggestions, then clear any that exist
return settingsPromise.resolve();
}
}
}
// Retrieve all selected settings by default.
// TODO: We should refactor this to be easier to consume. Ideally this function should retrieve
// whatever settings are specified, otherwise just use the saved settings. This requires changing
// the behavior to not *clear* whatever settings have been unselected, but it's hard to tell if
// this is possible without altering the autocomplete behavior. These are the scenarios we need to
// support:
// 1. Manual refresh. Specify what we want. Fetch specified, leave unspecified alone.
// 2. Changed selection and saved: Specify what we want. Fetch changed and selected, leave
// unchanged alone (both selected and unselected).
// 3. Poll: Use saved. Fetch selected. Ignore unselected.
export function clearSubscriptions() {
if (pollTimeoutId) {
clearTimeout(pollTimeoutId);
}
}
/**
*
* @param settings Settings A way to retrieve the current settings
* @param settingsToRetrieve any
*/
export function retrieveAutoCompleteInfo(settings, settingsToRetrieve) {
clearSubscriptions();
const mappingPromise = retrieveSettings('fields', settingsToRetrieve);
const aliasesPromise = retrieveSettings('indices', settingsToRetrieve);
const templatesPromise = retrieveSettings('templates', settingsToRetrieve);
$.when(mappingPromise, aliasesPromise, templatesPromise).done((mappings, aliases, templates) => {
let mappingsResponse;
if (mappings) {
const maxMappingSize = mappings[0].length > 10 * 1024 * 1024;
if (maxMappingSize) {
console.warn(
`Mapping size is larger than 10MB (${mappings[0].length / 1024 / 1024} MB). Ignoring...`
);
mappingsResponse = '[{}]';
} else {
mappingsResponse = mappings[0];
}
loadMappings(JSON.parse(mappingsResponse));
}
if (aliases) {
loadAliases(JSON.parse(aliases[0]));
}
if (templates) {
loadTemplates(JSON.parse(templates[0]));
}
if (mappings && aliases) {
// Trigger an update event with the mappings, aliases
$(mappingObj).trigger('update', [mappingsResponse, aliases[0]]);
}
// Schedule next request.
pollTimeoutId = setTimeout(() => {
// This looks strange/inefficient, but it ensures correct behavior because we don't want to send
// a scheduled request if the user turns off polling.
if (settings.getPolling()) {
retrieveAutoCompleteInfo(settings, settings.getAutocomplete());
}
}, POLL_INTERVAL);
});
}