Elastic Search : Sample code

Sample code to test elastic search with nodejs module



//Install module elastic search
var elasticsearch = require('elasticsearch');

//Create a new client

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

//Try out connection, Start elasticsearch server ..

client.ping({
  // ping usually has a 3000ms timeout
  requestTimeout: 1000
}, function (error) {
  if (error) {
    console.trace('elasticsearch cluster is down!');
  } else {
    console.log('successful connection .');
  }
});

//Search in elastic

client.search({
  index: 'twitter',
  type: 'tweet',
  body: {
    query: {
      match: {
        message: 'Elasticsearch'
      }
    }
  }
}).then(function (resp) {
    var hits = resp.hits.hits;
}, function (err) {
    console.trace(err.message);
});

Source : https://github.com/elastic/elasticsearch-js

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s