Integration of mongodb with elasticsearch in node.js environment

痴心易碎 提交于 2019-12-08 04:31:47

问题


I am working on a project which has following setup:

  • I have an Amazon EC2 cluster with one master, 3 config servers and 3 shard servers.
  • Master has a node.js application running which is basically a REST API written using Express.js module.
  • I am using mongodb as database. Master has "mongos" service running which shards the data into 3 shard servers. Those servers have "mongod" service running on them.

With this setup, I want to integrate elasticsearch to perform search queries. To do this I want to add a route in my node.js REST API application to perform search query on the data stored in shards.

Are there any additional steps involved given that I have three shards running on independent machines? How do I configure elasticsearch to access the data from the shards to build index? Does it detect this configuration automatically and builds the index? Can someone please provide me the steps that I should follow to accomplish this?


回答1:


I have done it this way:

I am using sails.js framework for node and using mongo as DB.

First of all, i've installed elasticsearch module using npm. Then added this code in a file called elasticSeach.js in config section.

It has the following code:

var elasticsearch = require('elasticsearch'),

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

module.exports.elasticSearchClient = client;

module.exports.elasticSearchConfig = {
  index: index
};

After that, simply create a file ElasticSearchService.js in which you will do all the operations like search, update etc. Here is an example of an elasticsearch index method to index the values, which takes :

a) type

b) item, which is an object of json type like

item = {
 "name" : "vishal",
 "website" : "stackOverflow"
};

and method is

function indexItem(type, item) {
  return Q.promise(function(resolve, reject){
    elasticSearchClient
      .index({
        index: elasticSearchConfig.index,
        type: type,
        body: item
      })
      .then(function (response) {
        sails.log.info("ElasticSearchService#indexItem :: Response :: ", response);
        return resolve(response);
      })
      .catch(function(err) {
        sails.log.error("ElasticSearchService#indexItem :: Error :: ", err);
        return reject(err);
      });
  });
}

Call this method from wherever you want.

I am using a promise to return values. You don't need to worry about shard implementation and all. Elastic takes care of that.

More about type and mappings here : https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html



来源:https://stackoverflow.com/questions/38423759/integration-of-mongodb-with-elasticsearch-in-node-js-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!