From e4950c2dc1012cb1a36cca947666244417e56194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 12 Jul 2017 07:43:12 +0200 Subject: [PATCH] fix: elasticsearch with template variable with terms agg on IP field, fixes #8662 --- .../datasource/elasticsearch/datasource.js | 13 ++++---- .../elasticsearch/specs/datasource_specs.ts | 33 ++++++++++++++++--- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/datasource.js b/public/app/plugins/datasource/elasticsearch/datasource.js index 02511f4900b..49398a894e7 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.js +++ b/public/app/plugins/datasource/elasticsearch/datasource.js @@ -323,26 +323,27 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes var buckets = res.responses[0].aggregations["1"].buckets; return _.map(buckets, function(bucket) { - return {text: bucket.key, value: bucket.key}; + return { + text: bucket.key_as_string || bucket.key, + value: bucket.key + }; }); }); }; this.metricFindQuery = function(query) { query = angular.fromJson(query); - query.query = templateSrv.replace(query.query || '*', {}, 'lucene'); - if ('field' in query) { - query.field = templateSrv.replace(query.field, {}, 'lucene'); - } - if (!query) { return $q.when([]); } if (query.find === 'fields') { + query.field = templateSrv.replace(query.field, {}, 'lucene'); return this.getFields(query); } + if (query.find === 'terms') { + query.query = templateSrv.replace(query.query || '*', {}, 'lucene'); return this.getTerms(query); } }; diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts index f9684a3cd3f..d0133256982 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts @@ -248,7 +248,7 @@ describe('ElasticDatasource', function() { }); describe('When issuing metricFind query on es5.x', function() { - var requestOptions, parts, header, body; + var requestOptions, parts, header, body, results; beforeEach(function() { createDatasource({url: 'http://es.com', index: 'test', jsonData: {esVersion: '5'}}); @@ -256,13 +256,27 @@ describe('ElasticDatasource', function() { ctx.backendSrv.datasourceRequest = function(options) { requestOptions = options; return ctx.$q.when({ - data: { - responses: [{aggregations: {"1": [{buckets: {text: 'test', value: '1'}}]}}] - } + data: { + responses: [ + { + aggregations: { + "1": { + buckets: [ + {doc_count: 1, key: 'test'}, + {doc_count: 2, key: 'test2', key_as_string: 'test2_as_string'}, + ] + } + } + } + ] + } }); }; - ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}'); + ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => { + results = res; + }); + ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); @@ -270,6 +284,15 @@ describe('ElasticDatasource', function() { body = angular.fromJson(parts[1]); }); + it('should get results', function() { + expect(results.length).to.eql(2); + }); + + it('should use key or key_as_string', function() { + expect(results[0].text).to.eql('test'); + expect(results[1].text).to.eql('test2_as_string'); + }); + it('should not set search type to count', function() { expect(header.search_type).to.not.eql('count'); });