-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfer_mappings.test.js
More file actions
197 lines (168 loc) · 5.72 KB
/
infer_mappings.test.js
File metadata and controls
197 lines (168 loc) · 5.72 KB
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
const retry = require('async-retry');
const transformer = require('../dist/node-es-transformer.cjs');
const deleteIndex = require('./utils/delete_index');
const { elasticsearchUrl, getElasticsearchClient } = require('./utils/elasticsearch');
const client = getElasticsearchClient();
const inferredIndex = 'infer_mappings_csv';
const explicitMappingIndex = 'infer_mappings_explicit';
const explicitPipelineIndex = 'infer_mappings_explicit_pipeline';
function createCapturingClient(rawClient, calls, pipelineCalls, forceInferredPipeline = false) {
return {
info: (...args) => rawClient.info(...args),
close: (...args) => rawClient.close(...args),
indices: rawClient.indices,
helpers: rawClient.helpers,
ingest: {
putPipeline: async params => {
pipelineCalls.push(params);
return rawClient.ingest.putPipeline(params);
},
},
textStructure: {
findStructure: async params => {
calls.push(params);
const response = await rawClient.textStructure.findStructure(params);
if (!forceInferredPipeline) {
return response;
}
return {
...response,
ingest_pipeline: {
description: 'Inferred test pipeline',
processors: [
{
set: {
field: 'inferred_pipeline_used',
value: true,
},
},
],
},
};
},
},
};
}
describe('mapping inference via _text_structure/find_structure', () => {
afterAll(async () => {
await deleteIndex(client, inferredIndex)();
await deleteIndex(client, explicitMappingIndex)();
await deleteIndex(client, explicitPipelineIndex)();
await client.ingest.deletePipeline({ id: 'manual-csv-pipeline' }).catch(() => {});
await client.close();
});
it('should infer mappings for CSV without applying ingest pipeline', done => {
(async () => {
const calls = [];
const pipelineCalls = [];
const capturingClient = createCapturingClient(client, calls, pipelineCalls, true);
const { events } = await transformer({
sourceClient: capturingClient,
targetClient: capturingClient,
fileName: './data/sample_data_csv_1.csv',
sourceFormat: 'csv',
targetIndexName: inferredIndex,
inferMappings: true,
inferMappingsOptions: {
lines_to_sample: 100,
},
deleteIndex: true,
verbose: false,
});
events.on('finish', async () => {
expect(calls.length).toBe(1);
expect(calls[0].format).toBe('delimited');
expect(typeof calls[0].body).toBe('string');
expect(pipelineCalls.length).toBe(0);
await client.indices.refresh({ index: inferredIndex });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${inferredIndex}/_count`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.count).toBe(3);
});
done();
});
})();
});
it('should skip inference when mappings are explicitly provided', done => {
(async () => {
const calls = [];
const pipelineCalls = [];
const capturingClient = createCapturingClient(client, calls, pipelineCalls);
const { events } = await transformer({
sourceClient: capturingClient,
targetClient: capturingClient,
fileName: './data/sample_data_csv_1.csv',
sourceFormat: 'csv',
targetIndexName: explicitMappingIndex,
inferMappings: true,
mappings: {
properties: {
the_index: { type: 'integer' },
code: { type: 'integer' },
url: { type: 'keyword' },
text: { type: 'keyword' },
},
},
deleteIndex: true,
verbose: false,
});
events.on('finish', async () => {
expect(calls.length).toBe(0);
expect(pipelineCalls.length).toBe(0);
await client.indices.refresh({ index: explicitMappingIndex });
await retry(async () => {
const res = await fetch(`${elasticsearchUrl}/${explicitMappingIndex}/_count`);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.count).toBe(3);
});
done();
});
})();
});
it('should prefer explicit pipeline over inferred ingest pipeline', done => {
(async () => {
await client.ingest.putPipeline({
id: 'manual-csv-pipeline',
processors: [
{
set: {
field: 'manual_pipeline_used',
value: true,
},
},
],
});
const calls = [];
const pipelineCalls = [];
const capturingClient = createCapturingClient(client, calls, pipelineCalls, true);
const { events } = await transformer({
sourceClient: capturingClient,
targetClient: capturingClient,
fileName: './data/sample_data_csv_1.csv',
sourceFormat: 'csv',
targetIndexName: explicitPipelineIndex,
inferMappings: true,
pipeline: 'manual-csv-pipeline',
deleteIndex: true,
verbose: false,
});
events.on('finish', async () => {
expect(calls.length).toBe(1);
expect(pipelineCalls.length).toBe(0);
await client.indices.refresh({ index: explicitPipelineIndex });
await retry(async () => {
const res = await fetch(
`${elasticsearchUrl}/${explicitPipelineIndex}/_search?q=manual_pipeline_used:true`,
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body?.hits?.total?.value).toBe(3);
});
done();
});
})();
});
});