Explorar el Código

removed dependence on elasticsearch js client from search, will try to remove this depdence in other places and just use $http, elasticsearch js client is to big and does not provide enough value

Torkel Ödegaard hace 11 años
padre
commit
95925aafb1

+ 17 - 42
src/app/controllers/search.js

@@ -9,7 +9,7 @@ function (angular, _, config, $) {
 
 
   var module = angular.module('kibana.controllers');
   var module = angular.module('kibana.controllers');
 
 
-  module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, ejsResource) {
+  module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, ejsResource, elasticClient) {
 
 
     $scope.init = function() {
     $scope.init = function() {
       $scope.ejs = ejsResource(config.elasticsearch, config.elasticsearchBasicAuth);
       $scope.ejs = ejsResource(config.elasticsearch, config.elasticsearchBasicAuth);
@@ -49,30 +49,31 @@ function (angular, _, config, $) {
       }
       }
     };
     };
 
 
-    $scope.searchDasboards = function(query) {
-      var request = $scope.ejs.Request().indices(config.grafana_index).types('dashboard');
-      var tagsOnly = query.indexOf('tags!:') === 0;
+    $scope.searchDasboards = function(queryString) {
+      var tagsOnly = queryString.indexOf('tags!:') === 0;
       if (tagsOnly) {
       if (tagsOnly) {
-        var tagsQuery = query.substring(6, query.length);
-        query = 'tags:' + tagsQuery + '*';
+        var tagsQuery = queryString.substring(6, queryString.length);
+        queryString = 'tags:' + tagsQuery + '*';
       }
       }
       else {
       else {
-        if (query.length === 0) {
-          query = 'title:';
+        if (queryString.length === 0) {
+          queryString = 'title:';
         }
         }
 
 
-        if (query[query.length - 1] !== '*') {
-          query += '*';
+        if (queryString[queryString.length - 1] !== '*') {
+          queryString += '*';
         }
         }
       }
       }
 
 
-      return request
-        .query($scope.ejs.QueryStringQuery(query))
-        .sort('_uid')
-        .facet($scope.ejs.TermsFacet("tags").field("tags").order('term').size(50))
-        .size(20).doSearch()
-        .then(function(results) {
+      var query = {
+        query: { query_string: { query: queryString } },
+        facets: { tags: { terms: { field: "tags", order: "term", size: 50 } } },
+        size: 20,
+        sort: ["_uid"]
+      };
 
 
+      return elasticClient.post('dashboard/_search', query)
+        .then(function(results) {
           if(_.isUndefined(results.hits)) {
           if(_.isUndefined(results.hits)) {
             $scope.results.dashboards = [];
             $scope.results.dashboards = [];
             $scope.results.tags = [];
             $scope.results.tags = [];
@@ -115,32 +116,6 @@ function (angular, _, config, $) {
         $scope.searchDasboards(queryStr);
         $scope.searchDasboards(queryStr);
         return;
         return;
       }
       }
-
-      queryStr = queryStr.substring(2, queryStr.length);
-
-      var words = queryStr.split(' ');
-      var query = $scope.ejs.BoolQuery();
-      var terms = _.map(words, function(word) {
-        return $scope.ejs.MatchQuery('metricPath_ng', word).boost(1.2);
-      });
-
-      var ngramQuery = $scope.ejs.BoolQuery();
-      ngramQuery.must(terms);
-
-      var fieldMatchQuery = $scope.ejs.FieldQuery('metricPath', queryStr + "*").boost(1.2);
-      query.should([ngramQuery, fieldMatchQuery]);
-
-      var request = $scope.ejs.Request().indices(config.grafana_index).types('metricKey');
-      var results = request.query(query).size(20).doSearch();
-
-      results.then(function(results) {
-        if (results && results.hits && results.hits.hits.length > 0) {
-          $scope.results.metrics = { metrics: results.hits.hits };
-        }
-        else {
-          $scope.results.metrics = { metric: [] };
-        }
-      });
     };
     };
 
 
     $scope.openSearch = function (evt) {
     $scope.openSearch = function (evt) {

+ 1 - 1
src/app/services/all.js

@@ -9,6 +9,6 @@ define([
   './annotationsSrv',
   './annotationsSrv',
   './playlistSrv',
   './playlistSrv',
   './unsavedChangesSrv',
   './unsavedChangesSrv',
-  './elasticsearch/es-client2',
+  './elasticsearch/es-client',
 ],
 ],
 function () {});
 function () {});

+ 35 - 0
src/app/services/elasticsearch/es-client.js

@@ -0,0 +1,35 @@
+define([
+  'angular',
+  'config'
+],
+function(angular, config) {
+  "use strict";
+
+  var module = angular.module('kibana.services');
+
+  module.service('elasticClient', function($http) {
+
+    this.post = function(url, data) {
+
+      var options = {
+        url: config.elasticsearch + "/" + config.grafana_index + "/" + url,
+        method: 'POST',
+        data: data
+      };
+
+      if (config.elasticsearchBasicAuth) {
+        options.headers = {
+          "Authorization": "Basic " + config.elasticsearchBasicAuth
+        };
+      }
+
+      return $http(options)
+        .then(function(results) {
+          return results.data;
+        }, function(results) {
+          return results.data;
+        });
+    };
+
+  });
+});