@terascope/elasticsearch-api 3.1.0 → 3.2.1

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.
@@ -0,0 +1,130 @@
1
+ 'use strict';
2
+
3
+ const { Client } = require('elasticsearch');
4
+ const opensearch = require('@opensearch-project/opensearch');
5
+ const elasticsearch6 = require('elasticsearch6');
6
+ const elasticsearch7 = require('elasticsearch7');
7
+ const elasticsearch8 = require('elasticsearch8');
8
+ const { pDelay } = require('@terascope/utils');
9
+ const elasticAPI = require('../../index');
10
+
11
+ const {
12
+ ELASTICSEARCH_HOST, ELASTICSEARCH_API_VERSION,
13
+ ELASTICSEARCH_VERSION, RESTRAINED_OPENSEARCH_HOST,
14
+ } = require('./config');
15
+
16
+ const semver = ELASTICSEARCH_VERSION.split('.');
17
+ const majorVersion = Number(semver[0]);
18
+ const minorVersion = Number(semver[1]);
19
+
20
+ const isLegacyTest = process.env.LEGACY_CLIENT != null;
21
+ const isOpensearchTest = process.env.TEST_OPENSEARCH != null;
22
+
23
+ const isES6ClientTest = !isOpensearchTest && majorVersion === 6;
24
+ const isES7ClientTest = !isOpensearchTest && majorVersion === 7;
25
+ const isES8ClientTest = !isOpensearchTest && majorVersion === 8;
26
+
27
+ async function makeClient() {
28
+ const node = ELASTICSEARCH_HOST;
29
+
30
+ if (isLegacyTest) {
31
+ return new Client({
32
+ host: node,
33
+ log: 'error',
34
+ apiVersion: ELASTICSEARCH_API_VERSION,
35
+ });
36
+ }
37
+
38
+ if (isOpensearchTest) {
39
+ return new opensearch.Client({
40
+ node: RESTRAINED_OPENSEARCH_HOST
41
+ });
42
+ }
43
+
44
+ if (isES6ClientTest) {
45
+ return new elasticsearch6.Client({
46
+ node
47
+ });
48
+ }
49
+
50
+ if (isES7ClientTest) {
51
+ if (minorVersion <= 13) {
52
+ return new opensearch.Client({
53
+ node
54
+ });
55
+ }
56
+ return new elasticsearch7.Client({
57
+ node
58
+ });
59
+ }
60
+
61
+ if (isES8ClientTest) {
62
+ return new elasticsearch8.Client({
63
+ node
64
+ });
65
+ }
66
+
67
+ throw new Error('Invalid config, cannot determine what test type is being executed');
68
+ }
69
+
70
+ function formatUploadData(
71
+ index, data
72
+ ) {
73
+ const results = [];
74
+
75
+ data.forEach((record) => {
76
+ const meta = { _index: index };
77
+
78
+ if (!isES8ClientTest) {
79
+ meta._type = '_doc';
80
+ }
81
+
82
+ results.push({ action: { index: meta }, data: record });
83
+ });
84
+
85
+ return results;
86
+ }
87
+
88
+ async function waitForData(
89
+ client, index, count, logger, timeout = 5000
90
+ ) {
91
+ const esClient = elasticAPI(client, logger);
92
+ const failTestTime = Date.now() + timeout;
93
+
94
+ return new Promise((resolve, reject) => {
95
+ async function checkIndex() {
96
+ if (failTestTime <= Date.now()) reject(new Error('Could not find count in allotted time'));
97
+ await pDelay(100);
98
+ try {
99
+ const indexCount = await esClient.count({ index, q: '*' });
100
+ if (count === indexCount) return resolve();
101
+ } catch (err) {
102
+ if (err.statusCode !== 404) {
103
+ // return reject(err);
104
+ }
105
+ }
106
+
107
+ return checkIndex();
108
+ }
109
+
110
+ checkIndex();
111
+ });
112
+ }
113
+
114
+ async function cleanupIndex(
115
+ client, index
116
+ ) {
117
+ await client.indices
118
+ .delete({
119
+ index,
120
+ requestTimeout: 3000,
121
+ })
122
+ .catch(() => {});
123
+ }
124
+
125
+ module.exports = {
126
+ makeClient,
127
+ waitForData,
128
+ cleanupIndex,
129
+ formatUploadData
130
+ };