0

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!

blisstdev
  • 633
  • 4
  • 13

1 Answers1

1

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.

Oren Hizkiya
  • 4,420
  • 2
  • 23
  • 33
  • Thanks Oren. Yes I saw those adapters and that they were out of date, so I shied away from them. My intention was to do something like #2, but I was unsure how to set that up. – blisstdev Feb 09 '15 at 22:09
  • If you add some more specifics about how you are trying to use elasticsearch within your application I can try to provide more details about a possible implementation so it will be easier to fill in the gaps. – Oren Hizkiya Feb 09 '15 at 22:31
  • I am implementing an Instant Search in Ember. I have external XML data that I would like to load into ES, which a user can then Instant Search through. I would like there to be parameters which determine relevancy to the user which I can define in ES. – blisstdev Feb 13 '15 at 23:27
  • Also is there a downside to doing this: http://stackoverflow.com/questions/28201036/add-node-module-to-ember-cli-app – blisstdev Feb 14 '15 at 00:19
  • No downside other than having another package bundled along with your app that will increase the total size of the page that has been downloaded when someone visits your app. – Oren Hizkiya Feb 15 '15 at 00:06