简体   繁体   中英

elasticsearch with angularjs or node js

I am working on MEAN stack application which also uses elasticsearch to search records. I am using following code from angularJS controller to call the elasticsearch server

instantResult: function(term) {
var client = elasticsearch({
            host: $location.host() + ':9200'
        });
        var deferred = $q.defer();
        client.search({ 
//Query here

}).then(function(result) {
                var hits = result.hits.hits;
                deferred.resolve(hits);
            },
            function(err) {
                console.trace(err.message);
            }, deferred.reject);
        return deferred.promise;
}

Here we are calling elasticsearch server from client.

Same functionality that I am doing above can be achieved from Node Js ie server side by passing data using rest call and receive it back in controller.

My question is

  1. Which is the proper way to search.
  2. Port 9200 is blocked at many org so I am not able to get the search result.By using client side search as shown in code above,I am accessing that host:9200 separately.I mean not through the flow of my website(relative path).

Thanks for help.

Port 9200 is to access their APIs , when you call their RESTFUl API you use that port , since you said you are blocked to use 9200 port, the other way to talk to elasticsearch is to use transport client which runs on port 9300 . I am not sure if Node.js can use transport client,but in the server side you can use spring data or java client.

Take a look at the below link

http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/

connect Elastic Search using AngularJS like below.

Create new app.js file 建立新的app.js档案

var app = angular.module('testApp', ['elasticsearch']);

app.factory('elasticClient', ['esFactory', function (esFactory) {
    return esFactory({
       host: 'http://localhost:9200',
       sniffOnStart: true,
       sniffInterval: 300000
    });
}]);

In controller use below code 在控制器中使用以下代码

app.controller('mainController', function ($scope, elasticClient) {
$scope.results = [];
$scope.search = {
    queryTerm: ''
};

$scope.search = function () {
    elasticClient.search({
        index: 'books',
        size: 20,
        body: {
            'query': {
                'query_string': {
                    'query': $scope.search.queryTerm
                }
            }
        }
    }).then(function (response) {
        $scope.results = response.hits.hits;
    });   
}
});

Have a look https://github.com/vhvinod/AngularJS-ElasticSearch

First of all you should never open port 9200 to public in your production environment. You are simply letting anyone to fire queries directly to your database. Anyone can push your elasticsearch out of memory or simply pull down all your data. This may lead to havoc. So port 9200 should be always blocked on box.
Another reason to make your nodeJS to communicate with ES and expose it as APIs, is that your application will be decoupled from your server. So you can easily reuse same APIs if you want to build any mobile application. Otherwise your client logic will be tightly coupled, and you will be repeating a lot of codes everywhere. And more code means more maintenance and more bugs.
So, you should communicate with your nodeJS server and your server should talk to elasticsearch. Again this configuration will depend on how your boxes are arranged and communicating with each other, in case your nodejs application and elasticsearch are on different boxes.

For testing locally, I generally map my port 9200 to dev ES port 9200 using ssh. This gives me a feeling of having ES on my system. Again this will work only if you can ssh directly to your server and there are not intermediate boxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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