From a0beaa3bbe979f0697dca9a38d0e0bdf6dec7b1d Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Tue, 15 Sep 2020 09:14:47 +0100 Subject: [PATCH] Elasticsearch: Allow fields starting with underscore (#27520) --- .../elasticsearch/datasource.test.ts | 7 +++++- .../datasource/elasticsearch/datasource.ts | 24 ++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/datasource.test.ts b/public/app/plugins/datasource/elasticsearch/datasource.test.ts index ec0df08409f..4be972aef25 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.test.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.test.ts @@ -378,8 +378,12 @@ describe('ElasticDatasource', function(this: any) { mappings: { metricsets: { _all: {}, + _meta: { + test: 'something', + }, properties: { '@timestamp': { type: 'date' }, + __timestamp: { type: 'date' }, beat: { properties: { name: { @@ -426,6 +430,7 @@ describe('ElasticDatasource', function(this: any) { const fields = _.map(fieldObjects, 'text'); expect(fields).toEqual([ '@timestamp', + '__timestamp', 'beat.name.raw', 'beat.name', 'beat.hostname', @@ -455,7 +460,7 @@ describe('ElasticDatasource', function(this: any) { }); const fields = _.map(fieldObjects, 'text'); - expect(fields).toEqual(['@timestamp']); + expect(fields).toEqual(['@timestamp', '__timestamp']); }); }); diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index fd1113d1616..bba6f8146db 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -22,6 +22,20 @@ import { TemplateSrv } from 'app/features/templating/template_srv'; import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { DataLinkConfig, ElasticsearchOptions, ElasticsearchQuery } from './types'; +// Those are metadata fields as defined in https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-fields.html#_identity_metadata_fields. +// custom fields can start with underscores, therefore is not safe to exclude anything that starts with one. +const ELASTIC_META_FIELDS = [ + '_index', + '_type', + '_id', + '_source', + '_size', + '_field_names', + '_ignored', + '_routing', + '_meta', +]; + export class ElasticDatasource extends DataSourceApi { basicAuth?: string; withCredentials?: boolean; @@ -426,6 +440,10 @@ export class ElasticDatasource extends DataSourceApi { @@ -441,8 +459,8 @@ export class ElasticDatasource extends DataSourceApi { + if (this.isMetadataField(key)) { return false; } @@ -452,7 +470,7 @@ export class ElasticDatasource extends DataSourceApi system.process.cpu.total const fieldNameParts: any = [];