分布式搜索引擎Elasticsearch的简单使用
官方网址:https://www.elastic.co/products/elasticsearch/
一、特性
1、支持中文分词
2、支持多种数据源的全文检索引擎
3、分布式
4、基于lucene的开源搜索引擎
5、Restful api
二、资源
smartcn, 默认的中文分词 :https://github.com/elasticsearch/elasticsearch-analysis-smartcn
mmseg :https://github.com/medcl/elasticsearch-analysis-mmseg
ik:https://github.com/medcl/elasticsearch-analysis-ik
pinyin, 拼音分词可用于输入拼音提示中文 :https://github.com/medcl/elasticsearch-analysis-pinyin
stconvert, 中文简繁体互换 :https://github.com/medcl/elasticsearch-analysis-stconvert
elasticsearch-servicewrapper:https://github.com/elasticsearch/elasticsearch-servicewrapper
Elastic HQ,elasticsearch的监控工具:http://www.elastichq.org
elasticsearch-rtf :https://github.com/medcl/elasticsearch-rtf
三、安装
服务器:Linux(centos 6.x)
java环境:JDK 1.8.0
elasticsearch:2.3.1
elasticsearch-jdbc(数据源插件):2.3.1
IK Analysis(中文分词插件):1.9.1
1、安装Java
yum install java-1.8.0
2、安装Elasticsearch
#创建.repo文件(elasticsearch.repo)
cat >> /etc/yum.repos.d/elasticsearch.repo << EOF
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
EOF #导入key:
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
yum install elasticsearch
3、创建用户
如果是用root账号启动,会报以下错误
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93) at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144) at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285) at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35) Refer to the log for complete error details.
这是出于系统安全考虑设置的条件。由于ElasticSearch可以接收用户输入的脚本并且执行,为了系统安全考虑, 建议创建一个单独的用户用来运行ElasticSearch
groupadd elsearch
useradd elsearch -g elsearch -p elasticsearch
4、创建目录
mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs
chown -R elsearch:elsearch /data/elasticsearch/data
chown -R elsearch:elsearch /data/elasticsearch/logs
5、生成配置文件(/etc/elasticsearch/elasticsearch.yml)
#集群名(同一个集群,名称必须相同)
cluster.name: my-application
#服务节点名(每个服务节点不一样)
node.name: node-1
#数据存储路径
path.data: /data/elasticsearch/data
#服务日志路径
path.logs: /data/elasticsearch/logs
#服务ip地址
network.host: 0.0.0.0
#服务端口
http.port: 9200
四、IK的安装
1.安装maven工具
wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum install apache-maven
2.下载ik源码包
git clone https://github.com/medcl/elasticsearch-analysis-ik
3.生成jar插件包
mvn clean
mvn compile
mvn package unzip target/releases/elasticsearch-analysis-ik-*.zip
cp -r target/releases/ /usr/share/elasticsearch/plugins/ik
4.配置词库(ik自带搜狗词库)
配置:/usr/share/elasticsearch/plugins/ik/config/ik/IKAnalyzer.cfg.xml
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic</entry>
将jar包复制到Elasticsearch的plugins/analysis-ik 目录下,再把解压出的ik目录(配置和词典等),复制到Elasticsearch的config 目录下。然后编辑配置文件elasticsearch.yml ,在后面加一行:
index.analysis.analyzer.ik.type : "ik"
重启service elasticsearch restart
然后录入数据,创建索引
五、elasticsearch-jdbc
1、使用feeder方式
wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.1.0/elasticsearch-jdbc-2.3.1.0-dist.zip
unzip elasticsearch-jdbc-2.3.1.0-dist.zip
编辑数据导入脚本import.sh
export JDBC_IMPORTER_HOME=/elasticsearch-jdbc-2.3.1.0 bin=$JDBC_IMPORTER_HOME/bin
lib=$JDBC_IMPORTER_HOME/lib
echo '{
"type" : "jdbc",
"jdbc": {
"url":"jdbc:mysql://127.0.0.1:3306/dbtest",
"user":"root",
"password":"123456",
"sql":"select * from test_tb",
"index" : "customer",
"type" : "external"
}}' | java \
-cp "${lib}/*" \
-Dlog4j.configurationFile=${bin}/log4j2.xml \
org.xbib.tools.Runner \
org.xbib.tools.JDBCImporter
测试
curl -XGET 'localhost:9200/customer/external/_search?pretty&q=a'
删除索引
curl -XDELETE 'http://localhost:9200/customer'
2、使用river方式
#安装elasticsearch
curl -OL https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.zip cd $ES_HOME
unzip path/to/elasticsearch-1.4.2.zip #安装JDBC插件
./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-river-jdbc/1.4.0.6/elasticsearch-river-jdbc-1.4.0.6-plugin.zip #下载mysql driver
curl -o mysql-connector-java-5.1.33.zip -L 'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.33.zip/from/http://cdn.mysql.com/'
cp mysql-connector-java-5.1.33-bin.jar $ES_HOME/plugins/jdbc/ chmod 644 $ES_HOME/plugins/jdbc/* #启动elasticsearch
./bin/elasticsearch #停止river
curl -XDELETE 'localhost:9200/_river/my_jdbc_river/'
JDBC插件参数
curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "",
"password" : "",
"sql" : "select * from orders",
"index" : "myindex",
"type" : "mytype",
...
}
}'
如果一个数组传递给jdbc字段,多个river源也是可以的
curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
<river parameters>
"type" : "jdbc",
"jdbc" : [ {
<river definition 1>
}, {
<river definition 2>
} ]
}'
curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"driver" : "com.mysql.jdbc.Driver",
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "root",
"password" : "123456",
"sql" : "select * from test.student;",
"interval" : "30",
"index" : "test",
"type" : "student"
}
}’
查看ES是否已经同步了这些数据
curl -XGET 'localhost:9200/test/student/_search?pretty&q=*'
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
参考
https://www.elastic.co/guide/en/elasticsearch/guide/current/empty-search.html
https://github.com/medcl/elasticsearch-analysis-ik
http://blog.csdn.net/clementad/article/details/46898013
https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/elasticsearch-river-jdbc.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
https://github.com/jprante/elasticsearch-jdbc
http://www.voidcn.com/blog/wojiushiwo987/article/p-6058574.html
http://leotse90.com/2015/11/11/ElasticSearch%E4%B8%8EMySQL%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5%E4%BB%A5%E5%8F%8A%E4%BF%AE%E6%94%B9%E8%A1%A8%E7%BB%93%E6%9E%84/
http://www.jianshu.com/p/638ff7b848cc
http://www.cnblogs.com/buzzlight/p/logstash_elasticsearch_kibana_log.html
分布式搜索引擎Elasticsearch的简单使用的更多相关文章
- 分布式搜索引擎Elasticsearch在CentOS7中的安装
1. 概述 随着企业业务量的不断增大,业务数据随之增加,传统的基于关系型数据库的搜索已经不能满足需要. 在关系型数据库中搜索,只能支持简单的关键字搜索,做不到分词和统计的功能,而且当单表数据量到达上百 ...
- 分布式搜索引擎ElasticSearch+Kibana (Marvel插件安装详解)
在安装插件的过程中,尤其是安装Marvel插件遇到了很多问题,要下载license.Marvel-agent,又要下载安装Kibana 版本需求 Java 7 or later Elasticsear ...
- 快速掌握分布式搜索引擎ElasticSearch(一)
前言 由于最近在项目中接触使用到了ElasticSearch,从本篇博客开始将给大家分享这款风靡全球的产品.将涉及到ElasticSearch的安装.基础概念.基本用法.高级查询.中文分词器.与Spr ...
- ElasticSearch logo 分布式搜索引擎 ElasticSearch
原文来自:http://www.oschina.net/p/elasticsearch Elastic Search 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中 ...
- 分布式搜索引擎Elasticsearch性能优化与配置
1.内存优化 在bin/elasticsearch.in.sh中进行配置 修改配置项为尽量大的内存: ES_MIN_MEM=8g ES_MAX_MEM=8g 两者最好改成一样的,否则容易引发长时间GC ...
- 分布式搜索引擎Elasticsearch的查询与过滤
一.写入 先来一个简单的官方例子,插入的参数为-XPUT,插入一条记录. curl -XPUT 'http://localhost:9200/test/users/1' -d '{ "use ...
- 002_分布式搜索引擎Elasticsearch的查询与过滤
一.写入 先来一个简单的官方例子,插入的参数为-XPUT,插入一条记录. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 curl -XPUT 'http:/ ...
- 分布式搜索引擎Elasticsearch的架构分析
一.写在前面 ES(Elasticsearch下文统一称为ES)越来越多的企业在业务场景是使用ES存储自己的非结构化数据,例如电商业务实现商品站内搜索,数据指标分析,日志分析等,ES作为传统关系型数据 ...
- 分布式搜索引擎Elasticsearch PHP类封装 使用原生api
//官方的 php api写的鸡肋了,下面这个类可以使用 es api 操作. <?php class ElasticSearch { public $index; function __co ...
随机推荐
- 学习SpringMVC——如何获取请求参数
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...
- 浅谈2D游戏设计模式2- WZ文件详解(UI.WZ)之MapLogin.img(1)
玩过冒险岛的人都知道有一个WZ文件,那么这个WZ文件的内部是怎么样的呢,今天我就带大家一探究竟. 说实在的,我这是第一次接触WZ文件,但是却让我很震撼,为什么很震撼,因为这个居然是用VS2010写的! ...
- [Java 安全]加密算法
Base64编码 算法简述 定义 Base64内容传送编码是一种以任意8位字节序列组合的描述形式,这种形式不易被人直接识别. Base64是一种很常见的编码规范,其作用是将二进制序列转换为人类可读的A ...
- JS设置CSS样式的几种方式
用JS来动态设置CSS样式,常见的有以下几种 1. 直接设置style的属性 某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign) 如果想保 ...
- CSS垂直居中和水平居中
前言 CSS居中一直是一个比较敏感的话题,为了以后开发的方便,楼主觉得确实需要总结一下了,总的来说,居中问题分为垂直居中和水平居中,实际上水平居中是很简单的,但垂直居中的方式和方法就千奇百怪了. 内联 ...
- 【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中
项目输出目录(bin/debug|release)中经常是这个样子: main.exemain.pdb a.dll a.xml b.dll b.pdb b.xml ... 其中xml是同名dll的注释 ...
- C#~异步编程再续~async异步方法与同步方法的并行
返回目录 今天晚上没事写了个测试的代码,又看了看.net的并行编程,两个方法,一个是异步async修饰的,另一个是普通的方法,在控制台程序的Main方法里去调用这两个方法,会有什么结果呢? 首先我们看 ...
- 异构SOA系统架构之Asp.net实现(兼容dubbo)
我们公司技术部门情况比较复杂,分到多个集团,每个集团又可能分为几个部门,每个部门又可能分为多个小组,组织架构比较复杂,开发人员比较多. 使用的编程语言也有点复杂,主流语言有.net(C#).Java. ...
- java必备基础知识点
Java基础 1. 简述Java的基本历史 java起源于SUN公司的一个GREEN的项目,其原先目的是:为家用消费电子产品发送一个信息的分布式代码系统,通过发送信息控制电视机.冰箱等 2. 简单写出 ...
- LZW压缩算法——简明原理与实现
LZW和哈夫曼编码一样,是无损压缩中的一种.该算法通过建立字典,实现字符重用与编码,适用于source中重复率很高的文本压缩.本文首先讲下LZW的编解码原理,然后给出LZW的实现code. ***** ...