赞
踩
Elasticsearch
和
Kibana
版本一般需要保持一致才能一起使用,但是从
8.x.x
开始,安全验证不断加强,甚至8.x.x之间的版本安全验证方法都不一样,真的很恼火。
这里记录一次成功简单登陆Kibana
的实际经验。
运行Elasticsearch
容器
docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=true" \
-e "xpack.security.enrollment.enabled=true" \
-v your_host_es_data_path:/usr/share/elasticsearch/data \ #宿主机绝对路径挂载
-v your_host_es_plugins_path:/usr/share/elasticsearch/plugins \ #宿主机绝对路径挂载
--privileged \
--network es-net \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:8.11.3
重置elastic
密码,记住这段密码
docker exec -it es /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
重置kibana_system
密码,记住这段密码
docker exec -it es /usr/share/elasticsearch/bin/elasticsearch-reset-password -u kibana_system
运行Kibana
容器,账户密码填kibana_system
的
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
-e ELASTICSEARCH_USERNAME=kibana_system \
-e ELASTICSEARCH_PASSWORD=kibana_system_passwrod \ #刚才获得的kibana_system 密码
--network=es-net \
-p 5601:5601 \
kibana:8.11.3
访问http://localhost:5601
用elastic
的账号密码登录。
关于一些报错
kibana
容器创建时不允许用elastic
用户连接elasticsearch
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
报错SSL
错误8.11.3
版本的安全验证方法遇到的错误这里是官方的install with docker
教程,也是一坨shit。
https://www.elastic.co/guide/en/kibana/current/docker.html
这里是官方关于安全配置的docs,遇到什么问题就多翻翻。
https://www.elastic.co/guide/en/elasticsearch/reference/master/manually-configure-security.html
或者来社区多讨论讨论。
https://discuss.elastic.co/latest
这里是官方仓库
https://github.com/medcl/elasticsearch-analysis-ik
推荐有两种安装方式
# 进入容器内部
docker exec -it es /bin/bash
# 在线下载并安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.11.1/elasticsearch-analysis-ik-8.11.1.zip
#退出
exit
#重启容器
docker restart es
docker restart kibana
如果遇到ik版本和es版本不匹配问题请看下面
在发行版下载页面,找到和es
版本最接近的ik
版本(博主这里是ik8.11.1 + es8.11.3)
https://github.com/medcl/elasticsearch-analysis-ik/releases
在您的 your_host_es_plugins_path
目录下,创建一个名为 ik
的新文件夹。
将下载的 elasticsearch-analysis-ik-8.11.1.zip
文件解压到刚刚创建的 ik
文件夹中
修改plugin-descriptor.properties
文件
!如无需要请跳过,可能造成无法预估的bug
# 'version': plugin's version
version=8.11.3
# 'elasticsearch.version' version of elasticsearch compiled against
# You will have to release a new version of the plugin for each new
# elasticsearch release. This version is checked when the plugin
# is loaded so Elasticsearch will refuse to start in the presence of
# plugins with the incorrect elasticsearch.version.
elasticsearch.version=8.11.3
docker restart es
docker restart kibana
安装好了之后,登录kinaba
,找到Dev Tools - Console
#测试分词器
GET /_analyze
{
"text":"我爱吃冰淇淋,也喜欢小淇,i want to eat her",
"analyzer":"ik_smart"
}
#测试分词器
GET /_analyze
{
"text":"我爱吃冰淇淋,也喜欢小淇,i want to eat her",
"analyzer":"ik_max_word"
}
这里的句子分词ik_smart和ik_max_word区别不明显,可以换用“程序员”试试。
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">ext.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">stopword.dic</entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
这里的意思是,使用同目录下的ext.dic
作为扩展词汇;使用同目录下的stopword.dic
作为禁用词汇。这两个文件有就用,没有就新建。
最后记得重启es
容器
#测试分词器
GET /_analyze
{
"text":"程序员墨扛教育的课程可以白嫖啊,而且就业率高达95%哦,奥利给!嘤",
"analyzer":"ik_smart"
}
{ "tokens": [ { "token": "程序员", "start_offset": 0, "end_offset": 3, "type": "CN_WORD", "position": 0 }, { "token": "墨扛教育", "start_offset": 3, "end_offset": 7, "type": "CN_WORD", "position": 1 }, { "token": "课程", "start_offset": 8, "end_offset": 10, "type": "CN_WORD", "position": 2 }, { "token": "可以", "start_offset": 10, "end_offset": 12, "type": "CN_WORD", "position": 3 }, { "token": "白嫖", "start_offset": 12, "end_offset": 14, "type": "CN_WORD", "position": 4 }, { "token": "而且", "start_offset": 16, "end_offset": 18, "type": "CN_WORD", "position": 5 }, { "token": "就业率", "start_offset": 18, "end_offset": 21, "type": "CN_WORD", "position": 6 }, { "token": "高达", "start_offset": 21, "end_offset": 23, "type": "CN_WORD", "position": 7 }, { "token": "95", "start_offset": 23, "end_offset": 25, "type": "ARABIC", "position": 8 }, { "token": "奥利给", "start_offset": 28, "end_offset": 31, "type": "CN_WORD", "position": 9 } ] }
在发行版下载页面,找到和es
版本最接近的版本(博主这里是pinyin8.11.1 + es8.11.3)
https://github.com/medcl/elasticsearch-analysis-pinyin
在您的 your_host_es_plugins_path
目录下,创建一个名为 py
的新文件夹。
将下载的 elasticsearch-analysis-pinyin-8.11.1.zip
文件解压到刚刚创建的 py
文件夹中
修改plugin-descriptor.properties
文件
!如无需要请跳过,可能造成无法预估的bug
# 'version': plugin's version
version=8.11.3
# 'elasticsearch.version' version of elasticsearch compiled against
# You will have to release a new version of the plugin for each new
# elasticsearch release. This version is checked when the plugin
# is loaded so Elasticsearch will refuse to start in the presence of
# plugins with the incorrect elasticsearch.version.
elasticsearch.version=8.11.3
docker restart es
docker restart kibana
安装好了之后,登录kinaba
,找到Dev Tools - Console
#测试分词器
POST /_analyze
{
"text":"如家酒店还不错",
"analyzer":"pinyin"
}
pinyin
分词器默认时有很多缺点,比如每个字都拆分变成拼音,不符合一般需求,并且如果使用pinyin
分词器,默认的中文索引就没了,只剩下pinyin
索引了。所以,需要完善以下几点:
为了做到这几点,需要在创建索引库时构建一个自定义分词器,如下
PUT /test { "settings": { "analysis": { "analyzer": { "my_analyzer":{ "tokenizer":"ik_max_word", "filter":"py" } }, "filter": { "py":{ "type":"pinyin", "keep_full_pinyin":false, "keep_joined_full_pinyin":true, "keep_original":true, "limit_first_letter_length":16, "remove_duplicated_term":true, "none_chinese_pinyin_tokenize":false } } } }, "mappings": { "properties": { "name":{ "type": "text", "analyzer": "my_analyzer", "search_analyzer": "ik_smart" } } } }
我们自定义了三步之中的tokenizer
和filter
,前者用ik
分词,后者用pinyin
分词,同时自定义了pinyin
分词器的一些设置,分词时同时保留汉字和拼音,具体设置看pinyin
分词器的github
官网。同时设定了存入数据时使用分词器my_analyzer
,搜索时,使用分词器ik_smart
。
存入两个数据,如下
POST /test/_doc/1
{
"id":1,
"name":"狮子"
}
POST /test/_doc/2
{
"id":2,
"name":"虱子"
}
那么现在,索引库的具体内容如下所示
因为搜索时使用的是ik_smart
分词器,不是自定义分词器,所以这里已经解决了同音词的问题。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。