资源描述
Elasticsearch简单使用手册
1.Win7系统下安装ES
1.1ES的安装
从官网上下载最新版本的ES,本次使用的是elasticsearch-5.5.0版本
网址是https://www.elastic.co/downloads/elasticsearch
解压缩,执行elasticsearch-5.5.0\bin下面的elasticsearch.bat即可
在浏览器中输入http://localhost:9200,出现如下界面表示运行成功
1.2安装elasticsearch-head插件(非必须)
这个安装比较复杂,首先确保本地计算机安装有git和nodejs,npm
Step1:将elasticsearch-head 从github上面clone下来。具体操作如下,
git clone git://
Step2:修改elasticsearch-head/Gruntfile.js文件,增加hostname属性,设置为本地路径(127.0.0.1)或者*(代表任意)
Step3: 修改elasticsearch-head /_site/app.js文件
修改head的连接地址:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
把localhost修改成你es的服务器地址,如:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://10.10.10.10:9200";
Step4:文件目录elasticsearch-5.5.0 /config/elasticsearch.yml, 修改Elasticsearch的参数, 注意,设置参数的时候:后面要有空格!
# 增加新的参数,这样head插件可以访问es
http.cors.enabled: true
http.cors.allow-origin: "*"
Step5:安装grunt,命令如下
npm install -g grunt-cli
Step6:在elasticsearch-head目录下执行npm install 下载elasticsearch-head运行的依赖包
Step7:运行grunt server启动服务
Step8:访问http://localhost:9100/,出现如下界面表示安装成功
2.Linux系统下集群部署ES
2.1机器配置
三台Linux虚拟机,安装系统CentOS6.5
对应静态IP分别为192.168.244.128, 192.168.244.129,192.168.244.130
开放9200和9300端口,步骤如下:
#/sbin/iptables -I INPUT -p tcp --dport 9200 -j ACCEPT
#/sbin/iptables -I INPUT -p tcp --dport 9300 -j ACCEPT
然后保存:
#/etc/rc.d/init.d/iptables save
查看打开的端口:
# /etc/init.d/iptables status
2.2安装步骤
Step1:从官网上下载ElasticSearch的tar包,本次使用版本5.5.1。分别上传到三台机器中
Step2:解压tar包,修改elasticsearch-5.5.1/config路径下的elasticsearch.yml 配置文件。主要修改如下几个配置项:
#设置集群的名称,三台机器要一样
cluster.name: my-application
#设置各节点名称
node.name: node-128
#数据存储的位置
path.data: /home/hous/develop/elasticsearch-data/data
#日志存储的位置
path.logs: /home/hous/develop/elasticsearch-data/logs
#内存锁定,即不使用linux中的交换内存(swap)
bootstrap.memory_lock: true
#检查是否支持SecComp模式运行,centos6.x不支持
bootstrap.system_call_filter: false
#绑定本机IP地址
network.host: 192.168.244.128
#设置访问端口
http.port: 9200
#以单播方式嗅探集群中的机器
discovery.zen.ping.unicast.hosts: ["192.168.244.128", "192.168.244.129", "192.168.244.130"]
#防止脑裂(节点数/2+1得出)
discovery.zen.minimum_master_nodes: 2
#支持跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
Step3:分别启动elasticsearch,进入elasticsearch-5.5.1/bin路径下,
运行命令:./elasticsearch
或者后台启动命令:./ elasticsearch –d
启动完毕后,可以输入curl 192.168.244.128:9200/_cluster/health?pretty查看集群健康状态,显示为green表示正常启动
2.3常见问题
问题一
[WARN ][o.e.b.JNANatives ] unable to install syscall filter:
Java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMPandCONFIG_SECCOMP_FILTERcompiledinatorg.elasticsearch.bootstrap.Seccomp.linuxImpl(Seccomp.java:349) ~[elasticsearch-5.5.1.jar:5.5.1]
at org.elasticsearch.bootstrap.Seccomp.init(Seccomp.java:630) ~[elasticsearch-5.5.1.jar:5.5.1]
原因:报了一大串错误,大家不必惊慌,其实只是一个警告,主要是因为你Linux版本过低造成的
解决方案:
1、重新安装新版本的Linux系统
2、警告不影响使用,可以忽略
问题二
ERROR: bootstrap checks failed
max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
原因:无法创建本地文件问题,用户最大可创建文件数太小
解决方案:切换到root用户,编辑limits.conf配置文件
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
备注:* 代表Linux所有用户名称(比如 hadoop)
保存、退出、重新登录才可生效
问题三
max number of threads [1024] for user [es] likely too low, increase to at least [2048]
原因:无法创建本地线程问题,用户最大可创建线程数太小
解决方案:切换到root用户,进入limits.d目录下修改90-nproc.conf 配置文件
vi /etc/security/limits.d/90-nproc.conf
找到如下内容:
* soft nproc 1024
#修改为
* soft nproc 2048
问题四
max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
原因:最大虚拟内存太小
解决方案:切换到root用户下,修改配置文件sysctl.conf
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
然后重新启动elasticsearch,即可启动成功
问题五
ElasticSearch启动找不到主机或路由
原因:ElasticSearch 单播配置有问题
解决方案:检查ElasticSearch中的配置文件
vi config/elasticsearch.yml
找到如下配置:
discovery.zen.ping.unicast.hosts:["192.168.**.**:9300","192.168.**.**:9300"]
一般情况下,是这里配置有问题,注意书写格式
问题六
org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream
原因:ElasticSearch节点之间的jdk版本不一致
解决方案:ElasticSearch集群统一jdk环境
问题七
Unsupported major.minor version 52.0
原因:jdk版本问题太低
解决方案:更换jdk版本,ElasticSearch5.0.0支持jdk1.8.0
问题八
can not run elasticsearch as root
原因:不能以root用户启动ES服务器。
解决方案:非要以root用户运行,则配置 -Des.insecure.allow.root=true
对于5.X,在config/jvm.options配置文件中,
添加-Des.insecure.allow.root=true
问题九
memory locking requested for elasticsearch process but memory is not locked
解决方法1:配置 config/elasticsearch.yml ,注释掉以下内容
#bootstrap.memory_lock: true
解决方法2:配置:/etc/security/limits.conf,
* soft memlock unlimited
* hard memlock unlimited
3.Elasticsearch相关概念解释
对比与关系型数据库,ES的各种概念确实不好理解。尤其对“索引”二字更是与关系型数据库混淆的不行,下面通过与MySQL简单对比,加深理解
以上表为依据,在ES中的新建文档(在Index/type下)相当于mysql中(在某Database的Table)下插入一行数据。
4.安装中文分词器
1. 中文分词器的安装
中文分词器ik具体安装步骤如下
拼音分词器的安装步骤如下
2. java创建索引是添加分词器
5.关于字符串的一些说明
在ES中,字符串的类型有两种,分别是text和keyword。他们的区别是,text可以进行analyzed分词操作。keyword不可以not_analyzed。下图是对name字符串的属性说明
1)表示使用ik_smart中文分词
2)表示字符串类型为text,可以进行分词
3)ignore_above是索引的范围,用于not_analyzed字段,默认不会对20字节的数据索引,如果要存储超过32766字节的数据,设置ignore_above=256就可以了
4)表示字符串类型为keyword,不可以进行分词
总体说明:name是text类型,用ik_smart分词倒排索引。name.keyword是keyword类型不可以分词,存储超过32766字节
6.通过CURL操作的简单命令
下面以索引名my_index,索引类型my_type为例进行说明
6.1创建索引
创建一个空索引,索引名为my_index
curl -XPUT 192.168.244.1:9200/my_index -d '
{}
'
6.2添加mapping
添加索引类型,包括字段类型
curl -XPUT 192.168.244.1:9200/my_index/_mapping/my_type -d '
{
"properties": {
"name": {
"analyzer": "ik_smart",
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"id": {
"type": "long"
},
"age": {
"type": "integer"
},
"desc": {
"analyzer": "ik_smart",
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
'
6.3添加文档数据
添加一条文档数据
curl -XPOST 192.168.244.1:9200/my_index/my_type -d '
{
"id": 1,
"name": "张小明",
"desc": "java工程师,从事大数据软件开发",
"age": 23
}
'
批量添加文档数据,这种格式类似一个有效的单行JSON文档流,它通过换行符(\n)连接到一起。注意两个要点:
l 每行一定要以换行符(\n)结尾, 包括最后一行 。这些换行符被用作一个标记,可以有效分隔行。
l 这些行不能包含未转义的换行符,因为他们将会对解析造成干扰。
curl -XPOST 192.168.244.1:9200/_bulk -d '
{ "index": { "_index": "my_index", "_type": "my_type" }}
{"id":5,"name":"张三丰","desc":"java工程师,从事大数据软件开发,对nosql数据库有研究","age":123}
{ "index": { "_index": "my_index", "_type": "my_type" }}
{"id":2,"name":"jack","desc":"php工程师,从事网站开发,对java也有一定了解","age":30}
{ "index": { "_index": "my_index", "_type": "my_type" }}
{"id":3,"name":"李晓红","desc":"前端工程师,对react,angularjs有研究","age":22}
{ "index": { "_index": "my_index", "_type": "my_type" }}
{"id":4,"name":"王晓燕","desc":"UI设计师,目前处于实现阶段","age":19}
{ "index": { "_index": "my_index", "_type": "my_type" }}
{"id":6,"name":"jannes","desc":"数据库工程师,对oracle和mysql有一定的经验","age":35}
'
6.4删除文档数据
删除一条文档数据
curl -XDELETE 192.168.244.1:9200/my_index/my_type/AV3E0qBK9W2J9WuRt8D3
删除多条文档数据
curl -XPOST 192.168.244.1:9200/_bulk -d '
{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D4" }}
{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D5" }}
{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D6" }}
{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3E0qBK9W2J9WuRt8D7" }}
{ "delete": { "_index": "my_index", "_type": "my_type", "_id": "AV3BQz3xJ5KYRov5JoNA" }}
'
6.5查询文档数据
wildcard通配符查询
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query": {
"wildcard": {
"name.keyword": "张*"
}
}
}
'
term 精确查询
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query": {
"term": {
"desc": "工程师"
}
}
}
'
terms查找多个精确值
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query": {
"terms": {
"age": [22,23]
}
}
}
'
range范围查询
range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:
gt: > 大于(greater than)
lt: < 小于(less than)
gte: >= 大于或等于(greater than or equal to)
lte: <= 小于或等于(less than or equal to)
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"age" : {
"gte" : 20,
"lt" : 40
}
}
}
}
}
}
'
exists 查询和not exists(missing查询过时被删除) (null and not null)
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query" : {
"exists" : { "field" : "desc" }
}
}
'
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "desc"
}
}
}
}
}
'
Bool Query组合查询
bool查询由以下元素组成,分别是must,filter,should,must_not
must:所有的语句都必须(must)匹配,与AND等价
must_not:所有的语句都不能(must not)匹配,与NOT等价
should:至少有一个语句要匹配,与OR等价
filter:过滤器查询,忽略score
boost增加相关度评分比重_score, minimum_should_match
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter": {
"term" : { "tag" : "tech" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tag" : "wow" } },
{ "term" : { "tag" : "elasticsearch" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
curl -XGET 192.168.244.128:9200/my_index8/my_type8/_search -d '
{
"query": {
"bool" : {
"must" : {
"term" : { "desc" : "工程师" }
},
"filter": {
"term" : { "name" : "张三丰" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 0, "lte" : 20 }
}
},
"should" : [
{ "term" : { "name" : "晓" } },
{ "term" : { "desc" : "java" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
’
match匹配查询
operator默认or,可以为and,表示满足所有分词条件
curl -XGET 192.168.244.130:9200/my_index8/my_type8/_search -d '
{
"query": {
"match": {
"desc": {
"query": "java工程师数据软件开发",
"operator": "and"
}
}
}
}
'
相邻单词查询,slop相邻的单词个数
curl -XGET 192.168.244.130:9200/my_index8/my_type8/_search -d '
{
"query": {
"match_phrase": {
"desc": {
"query": "jjava工程师",
"slop": 1
}
}
}
}
'
Multi Match Query
可以添加type查询参数:best_fields,most_fields,cross_fields,phrase,phrase_prefix
curl -XGET 192.168.244.130:9200/my_index8/my_type8/_search -d '
{
"query": {
"multi_match" : {
"query": "软件开发",
"fields": [ "name", "desc" ]
}
}
}
'
展开阅读全文