在node.js环境中将mongodb与elasticsearch集成在一起

我正在做一个有如下设置的项目:

  • 我有一个Amazon EC2集群,有一个主服务器,3个configuration服务器和3个分片服务器。
  • Master有一个node.js应用程序,它基本上是一个使用Express.js模块编写的REST API。
  • 我正在使用MongoDB作为数据库。 Master有“mongos”服务运行,将数据分割成3个分片服务器。 这些服务器上运行着“mongod”服务。

有了这个设置,我想整合elasticsearch来执行search查询。 为了做到这一点,我想在我的node.js REST API应用程序中添加一个path,以对存储在分片中的数据执行search查询。

考虑到我在独立机器上运行三个碎片,是否还有其他步骤? 如何configurationelasticsearch访问分片中的数据来构build索引? 它会自动检测这个configuration并build立索引吗? 有人可以给我提供我应该遵循的步骤来完成这个步骤吗?

我这样做了:

我正在使用sails.js框架节点和使用mongo作为数据库。

首先,我已经使用npm安装了elasticsearch模块。 然后将这段代码添加到configuration部分的一个名为elasticSeach.js的文件中。

它有以下代码:

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 }; 

之后,只需创build一个文件ElasticSearchService.js ,您将在其中执行search,更新等所有操作。下面是一个索引值的elasticsearch索引方法的示例,其中包括:

a) types

b) item ,这是jsontypes的对象

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

和方法是

 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); }); }); } 

从任何你想要的地方调用这个方法。

我正在使用一个承诺来返回值。 你不需要担心碎片实施和所有。 弹性照顾。

更多关于types和映射在这里: https : //www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html