Skip to content

Commit 7ee91e7

Browse files
committed
[ML] cap aggregation to max 1000 buckets
1 parent a85a4d6 commit 7ee91e7

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

x-pack/plugins/ml/server/models/fields_service/fields_service.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import Boom from 'boom';
78
import { APICaller } from 'kibana/server';
9+
import { parseInterval } from '../../../common/util/parse_interval';
810

911
/**
1012
* Service for carrying out queries to obtain data
@@ -152,6 +154,34 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
152154
});
153155
}
154156

157+
/**
158+
* Caps provided time boundaries based on the interval.
159+
* @param earliestMs
160+
* @param latestMs
161+
* @param interval
162+
*/
163+
function getSafeTimeRange(
164+
earliestMs: number,
165+
latestMs: number,
166+
interval: string
167+
): { start: number; end: number } {
168+
const maxNumberOfBuckets = 1000;
169+
const end = latestMs;
170+
171+
const intervalInMs = parseInterval(interval);
172+
173+
if (intervalInMs === null) {
174+
throw Boom.badRequest('Interval is invalid');
175+
}
176+
177+
const start = Math.max(
178+
earliestMs,
179+
latestMs - maxNumberOfBuckets * intervalInMs.asMilliseconds()
180+
);
181+
182+
return { start, end };
183+
}
184+
155185
/**
156186
* Retrieves max cardinalities for provided fields from date interval buckets
157187
* using max bucket pipeline aggregation.
@@ -183,12 +213,14 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
183213
return {};
184214
}
185215

216+
const { start, end } = getSafeTimeRange(earliestMs, latestMs, interval);
217+
186218
const mustCriteria = [
187219
{
188220
range: {
189221
[timeFieldName]: {
190-
gte: earliestMs,
191-
lte: latestMs,
222+
gte: start,
223+
lte: end,
192224
format: 'epoch_millis',
193225
},
194226
},
@@ -223,7 +255,7 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
223255
const body = {
224256
query: {
225257
bool: {
226-
must: mustCriteria,
258+
filter: mustCriteria,
227259
},
228260
},
229261
size: 0,

0 commit comments

Comments
 (0)