如何在 MongoDB 中使用 Elasticsearch? [英] How to use Elasticsearch with MongoDB?

查看:29
本文介绍了如何在 MongoDB 中使用 Elasticsearch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了许多关于为 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.

请向我解释安装elasticsearch的分步过程,其中应包括:

Please explain to me a step by step process for installing elasticsearch, which should include:

  • 配置
  • 在浏览器中运行

我正在使用 Node.js 和 express.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,以便在新的 EC2 Ubuntu 14.04 实例上的 NodeJS、Express 应用程序中使用.

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.

确保一切都是最新的.

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 包.

Get the 10gen package.

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

现在将独立的MongoDB转换为副本集.

首先关闭进程.

mongo YOUR_DATABASE_NAME
use admin
db.shutdownServer()

现在我们将 MongoDB 作为服务运行,因此当我们重新启动 mongod 进程时,我们不会在命令行参数中传入--replSet rs0"选项.相反,我们将其放在 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.

Make sure Java is installed.

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 启用了以下配置选项:

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

如果你看到这样的东西,那么你很好.

If you see something like this then you're good.

{
  "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

这两个插件不是必需的,但它们适用于测试查询和可视化索引更改.

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

重启 Elasticsearch.

Restart Elasticsearch.

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"
  }
}'

检查您的索引是否在 Elasticsearch 中

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 中使用 Elasticsearch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆