部署单点es

1.创建网络

因为我们还需要部署kibana容器,因此需要让es和kibana容器互联。这里先创建一个网络:

  1. docker network create es-net

2.加载镜像

elasticsearch和kibana的体积非常大,每个都有1G,建议用本地加载的方式,远程拉取比较慢。
把下载的elasticsearch安装包拷贝到服务器上

  1. docker load -i /home/es/es.tar

同理还有kibana的tar包也需要这样做。

  1. docker load -i /home/es/kibana.tar

3.运行es

部署单点es:

  1. docker run -d \
  2. --name es \
  3. -e "ES_JAVA_OPTS=-Xms256m -Xmx512m" \
  4. -e "discovery.type=single-node" \
  5. -v es-data:/usr/share/elasticsearch/data \
  6. -v es-plugins:/usr/share/elasticsearch/plugins \
  7. --privileged \
  8. --network es-net \
  9. -p 9200:9200 \
  10. -p 9300:9300 \
  11. elasticsearch:7.12.1

命令解释:

  • -e "cluster.name=es-docker-cluster":设置集群名称
  • -e "http.host=0.0.0.0":监听的地址,可以外网访问
  • -e "ES_JAVA_OPTS=-Xms64m -Xmx256m":内存大小,设置初始化内存为256m,最大内存为512m,这个非常耗内存,控制小一点,但是太小了运行起来容易挂
  • -e "discovery.type=single-node":非集群模式
  • -v es-data:/usr/share/elasticsearch/data:挂载逻辑卷,绑定es的数据目录
  • -v es-logs:/usr/share/elasticsearch/logs:挂载逻辑卷,绑定es的日志目录
  • -v es-plugins:/usr/share/elasticsearch/plugins:挂载逻辑卷,绑定es的插件目录
  • --privileged:授予逻辑卷访问权
  • --network es-net :加入一个名为es-net的网络中
  • -p 9200:9200:端口映射配置

在浏览器中输入:http://ip:9200 即可看到elasticsearch的响应结果:
image.png

4.解决内存不足问题

es一运行起来就自动关闭了,这是内存不足。哪怕向上面这样设置了512m还是会内存不足,没钱就是这样。

  1. 修改/etc目录下的sysctl.conf
  2. vim /etc/sysctl.conf
  3. 添加下面的内容
  4. vm.max_map_count=655360
  5. 然后保存退出执行以下命令
  6. sysctl -p

然后重启一下服务器,在运行es容器,最后才正常打开了。

部署kibana

kibana的版本一定要和es的版本一致

  1. docker run -d \
  2. --name kibana \
  3. -e ELASTICSEARCH_HOSTS=http://es:9200 \
  4. --network=es-net \
  5. -p 5601:5601 \
  6. kibana:7.12.1
  • --network es-net :加入一个名为es-net的网络中,与elasticsearch在同一个网络中
  • -e "ELASTICSEARCH_HOSTS=http://es:9200":设置elasticsearch的地址,因为kibana已经与elasticsearch在一个网络,因此可以用容器名直接访问elasticsearch
  • -p 5601:5601:端口映射配置

kibana启动一般比较慢,需要多等待一会,可以通过命令来查看运行是否成功

  1. docker logs -f kibana

运行成功以后访问 5601端口,就能看到这个画面了,注意内存,内存不够了es直接关了,es关了kibana也用不了
image.png

安装IK分词器

官方地址:
https://github.com/medcl/elasticsearch-analysis-ik
es自带的分词器对中文不太友好,不饿能理解中文的语义。
默认分词器standard和english分词器,对中文是一个字一个字的分的。
在kibana的dev_tools里面发DSL查询
image.png

  1. GET /_analyze
  2. {
  3. "analyzer": "english",
  4. "text": "今天真是一个very good的日子"
  5. }
  6. GET /_analyze
  7. {
  8. "analyzer": "standard",
  9. "text": "今天真是一个very good的日子"
  10. }

分成这样

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "今",
  5. "start_offset" : 0,
  6. "end_offset" : 1,
  7. "type" : "<IDEOGRAPHIC>",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "天",
  12. "start_offset" : 1,
  13. "end_offset" : 2,
  14. "type" : "<IDEOGRAPHIC>",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "真",
  19. "start_offset" : 2,
  20. "end_offset" : 3,
  21. "type" : "<IDEOGRAPHIC>",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "是",
  26. "start_offset" : 3,
  27. "end_offset" : 4,
  28. "type" : "<IDEOGRAPHIC>",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "一",
  33. "start_offset" : 4,
  34. "end_offset" : 5,
  35. "type" : "<IDEOGRAPHIC>",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "个",
  40. "start_offset" : 5,
  41. "end_offset" : 6,
  42. "type" : "<IDEOGRAPHIC>",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "very",
  47. "start_offset" : 6,
  48. "end_offset" : 10,
  49. "type" : "<ALPHANUM>",
  50. "position" : 6
  51. },
  52. {
  53. "token" : "good",
  54. "start_offset" : 11,
  55. "end_offset" : 15,
  56. "type" : "<ALPHANUM>",
  57. "position" : 7
  58. },
  59. {
  60. "token" : "的",
  61. "start_offset" : 15,
  62. "end_offset" : 16,
  63. "type" : "<IDEOGRAPHIC>",
  64. "position" : 8
  65. },
  66. {
  67. "token" : "日",
  68. "start_offset" : 16,
  69. "end_offset" : 17,
  70. "type" : "<IDEOGRAPHIC>",
  71. "position" : 9
  72. },
  73. {
  74. "token" : "子",
  75. "start_offset" : 17,
  76. "end_offset" : 18,
  77. "type" : "<IDEOGRAPHIC>",
  78. "position" : 10
  79. }
  80. ]
  81. }

我们用IK分词器,对中文进行分词

在线安装IK插件(较慢)

  1. # 进入容器内部
  2. docker exec -it elasticsearch /bin/bash
  3. # 在线下载并安装
  4. ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
  5. #退出
  6. exit
  7. #重启容器
  8. docker restart elasticsearch

离线安装ik插件(推荐)

  1. 安装插件需要知道elasticsearch的plugins目录位置,而我们用了数据卷挂载,因此需要查看elasticsearch的数据卷目录,通过下面命令查看: ```shell [root@izj6cev682kqg86i4ogj8rz ~]# docker volume inspect es-plugins [ {
    1. "CreatedAt": "2022-06-10T14:31:55+08:00",
    2. "Driver": "local",
    3. "Labels": null,
    4. "Mountpoint": "/var/lib/docker/volumes/es-plugins/_data",
    5. "Name": "es-plugins",
    6. "Options": null,
    7. "Scope": "local"
    } ]
  1. 说明plugins目录被挂载到了:`/var/lib/docker/volumes/es-plugins/_data`这个目录中
  2. 2. 下载好IK的压缩包解压
  3. 2. 上传到es容器的插件数据卷中,也就是`/var/lib/docker/volumes/es-plugins/_data`
  4. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/21464164/1654844248661-b97f05e3-91c7-4d0a-967e-8cf03755d909.png#clientId=uacbe990c-5942-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=148&id=u56ec01c8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=222&originWidth=672&originalType=binary&ratio=1&rotation=0&showTitle=false&size=10881&status=done&style=none&taskId=ua1a1a7d8-09fb-4d2a-9b18-d94dc9bd748&title=&width=448)
  5. 4. 重启es容器
  6. ```shell
  7. #重启容器
  8. docker restart es
  9. # 查看es日志
  10. docker logs -f es

测试

IK分词器包括两种模式:

  • ik_smart:最少切分
  • ik_max_word:最细切分

我们用这个两个查询开区别两者:

  1. GET /_analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text": "程序员小李有一个美丽动人的老婆"
  5. }
  6. GET /_analyze
  7. {
  8. "analyzer": "ik_max_word",
  9. "text": "程序员小李有一个美丽动人的老婆"
  10. }

ik_smart

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "程序员",
  5. "start_offset" : 0,
  6. "end_offset" : 3,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "小李",
  12. "start_offset" : 3,
  13. "end_offset" : 5,
  14. "type" : "CN_WORD",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "有",
  19. "start_offset" : 5,
  20. "end_offset" : 6,
  21. "type" : "CN_CHAR",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "一个",
  26. "start_offset" : 6,
  27. "end_offset" : 8,
  28. "type" : "CN_WORD",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "美丽动人",
  33. "start_offset" : 8,
  34. "end_offset" : 12,
  35. "type" : "CN_WORD",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "的",
  40. "start_offset" : 12,
  41. "end_offset" : 13,
  42. "type" : "CN_CHAR",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "老婆",
  47. "start_offset" : 13,
  48. "end_offset" : 15,
  49. "type" : "CN_WORD",
  50. "position" : 6
  51. }
  52. ]
  53. }

ik_max_word

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "程序员",
  5. "start_offset" : 0,
  6. "end_offset" : 3,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "程序",
  12. "start_offset" : 0,
  13. "end_offset" : 2,
  14. "type" : "CN_WORD",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "员",
  19. "start_offset" : 2,
  20. "end_offset" : 3,
  21. "type" : "CN_CHAR",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "小李",
  26. "start_offset" : 3,
  27. "end_offset" : 5,
  28. "type" : "CN_WORD",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "有",
  33. "start_offset" : 5,
  34. "end_offset" : 6,
  35. "type" : "CN_CHAR",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "一个",
  40. "start_offset" : 6,
  41. "end_offset" : 8,
  42. "type" : "CN_WORD",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "一",
  47. "start_offset" : 6,
  48. "end_offset" : 7,
  49. "type" : "TYPE_CNUM",
  50. "position" : 6
  51. },
  52. {
  53. "token" : "个",
  54. "start_offset" : 7,
  55. "end_offset" : 8,
  56. "type" : "COUNT",
  57. "position" : 7
  58. },
  59. {
  60. "token" : "美丽动人",
  61. "start_offset" : 8,
  62. "end_offset" : 12,
  63. "type" : "CN_WORD",
  64. "position" : 8
  65. },
  66. {
  67. "token" : "美丽",
  68. "start_offset" : 8,
  69. "end_offset" : 10,
  70. "type" : "CN_WORD",
  71. "position" : 9
  72. },
  73. {
  74. "token" : "动人",
  75. "start_offset" : 10,
  76. "end_offset" : 12,
  77. "type" : "CN_WORD",
  78. "position" : 10
  79. },
  80. {
  81. "token" : "的",
  82. "start_offset" : 12,
  83. "end_offset" : 13,
  84. "type" : "CN_CHAR",
  85. "position" : 11
  86. },
  87. {
  88. "token" : "老婆",
  89. "start_offset" : 13,
  90. "end_offset" : 15,
  91. "type" : "CN_WORD",
  92. "position" : 12
  93. }
  94. ]
  95. }

扩展IK词典

随着互联网的发展,“造词运动”也越发的频繁。出现了很多新的词语,在原有的词汇列表中并不存在。比如:“奥力给”,“钟离” 等。
所以我们的词汇也需要不断的更新,IK分词器提供了扩展词汇的功能。
在互联网项目中,在网络间传输的速度很快,所以很多语言是不允许在网络上传递的,如:关于宗教、政治等敏感词语,那么我们在搜索时也应该忽略当前词汇。
IK分词器也提供了强大的停用词功能,让我们在索引时就直接忽略当前的停用词汇表中的内容。

  1. 打开IK分词器config目录:

image.png

  1. 在IKAnalyzer.cfg.xml配置文件内容添加:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    3. <properties>
    4. <comment>IK Analyzer 扩展配置</comment>
    5. <!--用户可以在这里配置自己的扩展字典-->
    6. <entry key="ext_dict">ext.dic</entry>
    7. <!--用户可以在这里配置自己的扩展停止词字典-->
    8. <entry key="ext_stopwords">stopwords.dic</entry>
    9. </properties>

    :::danger ext.dic和stopword.dic是文件名,不是词,拓展词和违禁词写在这两个文件中 :::

  2. 在config目录下新建一个ext.dic和stopword.dic文件,存在就不用建了。把拓展词和违禁词分别写在里面,一行一个词 :::danger 当前文件的编码必须是 UTF-8 格式 :::

  3. 重启es docker restart es

  4. 测试

我在拓展词里面加了奥里给,钟离,巴巴托斯,违禁词里面加了啊,的

  1. GET /_analyze
  2. {
  3. "analyzer": "ik_smart",
  4. "text": "钟离比巴巴托斯强大的太多了啊,奥里给!"
  5. }

结果

  1. {
  2. "tokens" : [
  3. {
  4. "token" : "钟离",
  5. "start_offset" : 0,
  6. "end_offset" : 2,
  7. "type" : "CN_WORD",
  8. "position" : 0
  9. },
  10. {
  11. "token" : "比",
  12. "start_offset" : 2,
  13. "end_offset" : 3,
  14. "type" : "CN_CHAR",
  15. "position" : 1
  16. },
  17. {
  18. "token" : "巴巴托斯",
  19. "start_offset" : 3,
  20. "end_offset" : 7,
  21. "type" : "CN_WORD",
  22. "position" : 2
  23. },
  24. {
  25. "token" : "强大",
  26. "start_offset" : 7,
  27. "end_offset" : 9,
  28. "type" : "CN_WORD",
  29. "position" : 3
  30. },
  31. {
  32. "token" : "太",
  33. "start_offset" : 10,
  34. "end_offset" : 11,
  35. "type" : "CN_CHAR",
  36. "position" : 4
  37. },
  38. {
  39. "token" : "多了",
  40. "start_offset" : 11,
  41. "end_offset" : 13,
  42. "type" : "CN_WORD",
  43. "position" : 5
  44. },
  45. {
  46. "token" : "奥里给",
  47. "start_offset" : 15,
  48. "end_offset" : 18,
  49. "type" : "CN_WORD",
  50. "position" : 6
  51. }
  52. ]
  53. }

拼音分词器

要实现根据字母做补全,就必须对文档按照拼音分词。在GitHub上恰好有elasticsearch的拼音分词插件。地址:https://github.com/medcl/elasticsearch-analysis-pinyin
安装方式与IK分词器一样,看上面的离线安装ik插件:
①解压
②上传到服务器es-plugins的挂载目录中
③重启elasticsearch
④测试
image.png