一、Mac环境安装ES
1、下载ES 6.3.1版本
安装wge
brew install wget
下载ES
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.1.zip
可以直接在github上直接下载压缩包
解压到指定文件夹
unzip elasticsearch-6.3.1.zip
2、启动
进入/bin目录
./elasticsearch
访问:http://localhost:9200/ 请求9200端口(默认9200)
出现json串说明安装成功,包含当前节点、集群、版本等信息
{
"name" : "lC3EDPC",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Oyv1h5qcSE6Ga6j_L6xKLQ",
"version" : {
"number" : "6.3.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "eb782d0",
"build_date" : "2018-06-29T21:59:26.107521Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
但是通过ip访问http://192.168.**.**:9200/访问失败
进入elasticsearch-6.3.1/config目录下,编辑elasticsearch.yml
放开注释:network.host: ,修改ip为本机ip即可
再次访问http://192.168.**.**:9200/访问成功
二、Centos 7安装ES 6.3.1版本
相同步骤!
启动时候会报错:can not run elasticsearch as root
新建用户:
adduser es
设置密码:
passwd es
切换到用户es启动es
su es
错误信息:Exception in thread "main" java.nio.file.AccessDeniedException: /opt/ES-node/elasticsearch-6.3.1/config/jvm.options
错误原因:使用非 root用户启动ES,而该用户的文件权限不足而被拒绝执行。
设置权限:chown -R 用户名:用户名 文件夹名
chown -R es:es /opt/ES-node
可能会出现的问题:
1、ERROR: bootstrap checks failed max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536] max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]
切换到root用户,编辑limits.conf
vim /etc/security/limits.conf
文末添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2、max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
切换到root用户修改配置sysctl.conf
vim /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
修改elasticsearch.yml 中 network.host: ,修改ip为本机ip
启动正常,访问ip正常
三、集群搭建
两台服务器:192.168.174.148 (A) 192.168.7.95 (B)
1、配置elasticsearch-6.3.1/config/elasticsearch.yml
A\B中配置相同的集群名,不同的节点名,端口号保持一致
# 集群名
cluster.name: my-cluster
#节点名
node.name: node-1
# Set a custom port for HTTP:
# 设置监听HTTP传输的自定义端(默认为9200)
http.port: 9200
# 为节点之间的通信设置一个自定义端口(默认为9300)
transport.tcp.port: 9300
在A中配置B的ip为集群发现ip列表
如A中
#集群发现ip列表
discovery.zen.ping.unicast.hosts: ["192.168.7.95:9300"]
2、启动两个es服务
可能报错:with the same id but is a different node instance
解决:删除elsticsearch文件夹下的data文件夹下的节点数据
启动成功!
3、测试集群
使用postman请求在A节点建立索引weather:http://192.168.174.148:9200/weather put方式
分别查询A、B节点的所有索引:http://192.168.7.95:9200/_cat/indices?v get方式
返回索引信息
发现在A节点创建的索引,在B节点也可以查询出来,集群搭建成功!
四、插件安装与使用
1、ik分词插件安装
安装对应版本的ik分词插件:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.1/elasticsearch-analysis-ik-6.3.1.zip
2、使用
1)重启es服务,创建索引,指定分词插件为ik
http://192.168.174.148:9200/accounts put方式
body:
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}
2)新增数据:http://192.168.174.148:9200/accounts/person/1 put方式
body:
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}
3)查询数据:http://192.168.174.148:9200/accounts/person/1 get方式
返回:
4)查询所有数据:
5)全文检索
使用 Match查询,指定的匹配条件是desc
字段里面包含"数据库"这个词。返回结果如下