Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 759
Achyut Kr Deka Avatar asked Aug 20 '16 17:08

Achyut Kr Deka


People also ask

Can we use Elasticsearch with node js?

Elasticsearch lets you search through vast amounts of data, whether you're implementing real-time search experiences or doing in-depth data analysis. In this tutorial, you'll learn how to integrate Elasticsearch into your Node. js app.

Is Express JS good for big projects?

Middleware frameworks, like Express. js, are suitable for small and medium projects. If you are going to develop a large project that will be supported by a large team of developers, Express. js is not the best choice.

Is express good framework?

Advantages Of Express JS It makes configuration of the Express. js apps and customization your web development easily. js is an excellent framework that helps developers to define the routing of web-apps. It helps template engine integrations, especially with engines like Jade, Vash, and EJS.


2 Answers

  • 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;
    }).catch(function (error) {
      console.trace(error.message);
    });
    
  • Helpfull Link https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/

like image 126
SCC Avatar answered Nov 01 '22 02:11

SCC


First, of course you need a Elasticsearch instance running.

Then, you should use official client library for Node.js: https://github.com/elastic/elasticsearch-js

To use that module, simply create a client instance

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

And then push some data to it via client.send and search for it using client.search functions.

like image 30
Rafal Wiliński Avatar answered Nov 01 '22 02:11

Rafal Wiliński