一、 安装ElasticSearch

1.1 安装ElasticSearch

  1. 1.拉取镜像:docker pull elasticsearch:7.3.2
  2. 2.启动elasticsearchdocker 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. 3.浏览器测试是否成功:url:192.168.1.106:9200

1.2 安装Kibana可视化页面

  1. 1.拉取镜像:docker pull kibana:7.3.2
  2. 2.启动kibanadocker run -d --name kb -e ELASTICSEARCH_HOSTS=http://127.0.0.1:9200 --network=container:es kibana:7.3.2
  3. 3.浏览器测试是否成功(需要等待大概1分钟左右):url:192.168.1.106:5601/app/kibana

1.3 常用操作

  1. #添加文档
  2. PUT /demo/_doc/1
  3. {
  4. "name" : "张三",
  5. "job" : "经理",
  6. "age" : 25,
  7. "interests": [ "运动", "音乐" ]
  8. }
  9. demo:索引的名字
  10. _doc:类型的名字(7.x版本后统一用_doc)
  11. 1:员工的id
  12. PUT /demo/_doc/2
  13. {
  14. "name" : "李四",
  15. "job" : "经理",
  16. "age" : 22,
  17. "interests": [ "游戏", "音乐" ]
  18. }
  19. PUT /demo/_doc/3
  20. {
  21. "name" : "王五",
  22. "job" : "主管",
  23. "age" : 24,
  24. "interests": [ "运动", "睡觉" ]
  25. }
  26. #查询指定ID文档
  27. get /demo/_doc/1
  28. get /demo/_doc/2
  29. get /demo/_doc/3
  30. #查询多个文档
  31. get /demo/_search
  32. #精码匹配查询
  33. get demo/_search?q=age:25
  34. #查询20到30岁之间
  35. get demo/_search?q=age[20 TO 30]
  36. #复杂条件查询
  37. get demo/_search?q=age[20 TO 30]&sort=age:desc&from=0&size=2
  38. #DSL查询
  39. #term查询:完全匹配方式,不进行分词器分析,直接在倒排索引中匹配分词
  40. get demo/_search
  41. {
  42. "query": {
  43. "term": {
  44. "name": "张三"
  45. }
  46. }
  47. }
  48. #match查询,会使用分词器先进行分词,match查询相当于模糊匹配,只包含其中一部分关键词就行
  49. get demo/_search
  50. {
  51. "query": {
  52. "match": {
  53. "name": "张三"
  54. }
  55. }
  56. }
  57. #分词器测试
  58. get _analyze
  59. {
  60. "text":"张三"
  61. }

1.4 安装KI中文分词器

  1. 1.进入容器:
  2. [root@localhost ~]# docker exec -it es /bin/bash
  3. 2.下载分词器:
  4. 安装wget工具命令:yum -y install wget
  5. root@17aeb9b1c7fa:/usr/share/elasticsearch# wget https://github.com/medcl/elasticsearch-analysisik/releases/download/v7.3.2/elasticsearch-analysis-ik-7.3.2.zip
  6. 4.解压到/usr/share/elasticsearch/plugins/ik目录:
  7. 先创建ik目录:/usr/share/elasticsearch/plugins# mkdir ik
  8. root@17aeb9b1c7fa:/usr/share/elasticsearch# unzip -d ./plugins/ik/elasticsearchanalysis-ik-7.3.2.zip
  9. 5.安装好后重启容器测试:
  10. get _analyze
  11. {
  12. "analyzer":"ik_max_word",
  13. "text":"经理"
  14. }
  15. 6.创建索引模版指定分词器为IK
  16. PUT demo
  17. {
  18. "mappings": {
  19. "properties": {
  20. "name": {
  21. "type": "text",
  22. "analyzer": "ik_max_word"
  23. },
  24. "job": {
  25. "type": "text",
  26. "analyzer": "ik_max_word"
  27. },
  28. "age":{
  29. "type":"integer"
  30. }
  31. }
  32. }
  33. }

二、 集群搭建

  1. 1.先创建要挂载的目录及文件:
  2. 节点1所用到的挂载目录及文件:
  3. /es/data1:存储数据
  4. /es/config/es1.yml:集群配置
  5. 节点2所用到的挂载目录及文件:
  6. /es/data2
  7. /es/config/es2.yml
  8. 2.开放通信端口
  9. [root@localhost ES]# firewall-cmd --add-port=9300/tcp --permanent
  10. [root@localhost ES]# firewall-cmd --add-port=9301/tcp --permanent
  11. 3.第一个节点启动命令:
  12. 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
  13. 4.第二个节点启动命令:
  14. 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
  15. (查看集群状态:http://192.168.1.106:9200/_cat/nodes)
  16. 5.启动kibana启动命令:
  17. docker run --name kb -e ELASTICSEARCH_HOSTS=http://127.0.0.1:9200 -- network=container:es01 kibana:7.3.2

es01.yml

  1. cluster.name: elasticsearch-cluster
  2. node.name: node1
  3. node.master: true
  4. node.data: true
  5. bootstrap.memory_lock: false
  6. network.host: 0.0.0.0
  7. http.port: 9200
  8. transport.tcp.port: 9300
  9. discovery.seed_hosts: ["192.168.1.106:9300", "192.168.1.106:9301"]
  10. cluster.initial_master_nodes: ["node1", "node2"]
  11. http.cors.enabled: true
  12. http.cors.allow-origin: "*"

es02.yml

  1. cluster.name: elasticsearch-cluster
  2. node.name: node2
  3. node.master: true
  4. node.data: true
  5. bootstrap.memory_lock: false
  6. network.host: 0.0.0.0
  7. http.port: 9201
  8. transport.tcp.port: 9301
  9. discovery.seed_hosts: ["192.168.1.106:9300", "192.168.1.106:9301"]
  10. cluster.initial_master_nodes: ["node1", "node2"]
  11. http.cors.enabled: true
  12. 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. 1.修改配置sysctl.conf:vi /etc/sysctl.conf
  2. 2.添加内容:vm.max_map_count=262144
  3. 3.启动配置:sysctl -p