好的,所以我在互联网上的各个地方搜索了一些关于如何设置MongoDB和Elasticsearch的文档。似乎主要的elasticsearch github repo中有一个Mongodb河流插件,但没有文档。有没有人让这两种可爱的技术一起工作?
答案 0 :(得分:17)
这是一个相当陈旧的问题,但我会发布我的答案,以防其他人想知道同样的问题,特别是新版本的ES一直在出现。我花了一段时间让我的ES也使用MongoDB。
首先,我假设你安装了ES和MongoDB。如果您不使用副本集,请确保已启用oplog。请参考here了解如何操作。
河流插件有一个依赖项(elasticsearch-mapper-attachments),所以请务必先安装它以防止以后出现任何问题。这个wiki具有安装插件所需的必要命令。如果您使用的是ES 0.20.2及更高版本,请注意河流插件的备用下载链接。
重启ES。
使用以下命令启用索引编制:
curl -XPUT 'http://localhost:9200/_river/mongodb/_meta' -d '
{
"type": "mongodb",
"mongodb": {
"db": "your-database-name",
"collection": "your-collection-name"
},
"index": {
"name": "mongoindex",
"type": "your-type"
}
}'
要进行搜索,请使用curl -XGET 'http://localhost:9200/mongoindex/_search?q=field:value'
我从this website获得了大部分信息,但我觉得它可以更精简,因此也是我自己的方法。
答案 1 :(得分:0)
它适用于某些配置。 elasticsearch河流插件和mongodb的版本对于整个系统的工作至关重要。
以下是重现工作环境的步骤
1)使用elasticsearch版本1.2.4。 ubuntu的包位于:https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.4.deb
使用dpkg -i /path/to/elasticsearch-1.2.4.deb
2)安装河流插件及其依赖项:
cd /usr/share/elasticsearch && bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/1.9.0
cd /usr/share/elasticsearch && bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.1
可以使用较新版本的mapper atachments插件,但mongo river插件的官方维基建议使用1.9.0。
3)MongoDB v2.4.9肯定适用于elasticsearch河流插件。
确保使用--replSet rs0
启动mongod,或者replSet=rs0
中有mongod.conf
接下来,您必须确保在mongodb中启动了副本集:
登录mongo console:
mongo
然后输入:
rs.initiate()
rs.status()
4)准备工作已经完成,现在你需要初始化elasticsearch河流插件。 假设你在localhost上运行了elasticsearch:9200,而mongodb在10.0.2.15:27017上运行
curl -XPUT "localhost:9200/_river/feed/_meta" -d"
{
\"type\": \"mongodb\",
\"mongodb\": {
\"servers\": [
{\"host\": \"10.0.2.15", \"port\": 27017}
],
\"db\": \"YOUR_DB\",
\"collection\": \"YOUR_COLLECTION\"
},
\"index\": {
\"name\": \"YOUR_ELASTIC_INDEX\",
\"type\": \"item\"
}
}"
5)检查mongo服务器上的rs.config()。它应包含可解析的主机名或ips,用于mongodb river插件可以联系的副本集成员。 Elasticsearch河流插件将连接到mongodb.servers中指定的主机并获取副本配置。然后它将尝试通过rs.config()中指定的主机名连接到副本集的主要主机。如果您打算使用docker容器通过河流插件互连monogdb和elasticsearch而不是问题。
您可以通过执行以下操作来更新rs配置:
mongo
然后在mongo shell中
cfg = rs.config()
cfg.members[0].host = "12.34.56.78:27017"
rs.reconfig(cfg)