一、 安装ElasticSearch
1.1 安装ElasticSearch
1.拉取镜像:docker pull elasticsearch:7.3.2
2.启动elasticsearch:docker run -d --name es -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -e "discovery.type=single-node" elasticsearch:7.3.2
3.浏览器测试是否成功:url:192.168.1.106:9200
1.2 安装Kibana可视化页面
1.拉取镜像:docker pull kibana:7.3.2
2.启动kibana:docker run -d --name kb -e ELASTICSEARCH_HOSTS=http://127.0.0.1:9200 --network=container:es kibana:7.3.2
3.浏览器测试是否成功(需要等待大概1分钟左右):url:192.168.1.106:5601/app/kibana
1.3 常用操作
#添加文档
PUT /demo/_doc/1
{
"name" : "张三",
"job" : "经理",
"age" : 25,
"interests": [ "运动", "音乐" ]
}
demo:索引的名字
_doc:类型的名字(7.x版本后统一用_doc)
1:员工的id
PUT /demo/_doc/2
{
"name" : "李四",
"job" : "经理",
"age" : 22,
"interests": [ "游戏", "音乐" ]
}
PUT /demo/_doc/3
{
"name" : "王五",
"job" : "主管",
"age" : 24,
"interests": [ "运动", "睡觉" ]
}
#查询指定ID文档
get /demo/_doc/1
get /demo/_doc/2
get /demo/_doc/3
#查询多个文档
get /demo/_search
#精码匹配查询
get demo/_search?q=age:25
#查询20到30岁之间
get demo/_search?q=age[20 TO 30]
#复杂条件查询
get demo/_search?q=age[20 TO 30]&sort=age:desc&from=0&size=2
#DSL查询
#term查询:完全匹配方式,不进行分词器分析,直接在倒排索引中匹配分词
get demo/_search
{
"query": {
"term": {
"name": "张三"
}
}
}
#match查询,会使用分词器先进行分词,match查询相当于模糊匹配,只包含其中一部分关键词就行
get demo/_search
{
"query": {
"match": {
"name": "张三"
}
}
}
#分词器测试
get _analyze
{
"text":"张三"
}
1.4 安装KI中文分词器
1.进入容器:
[root@localhost ~]# docker exec -it es /bin/bash
2.下载分词器:
安装wget工具命令:yum -y install wget
root@17aeb9b1c7fa:/usr/share/elasticsearch# wget https://github.com/medcl/elasticsearch-analysisik/releases/download/v7.3.2/elasticsearch-analysis-ik-7.3.2.zip
4.解压到/usr/share/elasticsearch/plugins/ik目录:
先创建ik目录:/usr/share/elasticsearch/plugins# mkdir ik
root@17aeb9b1c7fa:/usr/share/elasticsearch# unzip -d ./plugins/ik/elasticsearchanalysis-ik-7.3.2.zip
5.安装好后重启容器测试:
get _analyze
{
"analyzer":"ik_max_word",
"text":"经理"
}
6.创建索引模版指定分词器为IK:
PUT demo
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"job": {
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type":"integer"
}
}
}
}
二、 集群搭建
1.先创建要挂载的目录及文件:
节点1所用到的挂载目录及文件:
/es/data1:存储数据
/es/config/es1.yml:集群配置
节点2所用到的挂载目录及文件:
/es/data2
/es/config/es2.yml
2.开放通信端口
[root@localhost ES]# firewall-cmd --add-port=9300/tcp --permanent
[root@localhost ES]# firewall-cmd --add-port=9301/tcp --permanent
3.第一个节点启动命令:
docker run -d --name es01 -p 5601:5601 -p 9200:9200 -p 9300:9300 -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" -v /es/data1:/usr/share/elasticsearch/data -v /es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml --privileged=true elasticsearch:7.3.2
4.第二个节点启动命令:
docker run -d --name es02 -p 9201:9201 -p 9301:9301 -e "ES_JAVA_OPTS=-Xms256m - Xmx256m" -v /es/data2:/usr/share/elasticsearch/data -v /es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml --privileged=true elasticsearch:7.3.2
(查看集群状态:http://192.168.1.106:9200/_cat/nodes)
5.启动kibana启动命令:
docker run --name kb -e ELASTICSEARCH_HOSTS=http://127.0.0.1:9200 -- network=container:es01 kibana:7.3.2
es01.yml
cluster.name: elasticsearch-cluster
node.name: node1
node.master: true
node.data: true
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["192.168.1.106:9300", "192.168.1.106:9301"]
cluster.initial_master_nodes: ["node1", "node2"]
http.cors.enabled: true
http.cors.allow-origin: "*"
es02.yml
cluster.name: elasticsearch-cluster
node.name: node2
node.master: true
node.data: true
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts: ["192.168.1.106:9300", "192.168.1.106:9301"]
cluster.initial_master_nodes: ["node1", "node2"]
http.cors.enabled: true
http.cors.allow-origin: "*"
附:bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144] 意思是elasticsearch用户拥有的内存权限太小,至少需要 262144 处理方法
1.修改配置sysctl.conf:vi /etc/sysctl.conf
2.添加内容:vm.max_map_count=262144
3.启动配置:sysctl -p