Achyut Kr Deka Achyut Kr Deka - 3 months ago 47
Node.js Question

ElasticSearch with Express framework

I want to built a application and a api which will mainly retrieve a resource, Now I have heard a lot about

Nodejs
and
ElasticSearch
and I know little bit Nodejs and Express framework. But I don't know how will I integrated ElasticSearch with Express framework.

SCC SCC
Answer
  • 1st download ElasticSearch
  • Inside your project / Express framework directory download ElasticSearch express driver using npm install elasticsearch --save
  • Run ElasticSearch Instance, By default it runs on the port 9200
  • To use that module, simply create a client instance

    var elasticsearch = require('elasticsearch');
    var client = elasticsearch.Client({
      host: 'localhost:9200'
    });
    
    client.search({
      index: 'books',
      type: 'book',
      body: {
        query: {
          multi_match: {
            query: 'express js',
            fields: ['title', 'description']
          }
        }
      }
    }).then(function(response) {
      var hits = response.hits.hits;
    }, function(error) {
      console.trace(error.message);
    });
    
  • Helpfull Link https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/

Comments