

Elasticsearch - Docker安装Elasticsearch8.12.2 - 王谷雨
source link: https://www.cnblogs.com/konghuanxi/p/18075715
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近在学习 ES,所以需要在服务器上装一个单节点的 ES
服务器环境:centos 7.9
目前最新版本是 8.12.2
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.12.2
新增配置文件 elasticsearch.yml
http.host: 0.0.0.0http.cors.enabled: truehttp.cors.allow-origin: "*"xpack.security.enabled: true
解释一下,前三行是开启远程访问和跨域,最后一行是开启密码访问
Networking | Elasticsearch Guide [8.12] | Elastic
创建数据挂载目录
在宿主机创建容器的挂载目录,我的目录如下
/home/elasticsearch
├── config
│ ├── analysis-ik
│ └── elasticsearch.yml
├── data
└── plugins
上一节说的配置文件 elasticsearch. yml 放在 /home/elasticsearch/config/elasticsearch. yml
创建 config/analysis-ik 、data、plugins 目录
这里需要重点说下 analysis-ik 文件夹,这是用来存放 ik 分词器的配置文件的目录,里面的文件由容器创建,所以需要分配与容器相同的权限(相同的用户)
不知道怎么弄的看以下步骤
# 启动临时的docker容器docker run --name es01 -e "discovery.type=single-node" -t docker.elastic.co/elasticsearch/elasticsearch:8.12.2# 查看用户ps -ef | grep elasticsearch# 查看用户idid 用户名# 删除临时的docker容器docker kill es01 && docker rm es01# 修改文件夹权限(uid就是用户id,自行替换)sudo chown -R uid:uid /home/elasticsearch/config/analysis-ik
启动 docker 容器
docker run --privileged=true --name elasticsearch \-p 9200:9200 -p 9300:9300 \-e "discovery.type=single-node" \-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \-v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \-v /home/elasticsearch/config/analysis-ik:/usr/share/elasticsearch/config/analysis-ik \-v /home/elasticsearch/data:/usr/share/elasticsearch/data \-v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins \-d docker.elastic.co/elasticsearch/elasticsearch:8.12.2
-e ES_JAVA_OPTS="-Xms 64 m -Xmx 128 m"
是指定 jvm 大小,参见 Manually set the heap size
安装 ik 分词器
# 进入docker容器docker exec -it elasticsearch /bin/bash# 安装ik分词器(此命令要在容器内执行)./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.12.2/elasticsearch-analysis-ik-8.12.2.zip
如果安装 ik 分词器时报错并且出现 Permission denied 字样,那就是 analysis-ik 文件夹的权限没有配置正确。
安装完成之后别急着退出容器,往下看
初始化密码
# 此命令要在容器内执行./bin/elasticsearch-setup-passwords interactive

设置 ik 分词器自定义用户词典
修改 /home/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml 文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties> <comment>IK Analyzer 扩展配置</comment> <!--用户可以在这里配置自己的扩展字典 --> <entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry> <!--用户可以在这里配置自己的扩展停止词字典--> <entry key="ext_stopwords">custom/ext_stopword.dic</entry> <!--用户可以在这里配置远程扩展字典 --> <entry key="remote_ext_dict">location</entry> <!--用户可以在这里配置远程扩展停止词字典--> <entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry></properties>
例如 custom/mydict.dic
,那么文件就存放在 /home/elasticsearch/config/analysis-ik/custom/mydict. dic
记得分配读写权限,不然容器没法去读取
本文介绍了 elasticsearch 8.12.2 的安装和 ik 分词器的安装。最后推荐一个连接 ES 的工具 es-client
es-head 已经停止更新了,有坑,快跑。
最后再 bb 一句,es-client 也有坑,查询请用 POST,因为 GET 方式没法携带请求 body
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK