@terascope/elasticsearch-api 4.12.3 → 4.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.d.ts +70 -0
- package/dist/src/index.d.ts.map +1 -0
- package/{index.js → dist/src/index.js} +334 -550
- package/dist/src/index.js.map +1 -0
- package/package.json +13 -3
- package/jest.config.js +0 -8
- package/test/api-spec.js +0 -1265
- package/test/bulk-send-dlq-spec.js +0 -96
- package/test/bulk-send-limit-spec.js +0 -49
- package/test/retry-spec.js +0 -36
- package/types/index.d.ts +0 -81
package/test/api-spec.js
DELETED
|
@@ -1,1265 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
debugLogger, cloneDeep, DataEntity,
|
|
3
|
-
isEmpty, pDelay
|
|
4
|
-
} from '@terascope/utils';
|
|
5
|
-
import esApi from '../index.js';
|
|
6
|
-
|
|
7
|
-
describe('elasticsearch-api', () => {
|
|
8
|
-
let recordsReturned = [];
|
|
9
|
-
let mgetQuery;
|
|
10
|
-
let bulkData;
|
|
11
|
-
let searchQuery;
|
|
12
|
-
let indexQuery;
|
|
13
|
-
let createQuery;
|
|
14
|
-
let updateQuery;
|
|
15
|
-
let removeQuery;
|
|
16
|
-
let failed = 0;
|
|
17
|
-
let failures = [];
|
|
18
|
-
let total = 0;
|
|
19
|
-
let bulkError = false;
|
|
20
|
-
let searchError = false;
|
|
21
|
-
let elasticDown = false;
|
|
22
|
-
let recoverError = false;
|
|
23
|
-
let changeMappings = false;
|
|
24
|
-
let putTemplateCalled = false;
|
|
25
|
-
let reindexCalled = false;
|
|
26
|
-
let isExecutionTemplate = false;
|
|
27
|
-
let indexAlreadyExists = true;
|
|
28
|
-
let indicesDeleteCalled = false;
|
|
29
|
-
let indicesPutAliasCalled = false;
|
|
30
|
-
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
searchQuery = null;
|
|
33
|
-
searchError = false;
|
|
34
|
-
failed = 0;
|
|
35
|
-
failures = [];
|
|
36
|
-
bulkError = false;
|
|
37
|
-
elasticDown = false;
|
|
38
|
-
recoverError = false;
|
|
39
|
-
changeMappings = false;
|
|
40
|
-
putTemplateCalled = false;
|
|
41
|
-
reindexCalled = false;
|
|
42
|
-
isExecutionTemplate = false;
|
|
43
|
-
indexAlreadyExists = true;
|
|
44
|
-
indicesDeleteCalled = false;
|
|
45
|
-
indicesPutAliasCalled = false;
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
function getData() {
|
|
49
|
-
return {
|
|
50
|
-
_shards: {
|
|
51
|
-
failed,
|
|
52
|
-
failures
|
|
53
|
-
},
|
|
54
|
-
hits: {
|
|
55
|
-
total,
|
|
56
|
-
hits: recordsReturned
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function getMGetData() {
|
|
62
|
-
return {
|
|
63
|
-
docs: recordsReturned
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function waitFor(time, fn) {
|
|
68
|
-
await pDelay(time);
|
|
69
|
-
if (fn) {
|
|
70
|
-
fn();
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function postedData(action, id) {
|
|
76
|
-
const result = {
|
|
77
|
-
_index: 'bigdata7',
|
|
78
|
-
_type: 'events',
|
|
79
|
-
_id: id || 'AWKWOrWojTNwAyqyzq5l',
|
|
80
|
-
_version: 1,
|
|
81
|
-
result: action,
|
|
82
|
-
_shards: { total: 2, successful: 1, failed: 0 }
|
|
83
|
-
};
|
|
84
|
-
if (action === 'created') result.created = true;
|
|
85
|
-
if (action === 'deleted') result.found = true;
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const template = {
|
|
90
|
-
template: 'test*',
|
|
91
|
-
settings: {
|
|
92
|
-
'index.number_of_shards': 5,
|
|
93
|
-
'index.number_of_replicas': 1
|
|
94
|
-
},
|
|
95
|
-
mappings: {
|
|
96
|
-
state: {
|
|
97
|
-
_all: {
|
|
98
|
-
enabled: true
|
|
99
|
-
},
|
|
100
|
-
dynamic: 'false',
|
|
101
|
-
properties: {
|
|
102
|
-
ip: {
|
|
103
|
-
type: 'string',
|
|
104
|
-
index: 'not_analyzed'
|
|
105
|
-
},
|
|
106
|
-
userAgent: {
|
|
107
|
-
type: 'string',
|
|
108
|
-
index: 'not_analyzed'
|
|
109
|
-
},
|
|
110
|
-
url: {
|
|
111
|
-
type: 'string',
|
|
112
|
-
index: 'not_analyzed'
|
|
113
|
-
},
|
|
114
|
-
uuid: {
|
|
115
|
-
type: 'string',
|
|
116
|
-
index: 'not_analyzed'
|
|
117
|
-
},
|
|
118
|
-
created: {
|
|
119
|
-
type: 'date'
|
|
120
|
-
},
|
|
121
|
-
ipv6: {
|
|
122
|
-
type: 'string',
|
|
123
|
-
index: 'not_analyzed'
|
|
124
|
-
},
|
|
125
|
-
location: {
|
|
126
|
-
type: 'geo_point'
|
|
127
|
-
},
|
|
128
|
-
bytes: {
|
|
129
|
-
type: 'integer'
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const template2 = {
|
|
137
|
-
template: 'test*',
|
|
138
|
-
settings: {
|
|
139
|
-
'index.number_of_shards': 5,
|
|
140
|
-
'index.number_of_replicas': 1
|
|
141
|
-
},
|
|
142
|
-
mappings: {
|
|
143
|
-
ex: {
|
|
144
|
-
_all: {
|
|
145
|
-
enabled: true
|
|
146
|
-
},
|
|
147
|
-
dynamic: 'false',
|
|
148
|
-
properties: {
|
|
149
|
-
ip: {
|
|
150
|
-
type: 'string',
|
|
151
|
-
index: 'not_analyzed'
|
|
152
|
-
},
|
|
153
|
-
userAgent: {
|
|
154
|
-
type: 'string',
|
|
155
|
-
index: 'not_analyzed'
|
|
156
|
-
},
|
|
157
|
-
url: {
|
|
158
|
-
type: 'string',
|
|
159
|
-
index: 'not_analyzed'
|
|
160
|
-
},
|
|
161
|
-
uuid: {
|
|
162
|
-
type: 'string',
|
|
163
|
-
index: 'not_analyzed'
|
|
164
|
-
},
|
|
165
|
-
created: {
|
|
166
|
-
type: 'date'
|
|
167
|
-
},
|
|
168
|
-
ipv6: {
|
|
169
|
-
type: 'string',
|
|
170
|
-
index: 'not_analyzed'
|
|
171
|
-
},
|
|
172
|
-
location: {
|
|
173
|
-
type: 'geo_point'
|
|
174
|
-
},
|
|
175
|
-
bytes: {
|
|
176
|
-
type: 'integer'
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
function getRecoveryData(index) {
|
|
184
|
-
const obj = {};
|
|
185
|
-
obj[index] = {
|
|
186
|
-
shards: [{ shard: 1, primary: true, stage: recoverError ? 'notdone' : 'DONE' }]
|
|
187
|
-
};
|
|
188
|
-
return obj;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function createBulkResponse(results) {
|
|
192
|
-
const response = { took: 22, errors: false, items: results };
|
|
193
|
-
if (!isEmpty(bulkError)) {
|
|
194
|
-
response.errors = true;
|
|
195
|
-
let i = -1;
|
|
196
|
-
response.items = results.body.flatMap((obj) => {
|
|
197
|
-
if (!obj.index && !obj.update && !obj.create && !obj.delete) {
|
|
198
|
-
// ignore the non-metadata objects
|
|
199
|
-
return [];
|
|
200
|
-
}
|
|
201
|
-
i++;
|
|
202
|
-
const [key, value] = Object.entries(obj)[0];
|
|
203
|
-
return [{
|
|
204
|
-
[key]: {
|
|
205
|
-
_index: value._index,
|
|
206
|
-
_type: value._type,
|
|
207
|
-
_id: String(i),
|
|
208
|
-
_version: 1,
|
|
209
|
-
result: `${key}d`,
|
|
210
|
-
error: { type: bulkError[i] || 'someType', reason: 'someReason' },
|
|
211
|
-
_shards: {
|
|
212
|
-
total: 2,
|
|
213
|
-
successful: 1,
|
|
214
|
-
failed: 0
|
|
215
|
-
},
|
|
216
|
-
status: 400,
|
|
217
|
-
_seq_no: 2,
|
|
218
|
-
_primary_term: 3
|
|
219
|
-
}
|
|
220
|
-
}];
|
|
221
|
-
});
|
|
222
|
-
} else {
|
|
223
|
-
response.errors = false;
|
|
224
|
-
let i = -1;
|
|
225
|
-
response.items = results.body.flatMap((obj) => {
|
|
226
|
-
if (!obj.index && !obj.update && !obj.create && !obj.delete) {
|
|
227
|
-
// ignore the non-metadata objects
|
|
228
|
-
return [];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
i++;
|
|
232
|
-
const [key, value] = Object.entries(obj)[0];
|
|
233
|
-
return [{
|
|
234
|
-
[key]: {
|
|
235
|
-
_index: value._index,
|
|
236
|
-
_type: value._type,
|
|
237
|
-
_id: String(i),
|
|
238
|
-
_version: 1,
|
|
239
|
-
result: `${key}d`,
|
|
240
|
-
_shards: {
|
|
241
|
-
total: 2,
|
|
242
|
-
successful: 1,
|
|
243
|
-
failed: 0
|
|
244
|
-
},
|
|
245
|
-
status: 200,
|
|
246
|
-
_seq_no: 2,
|
|
247
|
-
_primary_term: 3
|
|
248
|
-
}
|
|
249
|
-
}];
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
return response;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function simulateTemplateResponse(originalMapping, index, recordType) {
|
|
256
|
-
const results = {};
|
|
257
|
-
results[index] = { mappings: JSON.parse(JSON.stringify(originalMapping.mappings)) };
|
|
258
|
-
// simulate the 'false' to false issue
|
|
259
|
-
results[index].mappings[recordType].dynamic = 'false';
|
|
260
|
-
if (changeMappings) {
|
|
261
|
-
results[index].mappings[recordType].properties.newKey = { type: 'keyword' };
|
|
262
|
-
}
|
|
263
|
-
return results;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const logger = debugLogger('elasticsearch-api-spec');
|
|
267
|
-
const client = {
|
|
268
|
-
// set this so we can verify the index
|
|
269
|
-
transport: {
|
|
270
|
-
closed: false,
|
|
271
|
-
requestTimeout: 50,
|
|
272
|
-
connectionPool: {
|
|
273
|
-
_conns: {
|
|
274
|
-
alive: [{}],
|
|
275
|
-
dead: [{}]
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
mget: (query) => {
|
|
280
|
-
mgetQuery = query;
|
|
281
|
-
return Promise.resolve(getMGetData());
|
|
282
|
-
},
|
|
283
|
-
// let getQuery;
|
|
284
|
-
// let indexQuery;
|
|
285
|
-
// let createQuery;
|
|
286
|
-
// let updateQuery;
|
|
287
|
-
// let deleteQuery;
|
|
288
|
-
get: () => Promise.resolve(recordsReturned[0]),
|
|
289
|
-
index: (query) => {
|
|
290
|
-
indexQuery = query;
|
|
291
|
-
return Promise.resolve(postedData('created'));
|
|
292
|
-
},
|
|
293
|
-
create: (obj) => {
|
|
294
|
-
createQuery = obj;
|
|
295
|
-
return Promise.resolve(postedData('created', obj.id));
|
|
296
|
-
},
|
|
297
|
-
update: (query) => {
|
|
298
|
-
updateQuery = query;
|
|
299
|
-
return Promise.resolve(postedData('updated'));
|
|
300
|
-
},
|
|
301
|
-
delete: (query) => {
|
|
302
|
-
removeQuery = query;
|
|
303
|
-
return Promise.resolve(postedData('deleted'));
|
|
304
|
-
},
|
|
305
|
-
bulk: (data) => {
|
|
306
|
-
bulkData = data;
|
|
307
|
-
return Promise.resolve(createBulkResponse(data));
|
|
308
|
-
},
|
|
309
|
-
search: (_query) => {
|
|
310
|
-
searchQuery = _query;
|
|
311
|
-
logger.debug(searchQuery);
|
|
312
|
-
if (searchError) return Promise.reject(searchError);
|
|
313
|
-
return Promise.resolve(getData());
|
|
314
|
-
},
|
|
315
|
-
reindex: () => {
|
|
316
|
-
reindexCalled = true;
|
|
317
|
-
return Promise.resolve(true);
|
|
318
|
-
},
|
|
319
|
-
indices: {
|
|
320
|
-
exists: () => {
|
|
321
|
-
if (elasticDown) return Promise.reject(new Error('Elasticsearch is down'));
|
|
322
|
-
return Promise.resolve(indexAlreadyExists);
|
|
323
|
-
},
|
|
324
|
-
create: () => Promise.resolve({ acknowledged: true, shards_acknowledged: true }),
|
|
325
|
-
refresh: () => Promise.resolve({ _shards: { total: 10, successful: 5, failed: 0 } }),
|
|
326
|
-
recovery: (query) => Promise.resolve(getRecoveryData(query.index)),
|
|
327
|
-
getSettings: () => {
|
|
328
|
-
const obj = {};
|
|
329
|
-
obj.some_index = { settings: { index: { max_result_window: 1000000 } } };
|
|
330
|
-
return Promise.resolve(obj);
|
|
331
|
-
},
|
|
332
|
-
putTemplate: () => {
|
|
333
|
-
putTemplateCalled = true;
|
|
334
|
-
return Promise.resolve({ acknowledged: true });
|
|
335
|
-
},
|
|
336
|
-
delete: () => {
|
|
337
|
-
indicesDeleteCalled = true;
|
|
338
|
-
return Promise.resolve(true);
|
|
339
|
-
},
|
|
340
|
-
putAlias: () => {
|
|
341
|
-
indicesPutAliasCalled = true;
|
|
342
|
-
return Promise.resolve(true);
|
|
343
|
-
},
|
|
344
|
-
getMapping: () => {
|
|
345
|
-
let index = 'teracluster__state';
|
|
346
|
-
let type = 'state';
|
|
347
|
-
let templateArg = template;
|
|
348
|
-
if (isExecutionTemplate) {
|
|
349
|
-
index = 'teracluster__ex';
|
|
350
|
-
type = 'ex';
|
|
351
|
-
templateArg = template2;
|
|
352
|
-
indexAlreadyExists = false;
|
|
353
|
-
}
|
|
354
|
-
return Promise.resolve(simulateTemplateResponse(templateArg, index, type));
|
|
355
|
-
}
|
|
356
|
-
},
|
|
357
|
-
nodes: {
|
|
358
|
-
info: () => Promise.resolve({ _nodes: {} }),
|
|
359
|
-
stats: () => Promise.resolve({ _nodes: {} })
|
|
360
|
-
},
|
|
361
|
-
cluster: {
|
|
362
|
-
stats: () => Promise.resolve({ nodes: { versions: ['6.8.1'] } })
|
|
363
|
-
},
|
|
364
|
-
__testing: {
|
|
365
|
-
start: 100,
|
|
366
|
-
limit: 500
|
|
367
|
-
}
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
it('can instantiate', () => {
|
|
371
|
-
let api;
|
|
372
|
-
expect(() => {
|
|
373
|
-
api = esApi(client, logger);
|
|
374
|
-
}).not.toThrow();
|
|
375
|
-
expect(typeof api).toEqual('object');
|
|
376
|
-
expect(typeof api.search).toEqual('function');
|
|
377
|
-
expect(typeof api.count).toEqual('function');
|
|
378
|
-
expect(typeof api.get).toEqual('function');
|
|
379
|
-
expect(typeof api.index).toEqual('function');
|
|
380
|
-
expect(typeof api.indexWithId).toEqual('function');
|
|
381
|
-
expect(typeof api.create).toEqual('function');
|
|
382
|
-
expect(typeof api.update).toEqual('function');
|
|
383
|
-
expect(typeof api.remove).toEqual('function');
|
|
384
|
-
expect(typeof api.version).toEqual('function');
|
|
385
|
-
expect(typeof api.putTemplate).toEqual('function');
|
|
386
|
-
expect(typeof api.bulkSend).toEqual('function');
|
|
387
|
-
expect(typeof api.nodeInfo).toEqual('function');
|
|
388
|
-
expect(typeof api.nodeStats).toEqual('function');
|
|
389
|
-
expect(typeof api.buildQuery).toEqual('function');
|
|
390
|
-
expect(typeof api.indexExists).toEqual('function');
|
|
391
|
-
expect(typeof api.indexCreate).toEqual('function');
|
|
392
|
-
expect(typeof api.indexRefresh).toEqual('function');
|
|
393
|
-
expect(typeof api.indexRecovery).toEqual('function');
|
|
394
|
-
expect(typeof api.indexSetup).toEqual('function');
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it('count returns total amount for query', async () => {
|
|
398
|
-
const query = { body: 'someQuery' };
|
|
399
|
-
const api = esApi(client, logger);
|
|
400
|
-
|
|
401
|
-
const results = await api.count(query);
|
|
402
|
-
expect(query).toEqual({ body: 'someQuery', size: 0 });
|
|
403
|
-
expect(results).toEqual(0);
|
|
404
|
-
total = 500;
|
|
405
|
-
return expect(api.count(query)).resolves.toEqual(500);
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
it('can search', async () => {
|
|
409
|
-
const query = { body: 'someQuery' };
|
|
410
|
-
const api = esApi(client, logger);
|
|
411
|
-
const apiFullResponse = esApi(client, logger, { full_response: true });
|
|
412
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
413
|
-
|
|
414
|
-
const [results1, results2] = await Promise.all([
|
|
415
|
-
api.search(query),
|
|
416
|
-
apiFullResponse.search(query)
|
|
417
|
-
]);
|
|
418
|
-
|
|
419
|
-
expect(results1).toEqual([recordsReturned[0]._source]);
|
|
420
|
-
expect(results1).toEqual([{ some: 'data' }]);
|
|
421
|
-
expect(DataEntity.isDataEntity(results1[0])).toEqual(true);
|
|
422
|
-
expect(results2).toEqual(getData());
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
it('search can handle rejection errors', async () => {
|
|
426
|
-
const query = { body: 'someQuery' };
|
|
427
|
-
const api = esApi(client, logger);
|
|
428
|
-
let queryFailed = false;
|
|
429
|
-
searchError = { body: { error: { type: 'es_rejected_execution_exception' } } };
|
|
430
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
431
|
-
|
|
432
|
-
const [results] = await Promise.all([
|
|
433
|
-
api.search(query),
|
|
434
|
-
waitFor(50, () => {
|
|
435
|
-
searchError = false;
|
|
436
|
-
})
|
|
437
|
-
]);
|
|
438
|
-
expect(results).toEqual([recordsReturned[0]._source]);
|
|
439
|
-
searchError = { body: { error: { type: 'some_thing_else' } } };
|
|
440
|
-
try {
|
|
441
|
-
await api.search(query);
|
|
442
|
-
} catch (_err) {
|
|
443
|
-
queryFailed = true;
|
|
444
|
-
}
|
|
445
|
-
return expect(queryFailed).toEqual(true);
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
it('search can handle shard errors', async () => {
|
|
449
|
-
const query = { body: 'someQuery' };
|
|
450
|
-
const api = esApi(client, logger);
|
|
451
|
-
let queryFailed = false;
|
|
452
|
-
failed = 3;
|
|
453
|
-
failures = [{ reason: { type: 'es_rejected_execution_exception' } }];
|
|
454
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
455
|
-
|
|
456
|
-
const [results] = await Promise.all([
|
|
457
|
-
api.search(query),
|
|
458
|
-
waitFor(20, () => {
|
|
459
|
-
failed = 0;
|
|
460
|
-
failures = [];
|
|
461
|
-
})
|
|
462
|
-
]);
|
|
463
|
-
|
|
464
|
-
expect(results).toEqual([recordsReturned[0]._source]);
|
|
465
|
-
failed = 4;
|
|
466
|
-
failures = [{ reason: { type: 'some other error' } }];
|
|
467
|
-
try {
|
|
468
|
-
await Promise.all([
|
|
469
|
-
api.search(query),
|
|
470
|
-
waitFor(50, () => {
|
|
471
|
-
failed = 0;
|
|
472
|
-
failures = [];
|
|
473
|
-
})
|
|
474
|
-
]);
|
|
475
|
-
} catch (_err) {
|
|
476
|
-
queryFailed = true;
|
|
477
|
-
}
|
|
478
|
-
return expect(queryFailed).toEqual(true);
|
|
479
|
-
});
|
|
480
|
-
|
|
481
|
-
it('can call mget', async () => {
|
|
482
|
-
const query = {
|
|
483
|
-
index: 'someIndex',
|
|
484
|
-
type: 'someType',
|
|
485
|
-
body: { ids: ['id1', 'id2'] }
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
const api = esApi(client, logger);
|
|
489
|
-
|
|
490
|
-
recordsReturned = [
|
|
491
|
-
{
|
|
492
|
-
_index: 'someIndex',
|
|
493
|
-
_id: 'id1',
|
|
494
|
-
found: true,
|
|
495
|
-
_source: { some: 'data' }
|
|
496
|
-
},
|
|
497
|
-
{
|
|
498
|
-
found: false,
|
|
499
|
-
_source: { some: 'notFounddata' }
|
|
500
|
-
}
|
|
501
|
-
];
|
|
502
|
-
|
|
503
|
-
const results = await api.mget(query);
|
|
504
|
-
|
|
505
|
-
expect(results.length).toEqual(1);
|
|
506
|
-
expect(results).toEqual([{ some: 'data' }]);
|
|
507
|
-
expect(DataEntity.isDataEntity(results[0])).toEqual(true);
|
|
508
|
-
expect(results[0].getMetadata()).toMatchObject({ _index: 'someIndex', _key: 'id1' });
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
it('mget removes type for es7 indices', async () => {
|
|
512
|
-
const query = {
|
|
513
|
-
index: 'someIndex',
|
|
514
|
-
type: 'someType',
|
|
515
|
-
body: { ids: ['id1', 'id2'] }
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const es7client = cloneDeep(client);
|
|
519
|
-
|
|
520
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
521
|
-
|
|
522
|
-
const api = esApi(es7client, logger);
|
|
523
|
-
|
|
524
|
-
recordsReturned = [
|
|
525
|
-
{
|
|
526
|
-
_index: 'someIndex',
|
|
527
|
-
_id: 'id1',
|
|
528
|
-
found: true,
|
|
529
|
-
_source: { some: 'data' }
|
|
530
|
-
},
|
|
531
|
-
{
|
|
532
|
-
found: false,
|
|
533
|
-
_source: { some: 'notFounddata' }
|
|
534
|
-
}
|
|
535
|
-
];
|
|
536
|
-
|
|
537
|
-
const results = await api.mget(query);
|
|
538
|
-
|
|
539
|
-
expect(mgetQuery).toEqual({
|
|
540
|
-
index: 'someIndex',
|
|
541
|
-
body: { ids: ['id1', 'id2'] }
|
|
542
|
-
});
|
|
543
|
-
|
|
544
|
-
expect(results.length).toEqual(1);
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
it('can call get', async () => {
|
|
548
|
-
const query = { body: 'someQuery' };
|
|
549
|
-
const api = esApi(client, logger);
|
|
550
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
551
|
-
|
|
552
|
-
const results = await api.get(query);
|
|
553
|
-
expect(results).toEqual({ some: 'data' });
|
|
554
|
-
expect(DataEntity.isDataEntity(results)).toEqual(true);
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
it('can call index', async () => {
|
|
558
|
-
const query = { index: 'someIndex', type: 'sometype', body: 'someQuery' };
|
|
559
|
-
const api = esApi(client, logger);
|
|
560
|
-
|
|
561
|
-
const results = await api.index(query);
|
|
562
|
-
return expect(results.created).toEqual(true);
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
it('removes types in index request for es7', async () => {
|
|
566
|
-
const query = { index: 'someIndex', type: 'sometype', body: 'someQuery' };
|
|
567
|
-
|
|
568
|
-
const es7client = cloneDeep(client);
|
|
569
|
-
|
|
570
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
571
|
-
|
|
572
|
-
const api = await esApi(es7client, logger);
|
|
573
|
-
|
|
574
|
-
await api.index(query);
|
|
575
|
-
|
|
576
|
-
expect(indexQuery).toEqual({ index: 'someIndex', body: 'someQuery' });
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
it('can call indexWithId', async () => {
|
|
580
|
-
const query = {
|
|
581
|
-
index: 'someIndex',
|
|
582
|
-
id: 'someId',
|
|
583
|
-
type: 'sometype',
|
|
584
|
-
body: 'someQuery'
|
|
585
|
-
};
|
|
586
|
-
const api = esApi(client, logger);
|
|
587
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
588
|
-
|
|
589
|
-
const results = await api.indexWithId(query);
|
|
590
|
-
return expect(results).toEqual(query.body);
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
it('can remove type from indexWithId for es7', async () => {
|
|
594
|
-
const query = {
|
|
595
|
-
index: 'someIndex',
|
|
596
|
-
id: 'someId',
|
|
597
|
-
type: 'sometype',
|
|
598
|
-
body: 'someQuery'
|
|
599
|
-
};
|
|
600
|
-
|
|
601
|
-
const es7client = cloneDeep(client);
|
|
602
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
603
|
-
const api = esApi(es7client, logger);
|
|
604
|
-
|
|
605
|
-
recordsReturned = [{ _source: { some: 'data' } }];
|
|
606
|
-
|
|
607
|
-
await api.indexWithId(query);
|
|
608
|
-
|
|
609
|
-
expect(indexQuery).toEqual({
|
|
610
|
-
index: 'someIndex',
|
|
611
|
-
id: 'someId',
|
|
612
|
-
body: 'someQuery'
|
|
613
|
-
});
|
|
614
|
-
});
|
|
615
|
-
|
|
616
|
-
it('can call create', async () => {
|
|
617
|
-
const query = { index: 'someIndex', type: 'sometype', body: 'someQuery' };
|
|
618
|
-
const api = esApi(client, logger);
|
|
619
|
-
|
|
620
|
-
const results = await api.create(query);
|
|
621
|
-
return expect(results).toEqual(query.body);
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
it('can remove type from create request for es7', async () => {
|
|
625
|
-
const query = { index: 'someIndex', type: 'sometype', body: 'someQuery' };
|
|
626
|
-
|
|
627
|
-
const es7client = cloneDeep(client);
|
|
628
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
629
|
-
const api = esApi(es7client, logger);
|
|
630
|
-
|
|
631
|
-
await api.create(query);
|
|
632
|
-
|
|
633
|
-
expect(createQuery).toEqual({ index: 'someIndex', body: 'someQuery' });
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
it('can call update', async () => {
|
|
637
|
-
const query = { index: 'someIndex', type: 'sometype', body: { doc: { some: 'data' } } };
|
|
638
|
-
const api = esApi(client, logger);
|
|
639
|
-
|
|
640
|
-
const results = await api.update(query);
|
|
641
|
-
return expect(results).toEqual(query.body.doc);
|
|
642
|
-
});
|
|
643
|
-
|
|
644
|
-
it('can remove type from update requests on es7', async () => {
|
|
645
|
-
const query = { index: 'someIndex', type: 'sometype', body: { doc: { some: 'data' } } };
|
|
646
|
-
|
|
647
|
-
const es7client = cloneDeep(client);
|
|
648
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
649
|
-
const api = esApi(es7client, logger);
|
|
650
|
-
|
|
651
|
-
await api.update(query);
|
|
652
|
-
expect(updateQuery).toEqual({ index: 'someIndex', body: { doc: { some: 'data' } } });
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
it('can call remove', async () => {
|
|
656
|
-
const query = { index: 'someIndex', type: 'sometype', id: 'someId' };
|
|
657
|
-
const api = esApi(client, logger);
|
|
658
|
-
|
|
659
|
-
const results = await api.remove(query);
|
|
660
|
-
return expect(results).toEqual(true);
|
|
661
|
-
});
|
|
662
|
-
|
|
663
|
-
it('can remove type from query on remove call on es7', async () => {
|
|
664
|
-
const query = { index: 'someIndex', type: 'sometype', id: 'someId' };
|
|
665
|
-
|
|
666
|
-
const es7client = cloneDeep(client);
|
|
667
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
668
|
-
const api = esApi(es7client, logger);
|
|
669
|
-
|
|
670
|
-
await api.remove(query);
|
|
671
|
-
|
|
672
|
-
expect(removeQuery).toEqual({ index: 'someIndex', id: 'someId' });
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
it('can call indexExists', async () => {
|
|
676
|
-
const query = { index: 'someIndex' };
|
|
677
|
-
const api = esApi(client, logger);
|
|
678
|
-
|
|
679
|
-
const results = await api.indexExists(query);
|
|
680
|
-
return expect(results).toEqual(true);
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
it('can call indexCreate', async () => {
|
|
684
|
-
const query = { index: 'someIndex', body: { mappings: {} } };
|
|
685
|
-
const api = esApi(client, logger);
|
|
686
|
-
|
|
687
|
-
const results = await api.indexCreate(query);
|
|
688
|
-
return expect(results.acknowledged).toEqual(true);
|
|
689
|
-
});
|
|
690
|
-
|
|
691
|
-
it('can call indexRefresh', async () => {
|
|
692
|
-
const query = { index: 'someIndex' };
|
|
693
|
-
const api = esApi(client, logger);
|
|
694
|
-
|
|
695
|
-
const results = await api.indexRefresh(query);
|
|
696
|
-
return expect(results).toBeTruthy();
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
it('can call indexRecovery', async () => {
|
|
700
|
-
const query = { index: 'someIndex' };
|
|
701
|
-
const api = esApi(client, logger);
|
|
702
|
-
|
|
703
|
-
const results = await api.indexRecovery(query);
|
|
704
|
-
return expect(results[query.index]).toBeTruthy();
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
it('can call nodeInfo', async () => {
|
|
708
|
-
const api = esApi(client, logger);
|
|
709
|
-
|
|
710
|
-
const results = await api.nodeInfo();
|
|
711
|
-
return expect(results).toBeTruthy();
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it('can call nodeStats', async () => {
|
|
715
|
-
const api = esApi(client, logger);
|
|
716
|
-
|
|
717
|
-
const results = await api.nodeStats();
|
|
718
|
-
return expect(results).toBeTruthy();
|
|
719
|
-
});
|
|
720
|
-
|
|
721
|
-
it('can warn window size with version', async () => {
|
|
722
|
-
const api = esApi(client, logger, { index: 'some_index' });
|
|
723
|
-
// FIXME: this test is only really testing a side effect, need a better test
|
|
724
|
-
expect(api.version).toBeFunction();
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
it('can call putTemplate', async () => {
|
|
728
|
-
const api = esApi(client, logger);
|
|
729
|
-
|
|
730
|
-
const results = await api.putTemplate(template, 'somename');
|
|
731
|
-
return expect(results.acknowledged).toEqual(true);
|
|
732
|
-
});
|
|
733
|
-
|
|
734
|
-
it('can call bulkSend', async () => {
|
|
735
|
-
const api = esApi(client, logger);
|
|
736
|
-
|
|
737
|
-
const result = await api.bulkSend([
|
|
738
|
-
{
|
|
739
|
-
action: {
|
|
740
|
-
index: { _index: 'some_index', _type: 'events', _id: 1 }
|
|
741
|
-
},
|
|
742
|
-
data: { title: 'foo' }
|
|
743
|
-
},
|
|
744
|
-
{
|
|
745
|
-
action: {
|
|
746
|
-
delete: { _index: 'some_index', _type: 'events', _id: 5 }
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
]);
|
|
750
|
-
expect(bulkData).toEqual({
|
|
751
|
-
body: [
|
|
752
|
-
{ index: { _index: 'some_index', _type: 'events', _id: 1 } },
|
|
753
|
-
{ title: 'foo' },
|
|
754
|
-
{ delete: { _index: 'some_index', _type: 'events', _id: 5 } }
|
|
755
|
-
]
|
|
756
|
-
});
|
|
757
|
-
return expect(result).toBe(2);
|
|
758
|
-
});
|
|
759
|
-
|
|
760
|
-
it('can remove type from bulkSend', async () => {
|
|
761
|
-
const es7client = cloneDeep(client);
|
|
762
|
-
|
|
763
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
764
|
-
|
|
765
|
-
const api = esApi(es7client, logger);
|
|
766
|
-
|
|
767
|
-
await api.bulkSend([{
|
|
768
|
-
action: {
|
|
769
|
-
index: { _index: 'some_index', _type: 'events', _id: 1 }
|
|
770
|
-
},
|
|
771
|
-
data: { title: 'foo' }
|
|
772
|
-
},
|
|
773
|
-
{
|
|
774
|
-
action: {
|
|
775
|
-
delete: { _index: 'some_index', _type: 'events', _id: 5 }
|
|
776
|
-
}
|
|
777
|
-
}]);
|
|
778
|
-
expect(bulkData).toEqual({
|
|
779
|
-
body: [
|
|
780
|
-
{ index: { _index: 'some_index', _id: 1 } },
|
|
781
|
-
{ title: 'foo' },
|
|
782
|
-
{ delete: { _index: 'some_index', _id: 5 } }
|
|
783
|
-
]
|
|
784
|
-
});
|
|
785
|
-
});
|
|
786
|
-
|
|
787
|
-
it('will not remove _type from record in a bulkSend', async () => {
|
|
788
|
-
const es7client = cloneDeep(client);
|
|
789
|
-
|
|
790
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
791
|
-
|
|
792
|
-
const api = esApi(es7client, logger);
|
|
793
|
-
|
|
794
|
-
await api.bulkSend([{
|
|
795
|
-
action: {
|
|
796
|
-
delete: { _index: 'some_index', _type: 'events', _id: 5 }
|
|
797
|
-
},
|
|
798
|
-
},
|
|
799
|
-
{
|
|
800
|
-
action: {
|
|
801
|
-
index: { _index: 'some_index', _type: 'events', _id: 1 }
|
|
802
|
-
},
|
|
803
|
-
data: { title: 'foo', _type: 'doc', name: 'joe' }
|
|
804
|
-
}]);
|
|
805
|
-
expect(bulkData).toEqual({
|
|
806
|
-
body: [
|
|
807
|
-
{ delete: { _index: 'some_index', _id: 5 } },
|
|
808
|
-
{ index: { _index: 'some_index', _id: 1 } },
|
|
809
|
-
{ title: 'foo', _type: 'doc', name: 'joe' }
|
|
810
|
-
]
|
|
811
|
-
});
|
|
812
|
-
});
|
|
813
|
-
|
|
814
|
-
it('will not err if no _type in es7 bulkSend request metadata', async () => {
|
|
815
|
-
const es7client = cloneDeep(client);
|
|
816
|
-
|
|
817
|
-
es7client.transport._config = { apiVersion: '7.0' };
|
|
818
|
-
|
|
819
|
-
const api = esApi(es7client, logger);
|
|
820
|
-
|
|
821
|
-
await api.bulkSend([{
|
|
822
|
-
action: {
|
|
823
|
-
delete: { _index: 'some_index', _type: 'events', _id: 5 }
|
|
824
|
-
},
|
|
825
|
-
},
|
|
826
|
-
{
|
|
827
|
-
action: {
|
|
828
|
-
index: { _index: 'some_index', _type: 'events', _id: 1 }
|
|
829
|
-
},
|
|
830
|
-
data: { title: 'foo', _type: 'doc', name: 'joe' }
|
|
831
|
-
}]);
|
|
832
|
-
|
|
833
|
-
expect(bulkData).toEqual({
|
|
834
|
-
body: [
|
|
835
|
-
{ delete: { _index: 'some_index', _id: 5 } },
|
|
836
|
-
{ index: { _index: 'some_index', _id: 1 } },
|
|
837
|
-
{ title: 'foo', _type: 'doc', name: 'joe' }
|
|
838
|
-
]
|
|
839
|
-
});
|
|
840
|
-
});
|
|
841
|
-
|
|
842
|
-
it('can call bulkSend with errors', async () => {
|
|
843
|
-
const api = esApi(client, logger);
|
|
844
|
-
const myBulkData = [{
|
|
845
|
-
action: {
|
|
846
|
-
index: { _index: 'some_index', _type: 'events', _id: 1 }
|
|
847
|
-
},
|
|
848
|
-
data: { title: 'foo' }
|
|
849
|
-
},
|
|
850
|
-
{
|
|
851
|
-
action: {
|
|
852
|
-
delete: { _index: 'some_index', _type: 'events', _id: 5 }
|
|
853
|
-
}
|
|
854
|
-
}];
|
|
855
|
-
|
|
856
|
-
bulkError = [
|
|
857
|
-
'es_rejected_execution_exception',
|
|
858
|
-
'es_rejected_execution_exception',
|
|
859
|
-
];
|
|
860
|
-
|
|
861
|
-
waitFor(20, () => {
|
|
862
|
-
bulkError = false;
|
|
863
|
-
});
|
|
864
|
-
const result = await api.bulkSend(myBulkData);
|
|
865
|
-
|
|
866
|
-
expect(result).toBe(2);
|
|
867
|
-
|
|
868
|
-
bulkError = ['some_thing_else', 'some_thing_else'];
|
|
869
|
-
|
|
870
|
-
await expect(
|
|
871
|
-
api.bulkSend(myBulkData)
|
|
872
|
-
).rejects.toThrow('bulk send error: some_thing_else--someReason');
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
it('can call buildQuery for geo queries', () => {
|
|
876
|
-
const api = esApi(client, logger);
|
|
877
|
-
|
|
878
|
-
const badOpConfig1 = {
|
|
879
|
-
index: 'some_index',
|
|
880
|
-
geo_field: 'some_field'
|
|
881
|
-
};
|
|
882
|
-
const badOpConfig2 = {
|
|
883
|
-
index: 'some_index',
|
|
884
|
-
geo_field: 'some_field',
|
|
885
|
-
geo_box_top_left: '34.5234,79.42345'
|
|
886
|
-
};
|
|
887
|
-
const badOpConfig3 = {
|
|
888
|
-
index: 'some_index',
|
|
889
|
-
geo_field: 'some_field',
|
|
890
|
-
geo_box_bottom_right: '54.5234,80.3456'
|
|
891
|
-
};
|
|
892
|
-
const badOpConfig4 = {
|
|
893
|
-
index: 'some_index',
|
|
894
|
-
geo_field: 'some_field',
|
|
895
|
-
geo_point: '67.2435,100.2345'
|
|
896
|
-
};
|
|
897
|
-
const badOpConfig5 = {
|
|
898
|
-
index: 'some_index',
|
|
899
|
-
geo_field: 'some_field',
|
|
900
|
-
geo_distance: '200km'
|
|
901
|
-
};
|
|
902
|
-
const badOpConfig6 = {
|
|
903
|
-
index: 'some_index',
|
|
904
|
-
geo_field: 'some_field',
|
|
905
|
-
geo_box_top_left: '34.5234,79.42345',
|
|
906
|
-
geo_point: '67.2435,100.2345'
|
|
907
|
-
};
|
|
908
|
-
const badOpConfig7 = {
|
|
909
|
-
index: 'some_index',
|
|
910
|
-
geo_field: 'some_field',
|
|
911
|
-
geo_box_top_left: '34.5234,79.42345',
|
|
912
|
-
geo_box_bottom_right: '54.5234,80.3456',
|
|
913
|
-
geo_sort_unit: 'm'
|
|
914
|
-
};
|
|
915
|
-
const badOpConfig8 = {
|
|
916
|
-
index: 'some_index',
|
|
917
|
-
geo_field: 'some_field',
|
|
918
|
-
geo_box_top_left: '34.5234,79.42345',
|
|
919
|
-
geo_box_bottom_right: '54.5234,80.3456',
|
|
920
|
-
geo_sort_order: 'asc'
|
|
921
|
-
};
|
|
922
|
-
|
|
923
|
-
const goodConfig1 = {
|
|
924
|
-
index: 'some_index',
|
|
925
|
-
geo_field: 'some_field',
|
|
926
|
-
geo_box_top_left: '34.5234,79.42345',
|
|
927
|
-
geo_box_bottom_right: '54.5234,80.3456'
|
|
928
|
-
};
|
|
929
|
-
const goodConfig2 = {
|
|
930
|
-
index: 'some_index',
|
|
931
|
-
date_field_name: 'created',
|
|
932
|
-
geo_field: 'some_field',
|
|
933
|
-
geo_box_top_left: '34.5234,79.42345',
|
|
934
|
-
geo_box_bottom_right: '54.5234,80.3456',
|
|
935
|
-
geo_sort_point: '52.3456,79.6784'
|
|
936
|
-
};
|
|
937
|
-
const goodConfig3 = {
|
|
938
|
-
index: 'some_index',
|
|
939
|
-
geo_field: 'some_field',
|
|
940
|
-
geo_distance: '200km',
|
|
941
|
-
geo_point: '67.2435,100.2345'
|
|
942
|
-
};
|
|
943
|
-
const goodConfig4 = {
|
|
944
|
-
index: 'some_index',
|
|
945
|
-
geo_field: 'some_field',
|
|
946
|
-
geo_distance: '200km',
|
|
947
|
-
geo_point: '67.2435,100.2345',
|
|
948
|
-
geo_sort_point: '52.3456,79.6784',
|
|
949
|
-
geo_sort_unit: 'km',
|
|
950
|
-
geo_sort_order: 'desc'
|
|
951
|
-
};
|
|
952
|
-
|
|
953
|
-
const msg1 = { count: 100 };
|
|
954
|
-
const msg2 = { count: 100, start: new Date(), end: new Date() };
|
|
955
|
-
|
|
956
|
-
function makeResponse(opConfig, msg, data, sort) {
|
|
957
|
-
const query = {
|
|
958
|
-
index: opConfig.index,
|
|
959
|
-
size: msg.count,
|
|
960
|
-
body: {
|
|
961
|
-
query: {
|
|
962
|
-
bool: {
|
|
963
|
-
must: Array.isArray(data) ? data : [data]
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
};
|
|
968
|
-
if (opConfig.fields) {
|
|
969
|
-
query._source = opConfig.fields;
|
|
970
|
-
}
|
|
971
|
-
if (sort) query.body.sort = [sort];
|
|
972
|
-
return query;
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
const response1 = {
|
|
976
|
-
geo_bounding_box: {
|
|
977
|
-
some_field: {
|
|
978
|
-
top_left: {
|
|
979
|
-
lat: '34.5234',
|
|
980
|
-
lon: '79.42345'
|
|
981
|
-
},
|
|
982
|
-
bottom_right: {
|
|
983
|
-
lat: '54.5234',
|
|
984
|
-
lon: '80.3456'
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
};
|
|
989
|
-
const sort1 = {
|
|
990
|
-
_geo_distance: {
|
|
991
|
-
some_field: {
|
|
992
|
-
lat: '52.3456',
|
|
993
|
-
lon: '79.6784'
|
|
994
|
-
},
|
|
995
|
-
order: 'asc',
|
|
996
|
-
unit: 'm'
|
|
997
|
-
}
|
|
998
|
-
};
|
|
999
|
-
const response2 = {
|
|
1000
|
-
geo_distance: {
|
|
1001
|
-
distance: '200km',
|
|
1002
|
-
some_field: {
|
|
1003
|
-
lat: '67.2435',
|
|
1004
|
-
lon: '100.2345'
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
};
|
|
1008
|
-
const sort2 = {
|
|
1009
|
-
_geo_distance: {
|
|
1010
|
-
some_field: {
|
|
1011
|
-
lat: '67.2435',
|
|
1012
|
-
lon: '100.2345'
|
|
1013
|
-
},
|
|
1014
|
-
order: 'asc',
|
|
1015
|
-
unit: 'm'
|
|
1016
|
-
}
|
|
1017
|
-
};
|
|
1018
|
-
|
|
1019
|
-
const sort3 = {
|
|
1020
|
-
_geo_distance: {
|
|
1021
|
-
some_field: {
|
|
1022
|
-
lat: '52.3456',
|
|
1023
|
-
lon: '79.6784'
|
|
1024
|
-
},
|
|
1025
|
-
order: 'desc',
|
|
1026
|
-
unit: 'km'
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
const response3 = [
|
|
1030
|
-
{
|
|
1031
|
-
range: {
|
|
1032
|
-
created: {
|
|
1033
|
-
gte: msg2.start,
|
|
1034
|
-
lt: msg2.end
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
},
|
|
1038
|
-
{
|
|
1039
|
-
geo_bounding_box: {
|
|
1040
|
-
some_field: {
|
|
1041
|
-
top_left: {
|
|
1042
|
-
lat: '34.5234',
|
|
1043
|
-
lon: '79.42345'
|
|
1044
|
-
},
|
|
1045
|
-
bottom_right: {
|
|
1046
|
-
lat: '54.5234',
|
|
1047
|
-
lon: '80.3456'
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
];
|
|
1053
|
-
|
|
1054
|
-
const finalResponse1 = makeResponse(goodConfig1, msg1, response1);
|
|
1055
|
-
const finalResponse2 = makeResponse(goodConfig2, msg1, response1, sort1);
|
|
1056
|
-
const finalResponse3 = makeResponse(goodConfig3, msg1, response2, sort2);
|
|
1057
|
-
const finalResponse4 = makeResponse(goodConfig4, msg1, response2, sort3);
|
|
1058
|
-
const finalResponse5 = makeResponse(goodConfig2, msg2, response3, sort1);
|
|
1059
|
-
|
|
1060
|
-
expect(() => api.buildQuery(badOpConfig1, msg1)).toThrow(
|
|
1061
|
-
'if geo_field is specified then the appropriate geo_box or geo_distance query parameters need to be provided as well'
|
|
1062
|
-
);
|
|
1063
|
-
expect(() => api.buildQuery(badOpConfig2, msg1)).toThrow(
|
|
1064
|
-
'Both geo_box_top_left and geo_box_bottom_right must be provided for a geo bounding box query.'
|
|
1065
|
-
);
|
|
1066
|
-
expect(() => api.buildQuery(badOpConfig3, msg1)).toThrow(
|
|
1067
|
-
'Both geo_box_top_left and geo_box_bottom_right must be provided for a geo bounding box query.'
|
|
1068
|
-
);
|
|
1069
|
-
expect(() => api.buildQuery(badOpConfig4, msg1)).toThrow(
|
|
1070
|
-
'Both geo_point and geo_distance must be provided for a geo_point query.'
|
|
1071
|
-
);
|
|
1072
|
-
expect(() => api.buildQuery(badOpConfig5, msg1)).toThrow(
|
|
1073
|
-
'Both geo_point and geo_distance must be provided for a geo_point query.'
|
|
1074
|
-
);
|
|
1075
|
-
expect(() => api.buildQuery(badOpConfig6, msg1)).toThrow(
|
|
1076
|
-
'geo_box and geo_distance queries can not be combined.'
|
|
1077
|
-
);
|
|
1078
|
-
expect(() => api.buildQuery(badOpConfig7, msg1)).toThrow(
|
|
1079
|
-
'bounding box search requires geo_sort_point to be set if any other geo_sort_* parameter is provided'
|
|
1080
|
-
);
|
|
1081
|
-
expect(() => api.buildQuery(badOpConfig8, msg1)).toThrow(
|
|
1082
|
-
'bounding box search requires geo_sort_point to be set if any other geo_sort_* parameter is provided'
|
|
1083
|
-
);
|
|
1084
|
-
|
|
1085
|
-
expect(api.buildQuery(goodConfig1, msg1)).toEqual(finalResponse1);
|
|
1086
|
-
expect(api.buildQuery(goodConfig2, msg1)).toEqual(finalResponse2);
|
|
1087
|
-
expect(api.buildQuery(goodConfig3, msg1)).toEqual(finalResponse3);
|
|
1088
|
-
expect(api.buildQuery(goodConfig4, msg1)).toEqual(finalResponse4);
|
|
1089
|
-
|
|
1090
|
-
expect(api.buildQuery(goodConfig2, msg2)).toEqual(finalResponse5);
|
|
1091
|
-
});
|
|
1092
|
-
|
|
1093
|
-
it('can call buildQuery for elastic queries', () => {
|
|
1094
|
-
const api = esApi(client, logger);
|
|
1095
|
-
const opConfig1 = { index: 'some_index' };
|
|
1096
|
-
const opConfig2 = { index: 'some_index', date_field_name: 'created' };
|
|
1097
|
-
const opConfig3 = { index: 'some_index', query: 'someLucene:query' };
|
|
1098
|
-
const opConfig4 = {
|
|
1099
|
-
index: 'some_index',
|
|
1100
|
-
query: 'someLucene:query',
|
|
1101
|
-
fields: ['field1', 'field2']
|
|
1102
|
-
};
|
|
1103
|
-
const field = 'someField';
|
|
1104
|
-
const msg1 = { count: 100, key: 'someKey' };
|
|
1105
|
-
const msg2 = { count: 100, start: new Date(), end: new Date() };
|
|
1106
|
-
const msg3 = { count: 100 };
|
|
1107
|
-
const msg4 = { count: 100, wildcard: { field, value: 'someKey' } };
|
|
1108
|
-
|
|
1109
|
-
function makeResponse(opConfig, msg, data) {
|
|
1110
|
-
const query = {
|
|
1111
|
-
index: opConfig.index,
|
|
1112
|
-
size: msg.count,
|
|
1113
|
-
body: {
|
|
1114
|
-
query: {
|
|
1115
|
-
bool: {
|
|
1116
|
-
must: Array.isArray(data) ? data : [data]
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
};
|
|
1121
|
-
if (opConfig.fields) {
|
|
1122
|
-
query._source = opConfig.fields;
|
|
1123
|
-
}
|
|
1124
|
-
return query;
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
const response1 = { wildcard: { _uid: 'someKey' } };
|
|
1128
|
-
const response2 = { range: { created: { gte: msg2.start, lt: msg2.end } } };
|
|
1129
|
-
const response3 = { query_string: { query: opConfig3.query } };
|
|
1130
|
-
const response4 = [
|
|
1131
|
-
{ wildcard: { _uid: 'someKey' } },
|
|
1132
|
-
{ query_string: { query: opConfig3.query } }
|
|
1133
|
-
];
|
|
1134
|
-
const response5 = [
|
|
1135
|
-
{ wildcard: { [field]: 'someKey' } },
|
|
1136
|
-
{ query_string: { query: opConfig3.query } }
|
|
1137
|
-
];
|
|
1138
|
-
|
|
1139
|
-
expect(api.buildQuery(opConfig1, msg1)).toEqual(makeResponse(opConfig1, msg1, response1));
|
|
1140
|
-
expect(api.buildQuery(opConfig2, msg2)).toEqual(makeResponse(opConfig2, msg2, response2));
|
|
1141
|
-
expect(api.buildQuery(opConfig3, msg3)).toEqual(makeResponse(opConfig3, msg3, response3));
|
|
1142
|
-
expect(api.buildQuery(opConfig4, msg3)).toEqual(makeResponse(opConfig4, msg3, response3));
|
|
1143
|
-
expect(api.buildQuery(opConfig3, msg1)).toEqual(makeResponse(opConfig3, msg1, response4));
|
|
1144
|
-
expect(api.buildQuery(opConfig3, msg4)).toEqual(makeResponse(opConfig3, msg1, response5));
|
|
1145
|
-
});
|
|
1146
|
-
|
|
1147
|
-
it('can set up an index', async () => {
|
|
1148
|
-
const api = esApi(client, logger);
|
|
1149
|
-
const clusterName = 'teracluster';
|
|
1150
|
-
const newIndex = 'teracluster__state';
|
|
1151
|
-
const migrantIndexName = 'teracluster__state-v0.0.33';
|
|
1152
|
-
const recordType = 'state';
|
|
1153
|
-
const clientName = 'default';
|
|
1154
|
-
|
|
1155
|
-
await expect(api.indexSetup(
|
|
1156
|
-
clusterName,
|
|
1157
|
-
newIndex,
|
|
1158
|
-
migrantIndexName,
|
|
1159
|
-
template,
|
|
1160
|
-
recordType,
|
|
1161
|
-
clientName
|
|
1162
|
-
)).resolves.not.toThrow();
|
|
1163
|
-
});
|
|
1164
|
-
|
|
1165
|
-
it('can set up an index and wait for availability', async () => {
|
|
1166
|
-
const api = esApi(client, logger);
|
|
1167
|
-
const clusterName = 'teracluster';
|
|
1168
|
-
const newIndex = 'teracluster__state';
|
|
1169
|
-
const migrantIndexName = 'teracluster__state-v0.0.33';
|
|
1170
|
-
const recordType = 'state';
|
|
1171
|
-
const clientName = 'default';
|
|
1172
|
-
|
|
1173
|
-
searchError = true;
|
|
1174
|
-
|
|
1175
|
-
await expect(Promise.all([
|
|
1176
|
-
waitFor(300, () => {
|
|
1177
|
-
searchError = false;
|
|
1178
|
-
}),
|
|
1179
|
-
api.indexSetup(
|
|
1180
|
-
clusterName,
|
|
1181
|
-
newIndex,
|
|
1182
|
-
migrantIndexName,
|
|
1183
|
-
template,
|
|
1184
|
-
recordType,
|
|
1185
|
-
clientName
|
|
1186
|
-
)
|
|
1187
|
-
])).resolves.not.toThrow();
|
|
1188
|
-
});
|
|
1189
|
-
|
|
1190
|
-
it('can wait for elasticsearch availability', async () => {
|
|
1191
|
-
const api = esApi(client, logger);
|
|
1192
|
-
const clusterName = 'teracluster';
|
|
1193
|
-
const newIndex = 'teracluster__state';
|
|
1194
|
-
const migrantIndexName = 'teracluster__state-v0.0.33';
|
|
1195
|
-
const recordType = 'state';
|
|
1196
|
-
const clientName = 'default';
|
|
1197
|
-
// this mimics an index not available to be searched as its not ready
|
|
1198
|
-
elasticDown = true;
|
|
1199
|
-
recoverError = true;
|
|
1200
|
-
|
|
1201
|
-
await expect(Promise.all([
|
|
1202
|
-
api.indexSetup(
|
|
1203
|
-
clusterName,
|
|
1204
|
-
newIndex,
|
|
1205
|
-
migrantIndexName,
|
|
1206
|
-
template,
|
|
1207
|
-
recordType,
|
|
1208
|
-
clientName,
|
|
1209
|
-
1000
|
|
1210
|
-
),
|
|
1211
|
-
waitFor(1, () => {
|
|
1212
|
-
elasticDown = false;
|
|
1213
|
-
}),
|
|
1214
|
-
waitFor(1200, () => {
|
|
1215
|
-
recoverError = false;
|
|
1216
|
-
})
|
|
1217
|
-
])).resolves.not.toThrow();
|
|
1218
|
-
});
|
|
1219
|
-
|
|
1220
|
-
it('can send template on state mapping changes, does not migrate', async () => {
|
|
1221
|
-
const api = esApi(client, logger);
|
|
1222
|
-
const clusterName = 'teracluster';
|
|
1223
|
-
const newIndex = 'teracluster__state';
|
|
1224
|
-
const migrantIndexName = 'teracluster__state-v0.0.33';
|
|
1225
|
-
const recordType = 'state';
|
|
1226
|
-
const clientName = 'default';
|
|
1227
|
-
|
|
1228
|
-
changeMappings = true;
|
|
1229
|
-
|
|
1230
|
-
await api.indexSetup(
|
|
1231
|
-
clusterName,
|
|
1232
|
-
newIndex,
|
|
1233
|
-
migrantIndexName,
|
|
1234
|
-
template,
|
|
1235
|
-
recordType,
|
|
1236
|
-
clientName
|
|
1237
|
-
);
|
|
1238
|
-
expect(putTemplateCalled).toEqual(true);
|
|
1239
|
-
expect(reindexCalled).toEqual(false);
|
|
1240
|
-
});
|
|
1241
|
-
|
|
1242
|
-
it('can migrate on mapping changes', async () => {
|
|
1243
|
-
const api = esApi(client, logger);
|
|
1244
|
-
const clusterName = 'teracluster';
|
|
1245
|
-
const newIndex = 'teracluster__ex';
|
|
1246
|
-
const migrantIndexName = 'teracluster__ex-v0.0.33';
|
|
1247
|
-
const recordType = 'ex';
|
|
1248
|
-
const clientName = 'default';
|
|
1249
|
-
|
|
1250
|
-
changeMappings = true;
|
|
1251
|
-
isExecutionTemplate = true;
|
|
1252
|
-
|
|
1253
|
-
await api.indexSetup(
|
|
1254
|
-
clusterName,
|
|
1255
|
-
newIndex,
|
|
1256
|
-
migrantIndexName,
|
|
1257
|
-
template,
|
|
1258
|
-
recordType,
|
|
1259
|
-
clientName
|
|
1260
|
-
);
|
|
1261
|
-
expect(reindexCalled).toEqual(true);
|
|
1262
|
-
expect(indicesDeleteCalled).toEqual(true);
|
|
1263
|
-
expect(indicesPutAliasCalled).toEqual(true);
|
|
1264
|
-
});
|
|
1265
|
-
});
|