简体   繁体   English

如何将Elastic Search与Meanjs(Mongodb,Express,Angular,Nodejs)集成

[英]How to integrate Elastic Search with Meanjs (Mongodb ,Express,Angular,Nodejs)

I have installed MEANJS with grunt . 我已经用grunt安装了MEANJS。 its existing Modules are working Properly. 其现有的模块运行正常。 The issue is i am trying to Integrate Elastic Search with angular Js . 问题是我正在尝试将Elastic Search与angular Js集成在一起。 But didn't get any Proper Solution. 但是没有得到适当的解决方案。 When i am Connecting elastic search to server.js .then on terminal it shows the search result. 当我将弹性搜索连接到server.js时,然后在终端上显示搜索结果。 how to diplay the search result through angular js on Home Page. 如何通过主页上的angular js显示搜索结果。

I also want to connect the elastic database with mongodb database so that elastic search is auto update. 我还想将弹性数据库与mongodb数据库连接,以便弹性搜索是自动更新的。 Any Suggestion is very helpful for me. 任何建议对我都很有帮助。 for connecting through elastic search i am using 通过弹性搜索进行连接,我正在使用

  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
    };
}]
 );

Essentially what you want to do is this: 本质上,您要执行的操作是:

  • Run an Elastic Search (ES) server. 运行弹性搜索(ES)服务器。
  • On your server-side code (MEAN), you'll write a route that handles searching. 在服务器端代码(MEAN)上,您将编写一条处理搜索的路由。
  • Make your Angular code send requests to your backend route that does searching via ES. 使您的Angular代码将请求发送到通过ES搜索的后端路由。

You don't want to have Angular directly speak with ES over the network -- AFAIK there's no way to safely do this. 您不想让Angular通过网络直接与ES对话-AFA​​IK无法安全地做到这一点。

Hi finally Got the solution . 嗨,终于有了解决方案。

I have attached elastic.angular.js file /var/www/meanjs/config/env/all.js 我已经附加了elastic.angular.js文件/var/www/meanjs/config/env/all.js

and in the /var/www/meanjs/public/modules/core/controllers/home.client.controller. 并在/var/www/meanjs/public/modules/core/controllers/home.client.controller中。 I have write the following code and its working smoothly with search. 我已经编写了以下代码,它在搜索中可以正常工作。

 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();
}]
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM