2

I have installed MEANJS with grunt . its existing Modules are working Properly. The issue is i am trying to Integrate Elastic Search with 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. how to diplay the search result through angular js on Home Page.

I also want to connect the elastic database with mongodb database so that elastic search is auto update. 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
    };
}]
 );
3
  • You don't provide enough information or explain a specific problem that would make it possible to help. Check out the guide on asking questions for some advice on this matter. Commented Nov 4, 2014 at 20:05
  • Hi Matthew my issue is how to integrate elastic search with MEANJS Commented Nov 4, 2014 at 20:10
  • possible duplicate of Example of Angular and Elasticsearch Commented May 20, 2015 at 8:44

2 Answers 2

3

Essentially what you want to do is this:

  • Run an Elastic Search (ES) server.
  • On your server-side code (MEAN), you'll write a route that handles searching.
  • Make your Angular code send requests to your backend route that does searching via ES.

You don't want to have Angular directly speak with ES over the network -- AFAIK there's no way to safely do this.

Sign up to request clarification or add additional context in comments.

2 Comments

hi got the idea but can you provide detailed description with code. it will be very helpful for me .
I can't really show any examples as I'd have to build an entire site to showcase what I'm talking about :(
0

Hi finally Got the solution .

I have attached elastic.angular.js file /var/www/meanjs/config/env/all.js

and in the /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();
}]
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.