Commit e4950c2d by Torkel Ödegaard

fix: elasticsearch with template variable with terms agg on IP field, fixes #8662

parent 4844bf9b
......@@ -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);
}
};
......
......@@ -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');
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment