搭建ElasticSearch+MongoDB检索系统
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
以上是百度百科对ES的介绍。本文主要记述在linux环境下安装Elasticsearch及基本的可视化工具sense,并使用ES索引MongoDB中的数据,使我们可以直接通过ES去检索数据库中的数据,使检索速度大大加快,并使用python去完成对ES的检索操作。
环境:ubuntu 14.04, elasticsearch 2.3.4, mongodb 3.2, python 2.7
1.安装ElasticSearch
首先官方文档要求Elasticsearch 至少安装有Java 7. 因此首先确定已经安装有JDK7或以上版本。
安装ElasticSearch非常简单,只需要去官方下载地址https://www.elastic.co/downloads/elasticsearch下载想要安装的版本即可,例如下载2.3.4版本,下载完毕后解压,接着进入bin目录下
cd elasticsearch-2.3.4/bin
运行
./elasticsearch
接着打开浏览器,输入localhost:9200,如果出现以下信息则启动成功
{ "name" : "Tethlam", "cluster_name" : "elasticsearch", "version" : { "number" : "2.3.4", "build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f", "build_timestamp" : "2016-06-30T11:24:31Z", "build_snapshot" : false, "lucene_version" : "5.5.0" }, "tagline" : "You Know, for Search" }
2.安装kibana和sense
kibana是ES的可视化工具,功能强大,sense是kibana的一个插件,使用sense可以直接在浏览器里完成对ES的基本操作。
首先安装kibana,安装方法与ES一样,首先下载想要的版本https://www.elastic.co/downloads/kibana,然后解压,进入bin目录,运行以下命令安装sense
./bin/kibana plugin --install elastic/sense
安装成功后运行 ./bin/kibana
打开浏览器,输入http://localhost:5601/app/sense即可进入sense
至此一个基本的ES环境就安装完成了
3.安装
mongo-connector
接下来我们要使用ES去检索mongodb中的数据,这样首先需要用ES去同步存储在mongodb中数据,创建这些数据的索引。完成这一步工作需要使用的工具是mongo-connector,这是mongodb官方开发的一个非正式工具。
使用mongo-connector同步数据首先需要确保mongodb为副本集,这一步是必不可少的,因为ES是通过mongodb的oplog去建立索引,如果不是副本集就没有oplog,当然也就无法建立索引了,至于如何建立mongodb副本集请自行查阅mongodb文档。
可以使用pip直接安装mongo-connector:
pip install mongo-connector
然后下载 https://github.com/mongodb-labs/elastic2-doc-manager,接着可以直接运行以下命令完成数据同步
mongo-connector -m localhost:27017 -t localhost:9200 -d elastic2_doc_manager
注意如果使用的ES为1.x版本需要下载 https://github.com/mongodb-labs/elastic-doc-manager并运行
mongo-connector -m localhost:27017 -t localhost:9200 -d elastic_doc_manager
其中-m之后的参数为数据库地址,-t之后为ES地址,这样就完成了同步
4.用python操作ES
最后我们要使用python操作ES,对此ES本身提供了python client,同样使用pip即可安装
pip install elasticsearch
接着可以使用以下代码测试
from elasticsearch import Elasticsearch es = Elasticsearch() # ignore 400 cause by IndexAlreadyExistsException when creating an index es.indices.create(index='test-index', ignore=400) # ignore 404 and 400 es.indices.delete(index='test-index', ignore=[400, 404])
更多信息请参考官方文档
相关链接:ES官方文档https://www.elastic.co/guide/index.html
mongo-connector文档https://github.com/mongodb-labs/mongo-connector/wiki/Usage-with-ElasticSearch
Elasticsearch python client文档https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch
- 搭建局域网内部SUS服务
- 搭建Qt/Embedded linux开发环境
- fedora15上搭建cvs-server和telnet-server(二)
- MAC下Android的Eclipse开发环境的搭建
- Emacs+Lisp环境搭建
- RHEL6.4 和centos5.8搭建本地yum源
- Hadoop环境搭建、配置及通过执行计算来验证的案例
- maven 3.2.1之环境搭建与简单实例
- Eclipse IDE for Java EE 搭建 Android开发环境
- 在自己的电脑上搭建服务器,发布自己的网站(学习之用)
- robotium之测试APK环境搭建
- 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?(2)
- 搭建SpringMVC项目
- 搭建SSH三大框架WEB项目过程(Struts2.3+Hibernate4.3+Spring4.1)
- 安装Maven并搭建Maven私有仓库
- Solr Cloud 5.4 集群环境搭建
- [置顶] hive 单节点环境搭建指南 1.2.1版本
- ROS实践(1)-环境搭建
- VS2008下搭建ACE+TAO+CIAO环境
- 汇编实验1:环境搭建与Debug使用