Search code examples
ember.jselasticsearchember-cli

Including Elasticsearch in Ember-CLI application


I am new to Ember and Node, and I would like to use Elasticsearch with Ember.

I'm using the npm elasticsearch package.

From what I gather, I should inject ES as a service, but I'm not sure where to include the ES initialization code and register the client with the Ember app.

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});`

All I've got right now is ember g service elasticsearch

I appreciate the help for a noob!


Solution

  • You seem to want to use the elastic search client directly in your ember application (correct me if I am wrong). Unfortunately, you cannot use the node module directly within ember so you will need to architecture your application differently.

    You have a few options:

    1) Use a library like Ember Data Adapter for elasticsearch or ember-data-elasticsearch-kit in your ember application (which will run in the browser) to query elastic search. (I picked these since they were the first results in Google - they seem to be a bit out of date, but perhaps you can find something with more recent updates.)

    2) Create an API endpoint with node (and perhaps something like Express) and use the elasticsearch module and elastic search client to query elastic search on the server and return JSON which you can consume in your ember application. So, for instance:

        $.ajax({
           url: 'UrlOfMethodExposedByNode',
           type: 'GET',
           accepts: 'application/json',
           success: function(data) {
               // you can use the data here to set a value on a controller, etc
           },
           error: function() {
               // something went wrong while retrieving data from your API
           }
       });
    

    More details will depend on the specifics of your application.