安装elasticsearch,kibana

大家好, 我是苏麟 , 今天来安装es , kibana .

部署单点es

创建网络

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

docker network create es-net

镜像

我们用docker命令拉取和长传都可以选一种就可以 . 

拉取镜像

官网 :   Docker Hub Container Image Library | App Containerization

安装elasticsearch,kibana_第1张图片

 命令 :

docker pull elasticsearch:7.12.1
加载镜像

这里我们采用elasticsearch的7.12.1版本的镜像

提供了镜像的tar包:

链接:https://pan.baidu.com/s/1gly5GdwN0ccs1nKWodPcZg 
提取码:3goa

安装elasticsearch,kibana_第2张图片

大家将其上传到虚拟机中,然后运行命令加载即可:

# 导入数据
docker load -i es.tar

运行

docker命令,部署单点es:

docker run -d --name es -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -e "discovery.type=single-node" -v es-data:/usr/share/elasticsearch/data -v es-plugins:/usr/share/elasticsearch/plugins --privileged --network es-net -p 9200:9200 -p 9300:9300 elasticsearch:7.12.1

命令解释:

  • -e "cluster.name=es-docker-cluster" :设置集群名称
  • -e "http.host=0.0.0.0" :监听的地址,可以外网访问
  • -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" :内存大小
  • -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 :端口映射配置

 

 启动成功安装elasticsearch,kibana_第3张图片

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

安装elasticsearch,kibana_第4张图片

部署kibana

kibana可以给我们提供一个elasticsearch的可视化界面,便于我们学习。

镜像

下载 : 

docker pull kibana:7.12.1

上传 : 

安装elasticsearch,kibana_第5张图片

部署

运行docker命令,部署kibana

docker run -d --name kibana -e ELASTICSEARCH_HOSTS=http://es:9200 --network=es-net -p 5601:5601 kibana:7.12.1

安装elasticsearch,kibana_第6张图片

Kibana和elasticsearch的版本必须是一样的 . 

安装elasticsearch,kibana_第7张图片

 访问 :

http://ip地址:5601 

安装elasticsearch,kibana_第8张图片

 点击 Explore on my own

安装elasticsearch,kibana_第9张图片

  点击 Dev Tools

安装elasticsearch,kibana_第10张图片

安装elasticsearch,kibana_第11张图片

安装IK分词器

在线安装ik插件

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

 离线安装ik插件

查看数据卷目录

安装插件需要知道elasticsearch的plugins目录位置,而我们用了数据卷挂载,因此需要查看 elasticsearch的数据卷目录,通过下面命令查看:

docker volume inspect es-plugins

安装elasticsearch,kibana_第12张图片

说明plugins目录被挂载到了: /var/lib/docker/volumes/es-plugins/_data 这个目录中。

 上传到es容器的插件数据卷中

安装elasticsearch,kibana_第13张图片

重启容器

# 4、重启容器
docker restart es
# 查看es日志
docker logs -f es

测试:

IK分词器包含两种模式:

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

 ik_max_word

安装elasticsearch,kibana_第14张图片

{
  "tokens" : [
    {
      "token" : "程序员",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "程序",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "员",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "苏",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "麟",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "学习",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "java",
      "start_offset" : 7,
      "end_offset" : 11,
      "type" : "ENGLISH",
      "position" : 6
    },
    {
      "token" : "很简单",
      "start_offset" : 11,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "简单",
      "start_offset" : 12,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 8
    }
  ]
}

 ik_smart

安装elasticsearch,kibana_第15张图片

{
  "tokens" : [
    {
      "token" : "程序员",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "苏",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "麟",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "学习",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "java",
      "start_offset" : 7,
      "end_offset" : 11,
      "type" : "ENGLISH",
      "position" : 4
    },
    {
      "token" : "很简单",
      "start_offset" : 11,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 5
    }
  ]
}

扩展词词典

随着互联网的发展,“造词运动”也越发的频繁。出现了很多新的词语,在原有的词汇列表中并不存在。 比如:“奥力给” 等。

所以我们的词汇也需要不断的更新,IK分词器提供了扩展词汇的功能。

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

安装elasticsearch,kibana_第16张图片

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




    IK Analyzer 扩展配置
    
    ext.dic

 安装elasticsearch,kibana_第17张图片

3)新建一个 ext.dic,可以参考config目录下复制一个配置文件进行修改

 安装elasticsearch,kibana_第18张图片

奥利给
大傻子
杨科

 安装elasticsearch,kibana_第19张图片

4)重启elasticsearch

docker restart es

# 查看 日志
docker logs -f elasticsearch

5)测试效果:

安装elasticsearch,kibana_第20张图片

{
  "tokens" : [
    {
      "token" : "程序员",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "苏",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 1
    },
    {
      "token" : "麟",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "大",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "儿",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "杨科",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "爱吃",
      "start_offset" : 9,
      "end_offset" : 11,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "奥利给",
      "start_offset" : 11,
      "end_offset" : 14,
      "type" : "CN_WORD",
      "position" : 7
    }
  ]
}

停用词词典

在互联网项目中,在网络间传输的速度很快,所以很多语言是不允许在网络上传递的,如:关于宗教、 政治等敏感词语,那么我们在搜索时也应该忽略当前词汇。

IK分词器也提供了强大的停用词功能,让我们在索引时就直接忽略当前的停用词汇表中的内容。

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




        IK Analyzer 扩展配置
        
        ext.dic
        
        stopword.dic

 安装elasticsearch,kibana_第21张图片

3)在 stopword.dic 添加停用词

我儿

4)重启elasticsearch

# 重启服务
docker restart elasticsearch
docker restart kibana
# 查看 日志
docker logs -f elasticsearch

5)测试效果:

这里就不演示了 , 大家自己动手试试 .

这期就到这里 , 下期见!

你可能感兴趣的:(SpringCloud篇,1024程序员节,spring,cloud,搜索引擎,elasticsearch)