如何使用弹性搜索与MongoDB? [英] How to use Elasticsearch with MongoDB?
问题描述
我已经经历了许多关于MongoDB配置Elasticsearch的博客和网站来索引MongoDB中的收藏,但都没有直接的。
I have gone through many blogs and sites about configuring Elasticsearch for MongoDB to index Collections in MongoDB but none of them were straightforward.
请向我解释一下安装弹性搜索的步骤过程包括:
Please explain to me a step by step process for installing elasticsearch, which should include:
- 配置
- 在浏览器中运行
-
- configuration
- run in the browser
我正在使用具有express.js的Node.js,所以请相应地帮助。
I am using Node.js with express.js, so please help accordingly.
推荐答案
这个答案应该足够让你成为这个关于使用MongoDB,Elasticsearch和AngularJS构建一个功能搜索组件。
This answer should be enough to get you set up to follow this tutorial on Building a functional search component with MongoDB, Elasticsearch, and AngularJS.
如果您希望使用分面搜索与数据一个API,然后Matthiasn的 BirdWatch Repo 是您可能想要看的。
If you're looking to use faceted search with data from an API then Matthiasn's BirdWatch Repo is something you might want to look at.
所以这里是你如何设置一个单一的节点Elasticsearch集群用于索引MongoDB,用于NodeJS,Express应用程序在新的EC2 Ubuntu 14.04实例上。
So here's how you can setup a single node Elasticsearch "cluster" to index MongoDB for use in a NodeJS, Express app on a fresh EC2 Ubuntu 14.04 instance.
确保一切都是最新的。 / p>
Make sure everything is up to date.
sudo apt-get update
安装NodeJS。
sudo apt-get install nodejs
sudo apt-get install npm
安装MongoDB - 这些步骤是直接从MongoDB文档。
选择你喜欢的任何版本。我坚持使用v2.4.9,因为它似乎是最新的版本 MongoDB-River 支持没有问题。
Install MongoDB - These steps are straight from MongoDB docs. Choose whatever version you're comfortable with. I'm sticking with v2.4.9 because it seems to be the most recent version MongoDB-River supports without issues.
导入MongoDB公共GPG密钥。
Import the MongoDB public GPG Key.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
更新您的来源列表。
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
获取10gen包。
sudo apt-get install mongodb-10gen
然后选择你的版本,如果你不想要最近的。如果您正在将Windows 7或8机器的环境设置为远离v2.6,直到将其作为服务运行时出现错误。
Then pick your version if you don't want the most recent. If you are setting your environment up on a windows 7 or 8 machine stay away from v2.6 until they work some bugs out with running it as a service.
apt-get install mongodb-10gen=2.4.9
防止版本当您更新时,您的MongoDB安装程序会被冲突。
Prevent the version of your MongoDB installation being bumped up when you update.
echo "mongodb-10gen hold" | sudo dpkg --set-selections
启动MongoDB服务。
Start the MongoDB service.
sudo service mongodb start
您的数据库文件默认为/ var / lib / mongo,您的日志文件为/ var / log / mongo。
Your database files default to /var/lib/mongo and your log files to /var/log/mongo.
通过mongo shell创建一个数据库,并将一些虚拟数据推入
Create a database through the mongo shell and push some dummy data into it.
mongo YOUR_DATABASE_NAME
db.createCollection(YOUR_COLLECTION_NAME)
for (var i = 1; i <= 25; i++) db.YOUR_COLLECTION_NAME.insert( { x : i } )
a href =http://docs.mongodb.org/v2.2/tutorial/convert-standalone-to-replica-set/>将独立的MongoDB转换为副本集。
Now to Convert the standalone MongoDB into a Replica Set.
首先关闭该过程。
mongo YOUR_DATABASE_NAME
use admin
db.shutdownServer()
现在我们正在运行MongoDB作为一项服务,所以我们不在命令行arg中传递--replSet rs0选项当我们重新启动mongod进程时。相反,我们把它放在mongod.conf文件中。
Now we're running MongoDB as a service, so we don't pass in the "--replSet rs0" option in the command line argument when we restart the mongod process. Instead, we put it in the mongod.conf file.
vi /etc/mongod.conf
添加这些行,为您的数据库和日志路径提供支持。
Add these lines, subbing for your db and log paths.
replSet=rs0
dbpath=YOUR_PATH_TO_DATA/DB
logpath=YOUR_PATH_TO_LOG/MONGO.LOG
现在再次打开mongo shell来初始化副本集。
Now open up the mongo shell again to initialize the replica set.
mongo DATABASE_NAME
config = { "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "127.0.0.1:27017" } ] }
rs.initiate(config)
rs.slaveOk() // allows read operations to run on secondary members.
现在安装Elasticsearch。我只是关注这个有用的 Gist 。
Now install Elasticsearch. I'm just following this helpful Gist.
确保安装了Java。
sudo apt-get install openjdk-7-jre-headless -y
现在坚持使用v1.1.x,直到Mongo-River插件错误在v1.2.1中得到修复。
Stick with v1.1.x for now until the Mongo-River plugin bug gets fixed in v1.2.1.
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.deb
sudo dpkg -i elasticsearch-1.1.1.deb
curl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xz
sudo mv *servicewrapper*/service /usr/local/share/elasticsearch/bin/
sudo rm -Rf *servicewrapper*
sudo /usr/local/share/elasticsearch/bin/service/elasticsearch install
sudo ln -s `readlink -f /usr/local/share/elasticsearch/bin/service/elasticsearch` /usr/local/bin/rcelasticsearch
确保/etc/elasticsearch/elasticsearch.yml启用了以下配置选项,如果您现在只在单个节点上开发: / p>
Make sure /etc/elasticsearch/elasticsearch.yml has the following config options enabled if you're only developing on a single node for now:
cluster.name: "MY_CLUSTER_NAME"
node.local: true
启动Elasticsearch服务。
Start the Elasticsearch service.
sudo service elasticsearch start
验证它是否正常。
curl http://localhost:9200
如果你看到像
{
"status" : 200,
"name" : "Chi Demon",
"version" : {
"number" : "1.1.2",
"build_hash" : "e511f7b28b77c4d99175905fac65bffbf4c80cf7",
"build_timestamp" : "2014-05-22T12:27:39Z",
"build_snapshot" : false,
"lucene_version" : "4.7"
},
"tagline" : "You Know, for Search"
}
现在安装Elasticsearch插件,以便可以使用MongoDB。
Now install the Elasticsearch plugins so it can play with MongoDB.
bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/1.6.0
bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/1.6.0
这两个插件不是必需的,但它们对于测试查询和可视化索引的更改很有用。 / p>
These two plugins aren't necessary but they're good for testing queries and visualizing changes to your indexes.
bin/plugin --install mobz/elasticsearch-head
bin/plugin --install lukas-vlcek/bigdesk
重新启动弹性搜索。
sudo service elasticsearch restart
最后从MongoDB索引一个集合。
Finally index a collection from MongoDB.
curl -XPUT localhost:9200/_river/DATABASE_NAME/_meta -d '{
"type": "mongodb",
"mongodb": {
"servers": [
{ "host": "127.0.0.1", "port": 27017 }
],
"db": "DATABASE_NAME",
"collection": "ACTUAL_COLLECTION_NAME",
"options": { "secondary_read_preference": true },
"gridfs": false
},
"index": {
"name": "ARBITRARY INDEX NAME",
"type": "ARBITRARY TYPE NAME"
}
}'
检查您的索引是否在弹性搜索
Check that your index is in Elasticsearch
curl -XGET http://localhost:9200/_aliases
检查您的群集运行状况。
Check your cluster health.
curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
这可能是黄色的,有些未分配的碎片。我们必须告诉Elasticsearch我们想要使用什么。
It's probably yellow with some unassigned shards. We have to tell Elasticsearch what we want to work with.
curl -XPUT 'localhost:9200/_settings' -d '{ "index" : { "number_of_replicas" : 0 } }'
再次检查群集运行状况。现在应该是绿色。
Check cluster health again. It should be green now.
curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'
去玩。
这篇关于如何使用弹性搜索与MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!