Browse Source

fix(elasticsearch): fixed proper json escaping for lucene query, fixes #2981

Torkel Ödegaard 10 years ago
parent
commit
ae93f2b936

+ 4 - 1
public/app/plugins/datasource/elasticsearch/datasource.js

@@ -159,7 +159,10 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
         if (target.hide) {return;}
 
         var esQuery = angular.toJson(this.queryBuilder.build(target));
-        esQuery = esQuery.replace("$lucene_query", target.query || '*');
+        var luceneQuery = angular.toJson(target.query || '*');
+        // remove inner quotes
+        luceneQuery = luceneQuery.substr(1, luceneQuery.length - 2);
+        esQuery = esQuery.replace("$lucene_query", luceneQuery);
 
         payload += header + '\n' + esQuery + '\n';
         sentTargets.push(target);

+ 14 - 6
public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts

@@ -42,16 +42,15 @@ describe('ElasticDatasource', function() {
   });
 
   describe('When issueing metric query with interval pattern', function() {
+    var requestOptions, parts, header;
+
     beforeEach(function() {
       ctx.ds = new ctx.service({
         url: 'http://es.com',
         index: '[asd-]YYYY.MM.DD',
         jsonData: { interval: 'Daily' }
       });
-    });
 
-    it('should translate index pattern to current day', function() {
-      var requestOptions;
       ctx.backendSrv.datasourceRequest = function(options) {
         requestOptions = options;
         return ctx.$q.when({data: {responses: []}});
@@ -62,13 +61,22 @@ describe('ElasticDatasource', function() {
           from: moment([2015, 4, 30, 10]),
           to: moment([2015, 5, 1, 10])
         },
-        targets: [{ bucketAggs: [], metrics: [] }]
+        targets: [{ bucketAggs: [], metrics: [], query: 'escape\\:test' }]
       });
 
       ctx.$rootScope.$apply();
-      var parts = requestOptions.data.split('\n');
-      var header = angular.fromJson(parts[0]);
+
+      parts = requestOptions.data.split('\n');
+      header = angular.fromJson(parts[0]);
+    });
+
+    it('should translate index pattern to current day', function() {
       expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
     });
+
+    it('should json escape lucene query', function() {
+      var body = angular.fromJson(parts[1]);
+      expect(body.query.filtered.query.query_string.query).to.be('escape\\:test');
+    });
   });
 });

+ 1 - 1
public/app/plugins/datasource/elasticsearch/specs/index_pattern_specs.ts

@@ -8,7 +8,7 @@ declare var IndexPattern: any;
 
 describe('IndexPattern', function() {
 
-  describe.only('when getting index for today', function() {
+  describe('when getting index for today', function() {
     it('should return correct index name', function() {
       var pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
       var expected = 'asd-' + moment().format('YYYY.MM.DD');