mongodb 与 elasticsearch 在 node.js 环境中的集成
Integration of mongodb with elasticsearch in node.js environment
我正在从事一个具有以下设置的项目:
- 我有一个 Amazon EC2 集群,其中包含一个主服务器、3 个配置服务器和 3 个分片服务器。
- Master 有一个 node.js 应用程序 运行,它基本上是一个使用 Express.js 模块编写的 REST API。
- 我正在使用 mongodb 作为数据库。 Master 有 "mongos" 服务 运行 将数据分片到 3 个分片服务器。这些服务器上有 "mongod" 服务 运行。
有了这个设置,我想集成 elasticsearch 来执行搜索查询。为此,我想在我的 node.js REST API 应用程序中添加一个路由,以对存储在分片中的数据执行搜索查询。
考虑到我在独立机器上有三个分片 运行,是否涉及任何额外的步骤?如何配置 elasticsearch 以访问分片中的数据来构建索引?它会自动检测此配置并构建索引吗?有人可以向我提供完成此操作应遵循的步骤吗?
我是这样做的:
我正在为节点使用 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" : "Whosebug"
};
方法是
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);
});
});
}
从任何地方调用此方法。
我正在使用对 return 值的承诺。
您无需担心分片实施和所有问题。 Elastic 负责这件事。
更多关于类型和映射的信息:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html
我正在从事一个具有以下设置的项目:
- 我有一个 Amazon EC2 集群,其中包含一个主服务器、3 个配置服务器和 3 个分片服务器。
- Master 有一个 node.js 应用程序 运行,它基本上是一个使用 Express.js 模块编写的 REST API。
- 我正在使用 mongodb 作为数据库。 Master 有 "mongos" 服务 运行 将数据分片到 3 个分片服务器。这些服务器上有 "mongod" 服务 运行。
有了这个设置,我想集成 elasticsearch 来执行搜索查询。为此,我想在我的 node.js REST API 应用程序中添加一个路由,以对存储在分片中的数据执行搜索查询。
考虑到我在独立机器上有三个分片 运行,是否涉及任何额外的步骤?如何配置 elasticsearch 以访问分片中的数据来构建索引?它会自动检测此配置并构建索引吗?有人可以向我提供完成此操作应遵循的步骤吗?
我是这样做的:
我正在为节点使用 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" : "Whosebug"
};
方法是
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);
});
});
}
从任何地方调用此方法。
我正在使用对 return 值的承诺。 您无需担心分片实施和所有问题。 Elastic 负责这件事。
更多关于类型和映射的信息:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html