-
Notifications
You must be signed in to change notification settings - Fork 13
/
elasticsearch.ts
210 lines (180 loc) · 6.47 KB
/
elasticsearch.ts
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
import * as R from 'rambda';
import { Client } from 'elasticsearch';
import * as ts from '@terascope/utils';
import { TypeConfig, FieldType } from 'xlucene-evaluator';
import { getFirstKey, getFirstValue, buildNestPath } from './misc';
import { getErrorType } from './errors';
import * as i from '../interfaces';
export function getTimeByField(field = ''): (input: any) => number {
return R.ifElse(
R.has(field),
R.pipe(
R.path(field as any),
(input: any) => new Date(input).getTime()
),
() => Date.now()
);
}
export function shardsPath(index: string): (stats: any) => i.Shard[] {
return R.pathOr([], [index, 'shards']);
}
export const verifyIndexShards: (shards: i.Shard[]) => boolean = R.pipe(
// @ts-ignore
R.filter((shard: i.Shard) => shard.primary),
R.all((shard: i.Shard) => shard.stage === 'DONE')
);
export function timeseriesIndex(index: string, timeSeriesFormat: i.TimeSeriesFormat = 'monthly'): string {
const formatter = {
daily: 10,
monthly: 7,
yearly: 4,
};
const format = formatter[timeSeriesFormat];
if (!format) throw new Error(`Unsupported format "${timeSeriesFormat}"`);
const dateStr = new Date().toISOString();
// remove -* or * at the end of the index name
const indexName = index.replace(/\-{0,1}\*$/, '');
return `${indexName}-${dateStr.slice(0, format).replace(/-/g, '.')}`;
}
export function filterBulkRetries<T>(records: T[], result: i.BulkResponse): T[] {
if (!result.errors) return [];
const retry = [];
const { items } = result;
const errorTypes = ['document_already_exists_exception', 'document_missing_exception'];
for (let index = 0; index < items.length; index += 1) {
// key could either be create or delete etc, just want the actual data at the value spot
const { item } = getBulkResponseItem(items[index]);
// On a create request if a document exists it's not an error.
// are there cases where this is incorrect?
if (item.error && item.status !== 409) {
const type = getErrorType(item);
if (type === 'es_rejected_execution_exception') {
// retry this record
if (records[index] != null) {
retry.push(records[index]);
}
} else if (errorTypes.includes(type)) {
const error = new ts.TSError(`${type}--${item.error.reason}`);
Error.captureStackTrace(error, filterBulkRetries);
throw error;
}
}
}
return retry;
}
type BulkResponseItemResult = {
item: i.BulkResponseItem;
action: i.BulkAction;
};
/**
* Get the first key and value from the bulk response:
*
* Here is an example input:
*
* ```json
{
"index": {
"_index": "test",
"_type": "type1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true,
"status": 201
}
}
* ```
*/
export function getBulkResponseItem(input: any = {}): BulkResponseItemResult {
return {
item: getFirstValue(input) as i.BulkResponseItem,
action: getFirstKey(input) as i.BulkAction,
};
}
export function getXLuceneTypesFromMapping(mapping: any): TypeConfig | undefined {
if (!ts.isPlainObject(mapping) || ts.isEmpty(mapping)) return;
const result: TypeConfig = {};
if (mapping.properties != null) {
const entries = getTypesFromProperties(mapping.properties);
for (const [key, val] of entries) {
result[key] = val;
}
}
return result;
}
type TypeMappingPair = [string, FieldType];
type MappingProperties = { [key: string]: MappingProperty };
type MappingProperty = { type?: string; properties: MappingProperties };
export function getTypesFromProperties(properties: MappingProperties, basePath = ''): TypeMappingPair[] {
const result: TypeMappingPair[] = [];
for (const [key, value] of Object.entries(properties)) {
if (ts.isPlainObject(value) && key) {
const path = buildNestPath([basePath, key]);
if (value.properties) {
result.push(...getTypesFromProperties(value.properties, path));
} else {
const type = getXluceneTypeFromESType(value.type);
if (type) {
result.push([path, type]);
}
}
}
}
return result;
}
export function getXluceneTypeFromESType(type?: string): FieldType | undefined {
if (!type) return;
if (['geo_point', 'geo_shape'].includes(type)) return FieldType.Geo;
if (type === 'ip') return FieldType.IP;
if (type === 'date') return FieldType.Date;
if (['byte', 'short', 'integer', 'long'].includes(type)) return FieldType.Integer;
if (['double', 'float'].includes(type)) return FieldType.Float;
if (['keyword', 'text'].includes(type)) return FieldType.String;
if (type === 'object') return FieldType.Object;
if (type === 'boolean') return FieldType.Boolean;
return;
}
export function getESVersion(client: Client): number {
const version = ts.get(client, 'transport._config.apiVersion', '6.5');
if (version && ts.isString(version)) {
const [majorVersion] = version.split('.');
return ts.toNumber(majorVersion);
}
return 6;
}
export function fixMappingRequest(client: Client, _params: any, isTemplate: boolean) {
if (!_params || !_params.body) {
throw new Error('Invalid mapping request');
}
const params = ts.cloneDeep(_params);
const defaultParams: any = {};
const esVersion = getESVersion(client);
if (esVersion >= 6) {
if (params.body.template) {
if (isTemplate) {
params.body.index_patterns = ts.castArray(params.body.template).slice();
}
delete params.body.template;
}
}
if (esVersion >= 7) {
const typeMappings = ts.get(params.body, 'mappings', {});
if (typeMappings.properties) {
defaultParams.includeTypeName = false;
} else {
defaultParams.includeTypeName = true;
Object.values(typeMappings).forEach((typeMapping: any) => {
if (typeMapping && typeMapping._all) {
delete typeMapping._all;
}
return '';
});
}
}
return Object.assign({}, defaultParams, params);
}