栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在MongoDB中使用Elasticsearch?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在MongoDB中使用Elasticsearch?

这个答案应该足以使您开始按照本教程使用MongoDB,Elasticsearch和AngularJS构建功能搜索组件进行学习。

如果您想对来自API的数据使用多面搜索,那么您可能想看看Matthiasn的BirdWatch
Repo

因此,这是设置单个节点Elasticsearch“集群”以索引MongoDB的方法,以便在新EC2 Ubuntu 14.04实例上的NodeJS
Express应用中使用。

确保所有内容都是最新的。

sudo apt-get update

安装NodeJS。

sudo apt-get install nodejssudo 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_NAMEdb.createCollection(YOUR_COLLECTION_NAME)for (var i = 1; i <= 25; i++) db.YOUR_COLLECTION_NAME.insert( { x : i } )

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

首先关闭该过程。

mongo YOUR_DATAbase_NAMEuse admindb.shutdownServer()

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

vi /etc/mongod.conf

添加这些行,为您的数据库和日志路径提供辅助。

replSet=rs0dbpath=YOUR_PATH_TO_DATA/DBlogpath=YOUR_PATH_TO_LOG/MONGO.LOG

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

mongo DATAbase_NAMEconfig = { "_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.debsudo dpkg -i elasticsearch-1.1.1.debcurl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xzsudo mv *servicewrapper*/service /usr/local/share/elasticsearch/bin/sudo rm -Rf *servicewrapper*sudo /usr/local/share/elasticsearch/bin/service/elasticsearch installsudo 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.0bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/1.6.0

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

bin/plugin --install mobz/elasticsearch-headbin/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'

去玩。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号