在网上上我看已经有好多关于Elasticsearch的介绍,我就不在翻来覆去讲一些基本概念,大家感兴趣的可以自己去找一些资料巩固下。我这只为了顾及众多首次接触Elasticsearch,案例都讲的很浅显,还有就是受个人能力所限,实在写不出高大上的博文,各位读者发现有错误之处,还不要取笑我,给我指出来即可。
题外话,这一章节仅仅作为开发学习来构建基础的环境,并未考虑elasticsearch的高可用性,仅说明一些基础知识,带大家有一个认识。当然既然是自我发挥,也就是一家之言,难免有遗漏地方,希望大家也就本着看看。
1. 下载&安装
1.1. 环境需求
- CentOs7
- 内存4G+:这个因环境而异
- JDK11+:由于
elasticsearch
运行需要JDK环境,我机器 JDK 版本是 11,如果 JDK 低于 9.0 会有一些问题,下图我也贴出来。
Java HotSpot(TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
1.2. 下载
官方 elasticsearch 下载,下载 elasticsearch
,目前最新的稳定版本为 7.4.0 版本。
1.3. 安装
- 下载
elasticsearch
,会得到一个文件elasticsearch-X.X.X-linux-x86_64.tar.gz
- 创建个文件夹
elastic
- 再创建一个组,案例中我以
dev
命名 - 再创建一个用户,
elasticsearch
不允许使用root
启动,创建一个新的用户elastic
,并为这个账户赋予相应的权限来启动elasticsearch
。 - 解压文件
- 将
elasticsearch-X.X.X-linux-x86_64.tar.gz
文件移入elastic
中 - 重新调整下权限
chown -R
[root@localhost download]$ pwd
/data/download/
[root@localhost download]$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-linux-x86_64.tar.gz
[root@localhost download]$ cd ../app/
[root@localhost app]$ mkdir elastic
[root@localhost app]$ groupadd dev
[root@localhost app]$ useradd elastic -g dev
[root@localhost app]$ passwd elastic
[root@localhost app]$ chown -R elastic:dev elastic
[root@localhost app]$ su elastic
[elastic@localhost app]$ cd /elastic
[elastic@localhost elastic]$ cp ../../download/elasticsearch-7.4.0-linux-x86_64.tar.gz .
[elastic@localhost elastic]$ tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz
[elastic@localhost elastic]$ mv elasticsearch-7.4.0/ .
1.4. 修改配置文件
配置文件中有很多配置项,例如集群信息、端口等。
elasticsearch
本身为安全考虑,默认不允许外部访问,我们这里做演示,就需要将这个配置项修改掉,路径在 config/elasticsearch.yml
[elastic@localhost elastic]$ vi config/elasticsearch.yml
修改后的效果如下:
-- 激活节点1
node.name: node-1
-- 允许外部IP访问
network.host: 0.0.0.0
-- 把这个注释先放开并修改
cluster.initial_master_nodes: ["node-1"]
1.5. JVM配置
由于 elasticsearch
是 Java
开发的,所以可以通过 ${ES_HOME}/config/jvm.options
配置文件来设定 JVM
的相关设定。如果没有特殊需求按默认即可。
不过其中还是有两项最重要的-Xmx1g与-Xms1gJVM的最大最小内存。如果太小会导致Elasticsearch刚刚启动就立刻停止。太大会拖慢系统本身。
1.6. 启动&验证结果
- 启动
[elastic@localhost elastic]$ ./bin/elasticsearch
- 验证结果
elasticsearch
默认端口是 9200 ,打开地址:http://192.168.147.128:9200/
注意,启动后可能会有两种错误导致启动失败。
从中可以看出主要是打开时数量不够以及虚拟内存不足。
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决措施
- vi /etc/security/limits.conf
elastic hard nofile 65536
elastic soft nofile 65536
其中 elastic
为启动 elasticsearch
的用户名。
- vi /etc/sysctl.conf
vm.max_map_count=655360
再执行 sysctl -p
最后对 elasticsearch
进行重启。
1.7. 小结
从中我们看到, elasticsearch
安装本身并不困难,比较简单,修改配置文件以及注意 JDK
版本。
2. 可视化工具
elasticsearch
的可视化工具有很多,比如 elasticsearch-head
、Dejavu
、ElasticHD
等。
此处选择 ElasticHD
,比较适合学习以及演示,这是一个开源 elasticHD
, [Github下载地址](
https://github.com/360EntSecGroup-Skylar/ElasticHD/releases/download/1.4/elasticHD_linux_amd64.zip)
提供Windows和linux,但是这个版本有一个弊端,就是好久没更新,凑合着用呗。
1、unzip elasticHD_linux_amd64.zip
2、chmod -R 777 ElasticHD
3、./ElasticHD -p 0.0.0.0:9800
在浏览器中打开 http://192.168.244.128:9800/
就可以看到。
3. 中文分词插件IK
elasticsearch
本身对中文支持不够好,所以需要中文的分词插件,目前主流的都用 IK
。 以下这是 Google
的中文词条。
IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。从3.0版本开始,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。
3.1. IK安装
安装地址,截止当前 IK 最新版本是 v7.4.0
,但是我 elasticsearch
版本是 7.4所以下载与自己相对应的版本,否则分词插件将不能被识别。
- 下载
[root@localhost download]$ wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.0/elasticsearch-analysis-ik-7.4.0.zip
- 安装插件
在 elasticsearch
安装目录下 找到 plugins
文件夹并创建一个名为 ik
的目录,将下载的 elasticsearch-analysis-ik-7.10.0.zip
移入。
[elastic@localhost elastic]$ cd plugins
[elastic@localhost plugins]$ cd mkdir ik && cd ik
[elastic@localhost ik]$ cp ../../../download/elasticsearch-analysis-ik-7.4.0.zip .
[elastic@localhost ik]$ unzip elasticsearch-analysis-ik-7.4.0.zip
完成后,将 elasticsearch
重启,我们观察控制台
其中红线处就是 elasticsearch
将分词器加载,说明我们安装成功。
[2020-12-15T01:19:51,151][INFO ][o.e.p.PluginsService ] [centos8] loaded plugin [analysis-ik]
3.2. 分词器
上一章节我们演示对中文分词的安装,下来我们开始我们分词器验证之旅。
使用crul命令,输入下面的URL地址,验证分词器是否成功。
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏"}';
有的人喜欢 Postman
,也可以。
至此我们的中文分词器可以用啦。
3.3. ik_max_word和ik_smart
- ik_max_word: 将文本按最细粒度的组合来拆分,比如会将“中华五千年华夏”拆分为“五千年、五千、五千年华、华夏、千年华夏”,总之是可能的组合;
- ik_smart: 最粗粒度的拆分,比如会将“五千年华夏”拆分为“五千年、华夏”
当不添加分词类别,Elastic对于汉字默认使用standard只是将汉字拆分成一个个的汉字,而我们ik则更加的智能,下面通过几个案例来说明。
3.3.1. ik_smart分词
在JSON格式中添加analyzer节点内容为ik_smart
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏","analyzer": "ik_smart"}';
3.3.2. ik_max_word分词
在JSON格式中添加analyzer节点内容为ik_max_word
[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json" "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏","analyzer": "ik_max_word"}';
3.4. 自定义分词
IK
很友好,为我们提供热更新 IK
分词,在配置文件 {ES_HOME}/plugins/ik/config/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>
我们一般将需要自动更新的热词放在一个UTF8的txt文件里,再利用 nginx
,当 .txt
文件修改时,http server
会在客户端请求该文件时自动返回相应的 Last-Modified
和 ETag
。可以另外做一个工具来从业务系统提取相关词汇,并更新这个 .txt
文件。