mongodb - 如何在 MongoDB 中使用 Elasticsearch?

标签 mongodb elasticsearch

我浏览了许多关于配置 Elasticsearch for MongoDB 以索引 MongoDB 中的集合的博客和网站,但没有一个是直截了当的。

请给我解释一下安装elasticsearch的分步过程,应该包括:

  • 配置
  • 在浏览器中运行

我将 Node.js 与 express.js 一起使用,因此请提供相应的帮助。

最佳答案

这个答案应该足以让您在 Building a functional search component with MongoDB, Elasticsearch, and AngularJS 上学习本教程。 .

如果您希望对来自 API 的数据使用分面搜索,那么 Matthiasn 的 BirdWatch Repo是你可能想看的东西。

以下是您如何设置单节点 Elasticsearch“集群”来索引 MongoDB,以便在全新 EC2 Ubuntu 14.04 实例上的 NodeJS、Express 应用程序中使用。

确保一切都是最新的。

sudo apt-get update

安装 NodeJS。

sudo apt-get install nodejs
sudo apt-get install npm

Install MongoDB - 这些步骤直接来自 MongoDB 文档。 选择您喜欢的任何版本。我坚持使用 v2.4.9,因为它似乎是最新版本 MongoDB-River支持没有问题。

导入 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,直到他们将其作为服务运行时解决了一些错误。

apt-get install mongodb-10gen=2.4.9

防止您的 MongoDB 安装版本在您更新时被提升。

echo "mongodb-10gen hold" | sudo dpkg --set-selections

启动 MongoDB 服务。

sudo service mongodb start

您的数据库文件默认为/var/lib/mongo,您的日志文件默认为/var/log/mongo。

通过 mongo shell 创建一个数据库并将一些虚拟数据插入其中。

mongo YOUR_DATABASE_NAME
db.createCollection(YOUR_COLLECTION_NAME)
for (var i = 1; i <= 25; i++) db.YOUR_COLLECTION_NAME.insert( { x : i } )

现在到 Convert the standalone MongoDB into a Replica Set .

首先关闭进程。

mongo YOUR_DATABASE_NAME
use admin
db.shutdownServer()

现在我们将 MongoDB 作为服务运行,因此当我们重新启动 mongod 进程时,我们不会在命令行参数中传入“--replSet rs0”选项。相反,我们将其放在 mongod.conf 文件中。

vi /etc/mongod.conf

添加这些行,替换您的数据库和日志路径。

replSet=rs0
dbpath=YOUR_PATH_TO_DATA/DB
logpath=YOUR_PATH_TO_LOG/MONGO.LOG

现在再次打开 mongo shell 以初始化副本集。

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 .

确保已安装 Java。

sudo apt-get install openjdk-7-jre-headless -y

暂时坚持使用 v1.1.x,直到 Mongo-River 插件错误在 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 启用了以下配置选项:

cluster.name: "MY_CLUSTER_NAME"
node.local: true

启动 Elasticsearch 服务。

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 一起使用。

bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/1.6.0
bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/1.6.0

这两个插件不是必需的,但它们非常适合测试查询和可视化索引更改。

bin/plugin --install mobz/elasticsearch-head
bin/plugin --install lukas-vlcek/bigdesk

重启 Elasticsearch。

sudo service elasticsearch restart

最后从 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 中

curl -XGET http://localhost:9200/_aliases

检查您的集群运行状况。

curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'

它可能是黄色的,带有一些未分配的碎片。我们必须告诉 Elasticsearch 我们想要使用什么。

curl -XPUT 'localhost:9200/_settings' -d '{ "index" : { "number_of_replicas" : 0 } }'

再次检查集群运行状况。它现在应该是绿色的。

curl -XGET 'http://localhost:9200/_cluster/health?pretty=true'

去玩吧。

关于mongodb - 如何在 MongoDB 中使用 Elasticsearch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846971/

相关文章:

Elasticsearch /kibana 4 : field exists but is not equal to a value

python - 如何使用 pymongo 获取仅包含 ObjectId 的列表?

mongodb - 使用 mongoDB 切换 boolean 值

Elasticsearch Java API Function Score Query with geo and time 高斯函数

.net - 是否可以在Elasticsearch路径中使用lambda表达式?

elasticsearch - 登录后的AWS kibana抛出缺少角色错误

python - 如何(正确地)在 Kubernetes 上部署 MongoDB 并从另一个 Pod/Job 访问它?

mongodb - 对大型 mongo 集合的部分文档更新 - 如何不锁定数据库?

node.js - 使用 Mongoose 获取/查找子集合

elasticsearch - 重新启动任务后,融合的Kafka Connect Elasticsearch接收器吞吐量永久下降