小编典典

如何在MongoDB中使用Elasticsearch?

elasticsearch

我遍历了许多博客和网站,了解如何配置Elasticsearch for MongoDB来索引MongoDB中的集合,但是它们都不是直接的。

请向我说明逐步安装Elasticsearch的过程,其中应包括:

  • 组态
  • 在浏览器中运行

我将Node.js与express.js结合使用,因此请相应地提供帮助。


阅读 889

收藏
2020-06-22

共1个答案

小编典典

这个答案应该足以使您开始按照本教程使用MongoDB,Elasticsearch和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

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

导入MongoDB公共GPG密钥。

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

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

首先关闭该过程。

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,直到在v1.2.1中修复了Mongo-River插件错误为止。

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'

去玩。

2020-06-22