【问题标题】:Integration of mongodb with elasticsearch in node.js environmentnode.js环境下mongodb与elasticsearch的集成
【发布时间】:2016-07-17 17:04:27
【问题描述】:

我正在从事一个具有以下设置的项目:

  • 我有一个 Amazon EC2 集群,其中包含一个主服务器、3 个配置服务器和 3 个分片服务器。
  • Master 运行着一个 node.js 应用程序,它基本上是一个使用 Express.js 模块编写的 REST API。
  • 我使用 mongodb 作为数据库。 Master 运行“mongos”服务,将数据分片到 3 个分片服务器。这些服务器上运行着“mongod”服务。

通过此设置,我想集成 elasticsearch 来执行搜索查询。为此,我想在我的 node.js REST API 应用程序中添加一个路由,以对存储在分片中的数据执行搜索查询。

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

【问题讨论】:

  • 阅读 ES 文档,你所有的问题都有答案
  • @VsevolodGoloviznin 感谢您的回复。你能指点我一些资源吗?
  • 谷歌搜索 ES 文档

标签: node.js mongodb elasticsearch amazon-ec2 sharding


【解决方案1】:

我是这样做的:

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

首先,我使用 npm 安装了 elasticsearch 模块。 然后将此代码添加到配置部分中名为 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
};

之后,只需创建一个文件 ElasticSearchService.js,您将在其中执行所有操作,如搜索、更新等。这是一个用于索引值的弹性搜索索引方法的示例,它采用:

a) 类型

b) item,是像

这样的json类型的对象
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);
      });
  });
}

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

我正在使用 promise 来返回值。 你不需要担心分片的实现等等。 Elastic 可以解决这个问题。

更多关于类型和映射的信息在这里:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2018-03-02
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多