-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypescript-example.ts
More file actions
163 lines (146 loc) · 3.35 KB
/
typescript-example.ts
File metadata and controls
163 lines (146 loc) · 3.35 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
/**
* TypeScript Example
*
* Demonstrates TypeScript support with type checking and IntelliSense.
* This file is for reference only and is not meant to be executed directly.
*/
import pino from 'pino';
import transformer, { TransformerOptions, TransformFunction } from 'node-es-transformer';
const logger = pino({
name: 'node-es-transformer-example',
level: process.env.LOG_LEVEL || 'info',
});
// Example 1: Basic usage with type checking
const options: TransformerOptions = {
fileName: 'data/sample.json',
targetIndexName: 'my-index',
mappings: {
properties: {
'@timestamp': {
type: 'date'
},
'name': {
type: 'keyword'
},
'value': {
type: 'integer'
}
}
}
};
transformer(options).then(result => {
// Events are typed
result.events.on('complete', () => {
logger.info('Ingestion complete');
});
});
// Example 2: Transform function with type safety
interface SourceDoc {
first_name: string;
last_name: string;
age: number;
}
interface TargetDoc {
full_name: string;
age: number;
age_group: string;
}
const myTransform: TransformFunction = (doc: SourceDoc): TargetDoc => {
return {
full_name: `${doc.first_name} ${doc.last_name}`,
age: doc.age,
age_group: doc.age < 18 ? 'minor' : 'adult'
};
};
transformer({
sourceIndexName: 'users-v1',
targetIndexName: 'users-v2',
transform: myTransform
});
// Example 3: Document splitting with types
interface Tweet {
id: string;
text: string;
user: string;
hashtags: string[];
created_at: string;
}
interface HashtagDoc {
tweet_id: string;
hashtag: string;
text: string;
user: string;
'@timestamp': string;
}
const splitTransform: TransformFunction = (tweet: Tweet): HashtagDoc[] | null => {
if (!tweet.hashtags || tweet.hashtags.length === 0) {
return null;
}
return tweet.hashtags.map(hashtag => ({
tweet_id: tweet.id,
hashtag: hashtag,
text: tweet.text,
user: tweet.user,
'@timestamp': tweet.created_at
}));
};
transformer({
sourceIndexName: 'tweets',
targetIndexName: 'hashtags',
transform: splitTransform
});
// Example 4: Cross-version reindex with type safety
const crossVersionOptions: TransformerOptions = {
sourceClientConfig: {
node: 'https://es8-cluster.example.com:9200',
auth: {
apiKey: process.env.ES8_API_KEY
}
},
targetClientConfig: {
node: 'https://es9-cluster.example.com:9200',
auth: {
apiKey: process.env.ES9_API_KEY
}
},
sourceClientVersion: 8,
targetClientVersion: 9,
sourceIndexName: 'my-index',
targetIndexName: 'my-index'
};
transformer(crossVersionOptions);
// Example 5: All options with full type checking
const fullOptions: TransformerOptions = {
targetIndexName: 'complete-example',
deleteIndex: true,
sourceClientConfig: {
node: 'http://localhost:9200'
},
targetClientConfig: {
node: 'http://localhost:9200'
},
bufferSize: 5120,
searchSize: 100,
fileName: 'data/*.json',
splitRegex: /\n/,
mappings: {
properties: {
'id': { type: 'keyword' },
'value': { type: 'float' }
}
},
indexMappingTotalFieldsLimit: 2000,
populatedFields: false,
query: {
term: {
status: 'active'
}
},
skipHeader: false,
verbose: true,
transform: (doc: any) => ({
...doc,
processed_at: new Date().toISOString()
})
};
transformer(fullOptions);