2014-11-04 34 views
2

我已經用grunt安裝了MEANJS。其現有的模塊正常工作。問題是我正在嘗試使用角度Js來集成彈性搜索。但沒有得到任何適當的解決方案。當我將彈性搜索連接到server.js時,然後在終端上顯示搜索結果。如何通過主頁上的角度js顯示搜索結果。如何整合彈性搜索與Meanjs(Mongodb,Express,Angular,Nodejs)

我也想連接彈性數據庫與mongodb數據庫,以便彈性搜索自動更新。任何建議對我都很有幫助。 通過彈性連接搜索我使用

var MyOpenRecipes = angular.module('myOpenRecipes', ['elasticsearch'], 
['$locationProvider', function($locationProvider){ 
    $locationProvider.html5Mode(true); 
}] 
); 


    MyOpenRecipes.factory('recipeService', 
['$q', 'esFactory', '$location', function($q, elasticsearch, $location){ 
    var client = elasticsearch({ 
     host: $location.host() + ":9200" 
    }); 

    /** 
    * Given a term and an offset, load another round of 10 recipes. 
    * 
    * Returns a promise. 
    */ 
    var search = function(term, offset){ 
     var deferred = $q.defer(); 
     var query = { 
      "match": { 
       "_all": term 
      } 
     }; 

     client.search({ 
      "index": 'facilities', 
      "type": 'facility', 
      "body": { 
       "size": 10, 
       "from": (offset || 0) * 10, 
       "query": query 
      } 
     }).then(function(result) { 
      var ii = 0, hits_in, hits_out = []; 
      hits_in = (result.hits || {}).hits || []; 
      for(;ii < hits_in.length; ii++){ 
       hits_out.push(hits_in[ii]._source); 
      } 
      deferred.resolve(hits_out); 
     }, deferred.reject); 

     return deferred.promise; 
    }; 


    return { 
     "search": search 
    }; 
}] 
); 
+0

您不提供足夠的信息或解釋可能會有助於解決的特定問題。查詢[指導](http://stackoverflow.com/help/how-to-ask)提出有關此問題的建議。 – 2014-11-04 20:05:15

+0

您好馬修我的問題是如何整合彈性搜索與MEANJS – VipinS 2014-11-04 20:10:15

+0

可能的副本[角和Elasticsearch](http://stackoverflow.com/questions/22661996/example-of-angular-and-elasticsearch) – Manube 2015-05-20 08:44:53

回答

0

嗨,終於有了解決辦法。

我重視elastic.angular.js文件/var/www/meanjs/config/env/all.js

,並在/ var/WWW/meanjs /公/模塊/核心/控制器/家.client.controller。我寫了下面的代碼,並且在搜索過程中順利運行。

angular.module('core').factory('recipeService', 
['$q', 'esFactory', '$location', function($q, elasticsearch, $location){ 
    var client = elasticsearch({ 
     host: $location.host() + ':9200' 
    }); 

    /** 
    * Given a term and an offset, load another round of 10 recipes. 
    * 
    * Returns a promise. 
    */ 
    var search = function(term, offset){ 
     var deferred = $q.defer(); 
     var query = { 
      'match': { 
       '_all': term 
      } 
     }; 

     client.search({ 
      'index': 'facilities', 
      'type': 'facility', 
      'body': { 
       'size': 10, 
       'from': (offset || 0) * 10, 
       'query': query 
      } 
     }).then(function(result) { 
      var ii = 0, hits_in, hits_out = []; 
      hits_in = (result.hits || {}).hits || []; 
      for(;ii < hits_in.length; ii++){ 
       hits_out.push(hits_in[ii]._source); 
      } 
      deferred.resolve(hits_out); 
     }, deferred.reject); 

     return deferred.promise; 
    }; 


    return { 
     'search': search 
    }; 
    }] 
); 

angular.module('core').controller('recipeCtrl', 
['recipeService', '$scope', '$location', function(recipes, $scope, $location){ 
    // Provide some nice initial choices 
    var initChoices = [ 
     'ADS AMBULATORY SURGERY CTR', 
     'NOVAMED EYE SURGERY CENTER OF OVERLAND PARK', 
     'DISCOVER VISION SURGERY & LASER CENTER LLC', 
     'HUTCHINSON AMBULATORY SURGERY CENTER LLC', 
     'SHAWNEE MISSION PRAIRIE STAR SURGERY CENTER LLC', 
     'LASER CENTER', 
     'QUINLAN EYE SURGERY & LASER CENTER', 
     'ADS AMBULATORY SURGERY CTR' 
    ]; 
    var idx = Math.floor(Math.random() * initChoices.length); 

    // Initialize the scope defaults. 
    $scope.recipes = [];  // An array of recipe results to display 
    $scope.page = 0;   // A counter to keep track of our current page 
    $scope.allResults = false; // Whether or not all results have been found. 

    // And, a random search term to start if none was present on page load. 
    $scope.searchTerm = $location.search().q || initChoices[idx]; 

    /** 
    * A fresh search. Reset the scope variables to their defaults, set 
    * the q query parameter, and load more results. 
    */ 
    $scope.search = function(){ 
     $scope.page = 0; 
     $scope.recipes = []; 
     $scope.allResults = false; 
     $location.search({'q': $scope.searchTerm}); 
     $scope.loadMore(); 
    }; 

    /** 
    * Load the next page of results, incrementing the page counter. 
    * When query is finished, push results onto $scope.recipes and decide 
    * whether all results have been returned (i.e. were 10 results returned?) 
    */ 
    $scope.loadMore = function(){ 
     recipes.search($scope.searchTerm, $scope.page++).then(function(results){ 
      if(results.length !== 10){ 
       $scope.allResults = true; 
      } 

      var ii = 0; 
      for(;ii < results.length; ii++){ 
       $scope.recipes.push(results[ii]); 
      } 
     }); 
    }; 

    // Load results on first run 
    $scope.loadMore(); 
}] 
); 
+0

此代碼適合我。 – 2016-01-08 11:00:24

3

基本上你想要做的是這樣的:

  • 運行的彈性搜索(ES)服務器。
  • 在您的服務器端代碼(MEAN)上,您將編寫處理搜索的路由。
  • 使您的Angular代碼將請求發送到通過ES進行搜索的後端路由。

你不想讓Angular直接和網絡上的ES對話 - AFAIK沒有辦法安全地做到這一點。

+0

嗨得到這個想法,但你可以提供詳細的代碼描述。這對我很有幫助。 – VipinS 2014-11-05 16:44:44

+0

我無法真正展示任何示例,因爲我必須構建整個網站才能展示我在說什麼:( – rdegges 2014-11-05 22:53:21