2

我已经用 grunt 安装了 MEANJS。它现有的模块工作正常。问题是我正在尝试将 Elastic Search 与 angular Js 集成。但没有得到任何适当的解决方案。当我将弹性搜索连接到 server.js 时。然后在终端上显示搜索结果。如何在主页上通过angular js显示搜索结果。

我还想将弹性数据库与 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
    };
}]
 );
4

2 回答 2

3

基本上你想要做的是:

  • 运行弹性搜索 (ES) 服务器。
  • 在您的服务器端代码 (MEAN) 上,您将编写一个处理搜索的路由。
  • 让您的 Angular 代码向通过 ES 搜索的后端路由发送请求。

你不想让 Angular 通过网络直接与 ES 对话——AFAIK 没有办法安全地做到这一点。

于 2014-11-05T01:05:04.347 回答
0

嗨终于得到了解决方案。

我附上了 elastic.angular.js 文件 /var/www/meanjs/config/env/all.js

在 /var/www/meanjs/public/modules/core/controllers/home.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();
}]
);
于 2014-11-10T17:12:09.737 回答