2021-05-11 09:44:00 +01:00
|
|
|
import { gte, lt } from 'semver';
|
2020-12-04 14:29:40 +00:00
|
|
|
import {
|
|
|
|
|
Filters,
|
|
|
|
|
Histogram,
|
|
|
|
|
DateHistogram,
|
|
|
|
|
Terms,
|
|
|
|
|
} from './components/QueryEditor/BucketAggregationsEditor/aggregations';
|
|
|
|
|
import {
|
|
|
|
|
isMetricAggregationWithField,
|
|
|
|
|
isMetricAggregationWithSettings,
|
2021-04-26 16:54:23 +01:00
|
|
|
isMovingAverageWithModelSettings,
|
2020-12-04 14:29:40 +00:00
|
|
|
isPipelineAggregation,
|
|
|
|
|
isPipelineAggregationWithMultipleBucketPaths,
|
2021-03-05 12:48:45 +00:00
|
|
|
MetricAggregation,
|
|
|
|
|
MetricAggregationWithInlineScript,
|
2020-12-04 14:29:40 +00:00
|
|
|
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
|
2021-02-24 09:31:17 -07:00
|
|
|
import { defaultBucketAgg, defaultMetricAgg, findMetricById, highlightTags } from './query_def';
|
2020-12-04 14:29:40 +00:00
|
|
|
import { ElasticsearchQuery } from './types';
|
2021-03-05 12:48:45 +00:00
|
|
|
import { convertOrderByToMetricId, getScriptValue } from './utils';
|
2015-07-01 08:54:06 -04:00
|
|
|
|
2017-09-28 12:52:39 +02:00
|
|
|
export class ElasticQueryBuilder {
|
|
|
|
|
timeField: string;
|
2021-05-11 09:44:00 +01:00
|
|
|
esVersion: string;
|
2017-09-28 12:52:39 +02:00
|
|
|
|
2021-05-11 09:44:00 +01:00
|
|
|
constructor(options: { timeField: string; esVersion: string }) {
|
2015-09-07 08:57:46 +02:00
|
|
|
this.timeField = options.timeField;
|
2015-12-03 18:30:36 +01:00
|
|
|
this.esVersion = options.esVersion;
|
2015-09-07 08:57:46 +02:00
|
|
|
}
|
2015-07-01 08:54:06 -04:00
|
|
|
|
2017-09-28 12:52:39 +02:00
|
|
|
getRangeFilter() {
|
2019-07-11 17:05:45 +02:00
|
|
|
const filter: any = {};
|
2016-12-09 11:22:43 +01:00
|
|
|
filter[this.timeField] = {
|
2017-12-20 12:33:33 +01:00
|
|
|
gte: '$timeFrom',
|
|
|
|
|
lte: '$timeTo',
|
|
|
|
|
format: 'epoch_millis',
|
2016-12-09 11:22:43 +01:00
|
|
|
};
|
2015-11-22 21:39:56 +09:00
|
|
|
|
2015-09-04 11:17:52 +02:00
|
|
|
return filter;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-09-04 11:17:52 +02:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
buildTermsAgg(aggDef: Terms, queryNode: { terms?: any; aggs?: any }, target: ElasticsearchQuery) {
|
2017-12-19 16:06:54 +01:00
|
|
|
queryNode.terms = { field: aggDef.field };
|
2015-09-06 14:45:12 +02:00
|
|
|
|
2015-09-07 16:35:40 +02:00
|
|
|
if (!aggDef.settings) {
|
|
|
|
|
return queryNode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
// TODO: This default should be somewhere else together with the one used in the UI
|
|
|
|
|
const size = aggDef.settings?.size ? parseInt(aggDef.settings.size, 10) : 500;
|
|
|
|
|
queryNode.terms.size = size === 0 ? 500 : size;
|
|
|
|
|
|
2015-09-07 16:35:40 +02:00
|
|
|
if (aggDef.settings.orderBy !== void 0) {
|
2015-09-06 14:45:12 +02:00
|
|
|
queryNode.terms.order = {};
|
2021-05-11 09:44:00 +01:00
|
|
|
if (aggDef.settings.orderBy === '_term' && gte(this.esVersion, '6.0.0')) {
|
2018-11-02 17:52:40 +01:00
|
|
|
queryNode.terms.order['_key'] = aggDef.settings.order;
|
|
|
|
|
} else {
|
|
|
|
|
queryNode.terms.order[aggDef.settings.orderBy] = aggDef.settings.order;
|
|
|
|
|
}
|
2015-09-06 14:45:12 +02:00
|
|
|
|
|
|
|
|
// if metric ref, look it up and add it to this agg level
|
2021-01-15 04:10:16 -07:00
|
|
|
const metricId = convertOrderByToMetricId(aggDef.settings.orderBy);
|
|
|
|
|
if (metricId) {
|
2020-12-04 14:29:40 +00:00
|
|
|
for (let metric of target.metrics || []) {
|
2021-01-15 04:10:16 -07:00
|
|
|
if (metric.id === metricId) {
|
|
|
|
|
if (metric.type === 'count') {
|
|
|
|
|
queryNode.terms.order = { _count: aggDef.settings.order };
|
|
|
|
|
} else if (isMetricAggregationWithField(metric)) {
|
|
|
|
|
queryNode.aggs = {};
|
|
|
|
|
queryNode.aggs[metric.id] = {
|
|
|
|
|
[metric.type]: { field: metric.field },
|
|
|
|
|
};
|
2020-12-04 14:29:40 +00:00
|
|
|
}
|
2015-09-06 14:45:12 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-23 07:26:42 +01:00
|
|
|
if (aggDef.settings.min_doc_count !== void 0) {
|
2017-12-21 08:39:31 +01:00
|
|
|
queryNode.terms.min_doc_count = parseInt(aggDef.settings.min_doc_count, 10);
|
2020-06-17 10:04:38 +02:00
|
|
|
|
|
|
|
|
if (isNaN(queryNode.terms.min_doc_count)) {
|
|
|
|
|
queryNode.terms.min_doc_count = aggDef.settings.min_doc_count;
|
|
|
|
|
}
|
2017-01-23 07:26:42 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 22:34:28 +02:00
|
|
|
if (aggDef.settings.missing) {
|
|
|
|
|
queryNode.terms.missing = aggDef.settings.missing;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-06 14:45:12 +02:00
|
|
|
return queryNode;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-09-06 14:45:12 +02:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
getDateHistogramAgg(aggDef: DateHistogram) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const esAgg: any = {};
|
|
|
|
|
const settings = aggDef.settings || {};
|
2015-12-04 10:06:44 +01:00
|
|
|
esAgg.interval = settings.interval;
|
|
|
|
|
esAgg.field = this.timeField;
|
|
|
|
|
esAgg.min_doc_count = settings.min_doc_count || 0;
|
2017-12-20 12:33:33 +01:00
|
|
|
esAgg.extended_bounds = { min: '$timeFrom', max: '$timeTo' };
|
|
|
|
|
esAgg.format = 'epoch_millis';
|
2018-11-26 08:58:25 -05:00
|
|
|
|
|
|
|
|
if (settings.offset !== '') {
|
|
|
|
|
esAgg.offset = settings.offset;
|
|
|
|
|
}
|
2015-12-04 10:06:44 +01:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
if (esAgg.interval === 'auto') {
|
|
|
|
|
esAgg.interval = '$__interval';
|
2015-09-07 16:35:40 +02:00
|
|
|
}
|
2015-12-04 10:06:44 +01:00
|
|
|
|
|
|
|
|
return esAgg;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-09-07 16:35:40 +02:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
getHistogramAgg(aggDef: Histogram) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const esAgg: any = {};
|
|
|
|
|
const settings = aggDef.settings || {};
|
2017-03-29 13:44:01 +02:00
|
|
|
esAgg.interval = settings.interval;
|
|
|
|
|
esAgg.field = aggDef.field;
|
|
|
|
|
esAgg.min_doc_count = settings.min_doc_count || 0;
|
|
|
|
|
|
|
|
|
|
return esAgg;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2017-03-29 13:44:01 +02:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
getFiltersAgg(aggDef: Filters) {
|
|
|
|
|
const filterObj: Record<string, { query_string: { query: string; analyze_wildcard: boolean } }> = {};
|
|
|
|
|
|
|
|
|
|
for (let { query, label } of aggDef.settings?.filters || []) {
|
|
|
|
|
filterObj[label || query] = {
|
2016-12-06 16:04:31 +01:00
|
|
|
query_string: {
|
|
|
|
|
query: query,
|
2017-12-20 12:33:33 +01:00
|
|
|
analyze_wildcard: true,
|
|
|
|
|
},
|
2016-12-06 16:04:31 +01:00
|
|
|
};
|
2015-09-21 19:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filterObj;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-09-21 19:23:18 +02:00
|
|
|
|
2019-07-11 17:05:45 +02:00
|
|
|
documentQuery(query: any, size: number) {
|
2017-06-08 11:53:12 +02:00
|
|
|
query.size = size;
|
2020-12-10 04:19:14 -07:00
|
|
|
query.sort = [
|
|
|
|
|
{
|
|
|
|
|
[this.timeField]: { order: 'desc', unmapped_type: 'boolean' },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
_doc: { order: 'desc' },
|
|
|
|
|
},
|
|
|
|
|
];
|
2016-11-08 22:18:59 +01:00
|
|
|
|
|
|
|
|
// fields field not supported on ES 5.x
|
2021-05-11 09:44:00 +01:00
|
|
|
if (lt(this.esVersion, '5.0.0')) {
|
2017-12-20 12:33:33 +01:00
|
|
|
query.fields = ['*', '_source'];
|
2016-11-08 22:18:59 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-25 11:43:29 -04:00
|
|
|
query.script_fields = {};
|
2015-11-05 09:56:19 +01:00
|
|
|
return query;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-11-05 09:56:19 +01:00
|
|
|
|
2019-07-11 17:05:45 +02:00
|
|
|
addAdhocFilters(query: any, adhocFilters: any) {
|
2016-09-21 08:46:59 +02:00
|
|
|
if (!adhocFilters) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 17:05:45 +02:00
|
|
|
let i, filter, condition: any, queryCondition: any;
|
2017-09-05 10:16:02 +02:00
|
|
|
|
2016-09-21 08:46:59 +02:00
|
|
|
for (i = 0; i < adhocFilters.length; i++) {
|
|
|
|
|
filter = adhocFilters[i];
|
|
|
|
|
condition = {};
|
|
|
|
|
condition[filter.key] = filter.value;
|
2017-08-28 10:52:10 +02:00
|
|
|
queryCondition = {};
|
2017-12-19 16:06:54 +01:00
|
|
|
queryCondition[filter.key] = { query: filter.value };
|
2017-08-28 10:52:10 +02:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
switch (filter.operator) {
|
2017-12-20 12:33:33 +01:00
|
|
|
case '=':
|
2017-12-19 16:06:54 +01:00
|
|
|
if (!query.query.bool.must) {
|
|
|
|
|
query.query.bool.must = [];
|
|
|
|
|
}
|
|
|
|
|
query.query.bool.must.push({ match_phrase: queryCondition });
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
2017-12-20 12:33:33 +01:00
|
|
|
case '!=':
|
2017-12-19 16:06:54 +01:00
|
|
|
if (!query.query.bool.must_not) {
|
|
|
|
|
query.query.bool.must_not = [];
|
|
|
|
|
}
|
|
|
|
|
query.query.bool.must_not.push({ match_phrase: queryCondition });
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
2017-12-20 12:33:33 +01:00
|
|
|
case '<':
|
2017-12-19 16:06:54 +01:00
|
|
|
condition[filter.key] = { lt: filter.value };
|
|
|
|
|
query.query.bool.filter.push({ range: condition });
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
2017-12-20 12:33:33 +01:00
|
|
|
case '>':
|
2017-12-19 16:06:54 +01:00
|
|
|
condition[filter.key] = { gt: filter.value };
|
|
|
|
|
query.query.bool.filter.push({ range: condition });
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
2017-12-20 12:33:33 +01:00
|
|
|
case '=~':
|
2017-12-19 16:06:54 +01:00
|
|
|
query.query.bool.filter.push({ regexp: condition });
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
2017-12-20 12:33:33 +01:00
|
|
|
case '!~':
|
2017-12-19 16:06:54 +01:00
|
|
|
query.query.bool.filter.push({
|
2017-12-20 12:33:33 +01:00
|
|
|
bool: { must_not: { regexp: condition } },
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2017-02-21 05:01:52 +09:00
|
|
|
break;
|
|
|
|
|
}
|
2016-09-21 08:46:59 +02:00
|
|
|
}
|
2017-10-07 10:31:39 +02:00
|
|
|
}
|
2016-09-21 08:46:59 +02:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
build(target: ElasticsearchQuery, adhocFilters?: any, queryString?: string) {
|
2015-12-03 16:32:35 +01:00
|
|
|
// make sure query has defaults;
|
2020-12-04 14:29:40 +00:00
|
|
|
target.metrics = target.metrics || [defaultMetricAgg()];
|
|
|
|
|
target.bucketAggs = target.bucketAggs || [defaultBucketAgg()];
|
2017-12-19 16:06:54 +01:00
|
|
|
target.timeField = this.timeField;
|
2021-03-05 12:48:45 +00:00
|
|
|
let metric: MetricAggregation;
|
2015-09-02 17:45:41 +02:00
|
|
|
|
2021-03-05 12:48:45 +00:00
|
|
|
let i, j, pv, nestedAggs;
|
2018-08-29 14:27:29 +02:00
|
|
|
const query = {
|
2017-12-19 16:06:54 +01:00
|
|
|
size: 0,
|
|
|
|
|
query: {
|
|
|
|
|
bool: {
|
|
|
|
|
filter: [
|
|
|
|
|
{ range: this.getRangeFilter() },
|
2016-12-09 14:57:25 +00:00
|
|
|
{
|
2017-12-19 16:06:54 +01:00
|
|
|
query_string: {
|
|
|
|
|
analyze_wildcard: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
query: queryString,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
2016-12-06 16:04:31 +01:00
|
|
|
};
|
2015-09-03 08:18:00 +02:00
|
|
|
|
2016-09-21 08:46:59 +02:00
|
|
|
this.addAdhocFilters(query, adhocFilters);
|
|
|
|
|
|
2020-05-07 21:43:41 +02:00
|
|
|
// If target doesn't have bucketAggs and type is not raw_document, it is invalid query.
|
2015-11-05 09:56:19 +01:00
|
|
|
if (target.bucketAggs.length === 0) {
|
|
|
|
|
metric = target.metrics[0];
|
2020-07-15 15:20:39 +02:00
|
|
|
|
|
|
|
|
if (!metric || !(metric.type === 'raw_document' || metric.type === 'raw_data')) {
|
2017-12-20 12:33:33 +01:00
|
|
|
throw { message: 'Invalid query' };
|
2017-06-03 02:55:26 +00:00
|
|
|
}
|
2020-05-07 21:43:41 +02:00
|
|
|
}
|
2017-06-08 11:53:12 +02:00
|
|
|
|
2020-05-07 21:43:41 +02:00
|
|
|
/* Handle document query:
|
|
|
|
|
* Check if metric type is raw_document. If metric doesn't have size (or size is 0), update size to 500.
|
|
|
|
|
* Otherwise it will not be a valid query and error will be thrown.
|
|
|
|
|
*/
|
2020-07-15 15:20:39 +02:00
|
|
|
if (target.metrics?.[0]?.type === 'raw_document' || target.metrics?.[0]?.type === 'raw_data') {
|
2020-05-07 21:43:41 +02:00
|
|
|
metric = target.metrics[0];
|
2020-12-04 14:29:40 +00:00
|
|
|
|
|
|
|
|
// TODO: This default should be somewhere else together with the one used in the UI
|
|
|
|
|
const size = metric.settings?.size ? parseInt(metric.settings.size, 10) : 500;
|
|
|
|
|
|
|
|
|
|
return this.documentQuery(query, size || 500);
|
2015-11-05 09:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-04 16:05:47 +02:00
|
|
|
nestedAggs = query;
|
2015-09-04 09:41:23 +02:00
|
|
|
|
2015-09-04 16:05:47 +02:00
|
|
|
for (i = 0; i < target.bucketAggs.length; i++) {
|
2020-12-04 14:29:40 +00:00
|
|
|
const aggDef = target.bucketAggs[i];
|
2019-07-11 17:05:45 +02:00
|
|
|
const esAgg: any = {};
|
2015-09-03 11:14:25 +02:00
|
|
|
|
2017-09-28 12:52:39 +02:00
|
|
|
switch (aggDef.type) {
|
2017-12-20 12:33:33 +01:00
|
|
|
case 'date_histogram': {
|
|
|
|
|
esAgg['date_histogram'] = this.getDateHistogramAgg(aggDef);
|
2015-09-04 16:05:47 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 12:33:33 +01:00
|
|
|
case 'histogram': {
|
|
|
|
|
esAgg['histogram'] = this.getHistogramAgg(aggDef);
|
2017-03-29 13:44:01 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 12:33:33 +01:00
|
|
|
case 'filters': {
|
|
|
|
|
esAgg['filters'] = { filters: this.getFiltersAgg(aggDef) };
|
2015-09-21 19:23:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 12:33:33 +01:00
|
|
|
case 'terms': {
|
2015-09-06 14:45:12 +02:00
|
|
|
this.buildTermsAgg(aggDef, esAgg, target);
|
2015-09-04 16:05:47 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 12:33:33 +01:00
|
|
|
case 'geohash_grid': {
|
|
|
|
|
esAgg['geohash_grid'] = {
|
2017-12-19 16:06:54 +01:00
|
|
|
field: aggDef.field,
|
2020-12-04 14:29:40 +00:00
|
|
|
precision: aggDef.settings?.precision,
|
2017-12-19 16:06:54 +01:00
|
|
|
};
|
2016-04-28 15:29:54 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2015-09-04 16:05:47 +02:00
|
|
|
}
|
2015-09-03 12:35:21 +02:00
|
|
|
|
2015-09-05 12:51:05 +02:00
|
|
|
nestedAggs.aggs = nestedAggs.aggs || {};
|
2015-09-05 12:24:14 +02:00
|
|
|
nestedAggs.aggs[aggDef.id] = esAgg;
|
2015-09-04 16:05:47 +02:00
|
|
|
nestedAggs = esAgg;
|
2015-09-03 11:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-27 10:57:44 +01:00
|
|
|
nestedAggs.aggs = {};
|
2015-09-03 16:35:11 +02:00
|
|
|
|
2017-02-27 10:57:44 +01:00
|
|
|
for (i = 0; i < target.metrics.length; i++) {
|
|
|
|
|
metric = target.metrics[i];
|
2017-12-20 12:33:33 +01:00
|
|
|
if (metric.type === 'count') {
|
2017-02-27 10:57:44 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2015-09-05 10:14:21 +02:00
|
|
|
|
2019-07-11 17:05:45 +02:00
|
|
|
const aggField: any = {};
|
|
|
|
|
let metricAgg: any = null;
|
2015-12-08 12:07:56 +01:00
|
|
|
|
2020-12-04 14:29:40 +00:00
|
|
|
if (isPipelineAggregation(metric)) {
|
|
|
|
|
if (isPipelineAggregationWithMultipleBucketPaths(metric)) {
|
2018-11-16 16:54:25 +01:00
|
|
|
if (metric.pipelineVariables) {
|
|
|
|
|
metricAgg = {
|
|
|
|
|
buckets_path: {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < metric.pipelineVariables.length; j++) {
|
|
|
|
|
pv = metric.pipelineVariables[j];
|
|
|
|
|
|
|
|
|
|
if (pv.name && pv.pipelineAgg && /^\d*$/.test(pv.pipelineAgg)) {
|
2020-12-04 14:29:40 +00:00
|
|
|
const appliedAgg = findMetricById(target.metrics, pv.pipelineAgg);
|
2018-11-16 16:54:25 +01:00
|
|
|
if (appliedAgg) {
|
|
|
|
|
if (appliedAgg.type === 'count') {
|
|
|
|
|
metricAgg.buckets_path[pv.name] = '_count';
|
|
|
|
|
} else {
|
|
|
|
|
metricAgg.buckets_path[pv.name] = pv.pipelineAgg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-15 19:06:47 +01:00
|
|
|
}
|
2018-11-16 16:54:25 +01:00
|
|
|
} else {
|
|
|
|
|
continue;
|
2018-11-15 19:06:47 +01:00
|
|
|
}
|
2015-12-09 14:21:48 +01:00
|
|
|
} else {
|
2020-12-04 14:29:40 +00:00
|
|
|
if (metric.field && /^\d*$/.test(metric.field)) {
|
|
|
|
|
const appliedAgg = findMetricById(target.metrics, metric.field);
|
2018-11-16 16:54:25 +01:00
|
|
|
if (appliedAgg) {
|
|
|
|
|
if (appliedAgg.type === 'count') {
|
|
|
|
|
metricAgg = { buckets_path: '_count' };
|
|
|
|
|
} else {
|
2020-12-04 14:29:40 +00:00
|
|
|
metricAgg = { buckets_path: metric.field };
|
2018-11-16 16:54:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-12-09 14:21:48 +01:00
|
|
|
}
|
2020-12-04 14:29:40 +00:00
|
|
|
} else if (isMetricAggregationWithField(metric)) {
|
2017-12-19 16:06:54 +01:00
|
|
|
metricAgg = { field: metric.field };
|
2017-02-27 10:57:44 +01:00
|
|
|
}
|
2015-12-08 12:07:56 +01:00
|
|
|
|
2021-01-13 09:04:36 +00:00
|
|
|
if (isMetricAggregationWithSettings(metric)) {
|
|
|
|
|
Object.entries(metric.settings || {})
|
|
|
|
|
.filter(([_, v]) => v !== null)
|
2021-03-05 12:48:45 +00:00
|
|
|
.forEach(([k, v]) => {
|
2021-05-14 11:50:15 +01:00
|
|
|
metricAgg[k] =
|
|
|
|
|
k === 'script' ? this.buildScript(getScriptValue(metric as MetricAggregationWithInlineScript)) : v;
|
2021-03-05 12:48:45 +00:00
|
|
|
});
|
2021-04-26 16:54:23 +01:00
|
|
|
|
|
|
|
|
// Elasticsearch isn't generally too picky about the data types in the request body,
|
|
|
|
|
// however some fields are required to be numeric.
|
|
|
|
|
// Users might have already created some of those with before, where the values were numbers.
|
|
|
|
|
if (metric.type === 'moving_avg') {
|
|
|
|
|
metricAgg = {
|
|
|
|
|
...metricAgg,
|
|
|
|
|
...(metricAgg?.window !== undefined && { window: this.toNumber(metricAgg.window) }),
|
|
|
|
|
...(metricAgg?.predict !== undefined && { predict: this.toNumber(metricAgg.predict) }),
|
|
|
|
|
...(isMovingAverageWithModelSettings(metric) && {
|
|
|
|
|
settings: {
|
|
|
|
|
...metricAgg.settings,
|
|
|
|
|
...Object.fromEntries(
|
|
|
|
|
Object.entries(metricAgg.settings || {})
|
|
|
|
|
// Only format properties that are required to be numbers
|
|
|
|
|
.filter(([settingName]) => ['alpha', 'beta', 'gamma', 'period'].includes(settingName))
|
|
|
|
|
// omitting undefined
|
|
|
|
|
.filter(([_, stringValue]) => stringValue !== undefined)
|
|
|
|
|
.map(([_, stringValue]) => [_, this.toNumber(stringValue)])
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
} else if (metric.type === 'serial_diff') {
|
|
|
|
|
metricAgg = {
|
|
|
|
|
...metricAgg,
|
|
|
|
|
...(metricAgg.lag !== undefined && {
|
|
|
|
|
lag: this.toNumber(metricAgg.lag),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-01-13 09:04:36 +00:00
|
|
|
}
|
2017-02-27 10:57:44 +01:00
|
|
|
|
|
|
|
|
aggField[metric.type] = metricAgg;
|
|
|
|
|
nestedAggs.aggs[metric.id] = aggField;
|
2015-09-03 14:55:48 +02:00
|
|
|
}
|
2015-09-03 11:14:25 +02:00
|
|
|
|
2015-07-01 08:54:06 -04:00
|
|
|
return query;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2015-07-01 08:54:06 -04:00
|
|
|
|
2021-05-14 11:50:15 +01:00
|
|
|
private buildScript(script: string) {
|
|
|
|
|
if (gte(this.esVersion, '5.6.0')) {
|
|
|
|
|
return script;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
inline: script,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 16:54:23 +01:00
|
|
|
private toNumber(stringValue: unknown): unknown | number {
|
|
|
|
|
const parsedValue = parseFloat(`${stringValue}`);
|
|
|
|
|
if (isNaN(parsedValue)) {
|
|
|
|
|
return stringValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parsedValue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 17:05:45 +02:00
|
|
|
getTermsQuery(queryDef: any) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const query: any = {
|
2017-12-19 16:06:54 +01:00
|
|
|
size: 0,
|
|
|
|
|
query: {
|
|
|
|
|
bool: {
|
2017-12-20 12:33:33 +01:00
|
|
|
filter: [{ range: this.getRangeFilter() }],
|
|
|
|
|
},
|
|
|
|
|
},
|
2016-12-06 16:04:31 +01:00
|
|
|
};
|
2016-10-02 16:59:25 +02:00
|
|
|
|
2016-12-06 16:04:31 +01:00
|
|
|
if (queryDef.query) {
|
2017-01-21 08:31:29 +01:00
|
|
|
query.query.bool.filter.push({
|
2017-12-19 16:06:54 +01:00
|
|
|
query_string: {
|
|
|
|
|
analyze_wildcard: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
query: queryDef.query,
|
|
|
|
|
},
|
2016-12-06 16:04:31 +01:00
|
|
|
});
|
2016-05-10 19:38:22 +02:00
|
|
|
}
|
2017-03-29 16:03:15 +02:00
|
|
|
|
2018-08-30 09:03:11 +02:00
|
|
|
let size = 500;
|
2017-03-29 16:03:15 +02:00
|
|
|
if (queryDef.size) {
|
2017-01-25 11:29:41 +00:00
|
|
|
size = queryDef.size;
|
|
|
|
|
}
|
2017-03-29 16:03:15 +02:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
query.aggs = {
|
2017-12-20 12:33:33 +01:00
|
|
|
'1': {
|
2017-12-19 16:06:54 +01:00
|
|
|
terms: {
|
|
|
|
|
field: queryDef.field,
|
|
|
|
|
size: size,
|
2019-09-16 16:41:53 +01:00
|
|
|
order: {},
|
2017-12-20 12:33:33 +01:00
|
|
|
},
|
|
|
|
|
},
|
2015-09-22 09:31:58 +02:00
|
|
|
};
|
2018-11-02 17:52:40 +01:00
|
|
|
|
2019-09-16 16:41:53 +01:00
|
|
|
// Default behaviour is to order results by { _key: asc }
|
|
|
|
|
// queryDef.order allows selection of asc/desc
|
|
|
|
|
// queryDef.orderBy allows selection of doc_count ordering (defaults desc)
|
|
|
|
|
|
|
|
|
|
const { orderBy = 'key', order = orderBy === 'doc_count' ? 'desc' : 'asc' } = queryDef;
|
|
|
|
|
|
|
|
|
|
if (['asc', 'desc'].indexOf(order) < 0) {
|
|
|
|
|
throw { message: `Invalid query sort order ${order}` };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (orderBy) {
|
|
|
|
|
case 'key':
|
|
|
|
|
case 'term':
|
2021-05-11 09:44:00 +01:00
|
|
|
const keyname = gte(this.esVersion, '6.0.0') ? '_key' : '_term';
|
2019-09-16 16:41:53 +01:00
|
|
|
query.aggs['1'].terms.order[keyname] = order;
|
|
|
|
|
break;
|
|
|
|
|
case 'doc_count':
|
|
|
|
|
query.aggs['1'].terms.order['_count'] = order;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw { message: `Invalid query sort type ${orderBy}` };
|
2018-11-02 17:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-22 09:31:58 +02:00
|
|
|
return query;
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|
2019-06-24 22:15:03 +02:00
|
|
|
|
2021-04-13 17:39:07 +01:00
|
|
|
getLogsQuery(target: ElasticsearchQuery, limit: number, adhocFilters?: any, querystring?: string) {
|
2019-06-24 22:15:03 +02:00
|
|
|
let query: any = {
|
|
|
|
|
size: 0,
|
|
|
|
|
query: {
|
|
|
|
|
bool: {
|
|
|
|
|
filter: [{ range: this.getRangeFilter() }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-21 10:39:55 +01:00
|
|
|
this.addAdhocFilters(query, adhocFilters);
|
|
|
|
|
|
2019-06-24 22:15:03 +02:00
|
|
|
if (target.query) {
|
|
|
|
|
query.query.bool.filter.push({
|
|
|
|
|
query_string: {
|
|
|
|
|
analyze_wildcard: true,
|
2019-12-05 11:55:03 +01:00
|
|
|
query: querystring,
|
2019-06-24 22:15:03 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 17:39:07 +01:00
|
|
|
query = this.documentQuery(query, limit);
|
2019-06-24 22:15:03 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...query,
|
|
|
|
|
aggs: this.build(target, null, querystring).aggs,
|
2021-02-24 09:31:17 -07:00
|
|
|
highlight: {
|
|
|
|
|
fields: {
|
|
|
|
|
'*': {},
|
|
|
|
|
},
|
|
|
|
|
pre_tags: [highlightTags.pre],
|
|
|
|
|
post_tags: [highlightTags.post],
|
|
|
|
|
fragment_size: 2147483647,
|
|
|
|
|
},
|
2019-06-24 22:15:03 +02:00
|
|
|
};
|
|
|
|
|
}
|
2017-09-28 12:52:39 +02:00
|
|
|
}
|