ElasticSearch是一款非常强大的、基于Lucene的开源搜索及分析引擎;它是一个实时的分布式搜索分析引擎,它能让你以前所未有的速度和规模,去探索你的数据
它被用作全文检索、结构化搜索、分析以及这三个功能的组合
除了搜索,结合Kibana、Logstash、Beats开源产品,Elastic Stack(简称ELK)还被广泛运用在大数据近实时分析领域,包括:日志分析、指标监控、信息安全等。它可以帮助你探索海量结构化、非结构化数据,按需创建可视化报表,对监控数据设置报警阈值,通过使用机器学习,自动识别异常状况。
ElasticSearch是基于Restful WebApi,使用Java语言开发的搜索引擎库类,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。其客户端在Java、C#、PHP、Python等许多语言中都是可用的。
文件的关系如下:
创建索引的过程:
搜索索引的过程:
分析 包含下面的过程:
分析器 实际上是将三个功能封装到了一个包里:
Elasticsearch提供了开箱即用的字符过滤器、分词器和token 过滤器。 这些可以组合起来形成自定义的分析器以用于不同的目的
Elasticsearch还附带了可以直接使用的预包装的分析器。接下来我们会列出最重要的分析器。为了证明它们的差异,我们看看每个分析器会从下面的字符串得到哪些词条:
"Set the shape to semi-transparent by calling set_trans(5)"
ElasticSearch内置分词器:
1、标准分析器 标准分析器是Elasticsearch默认使用的分析器。它是分析各种语言文本最常用的选择。
POST _analyze
{
"analyzer": "standard",
"text": "Set the shape to semi-transparent by calling set_trans(5)"
}
它根据 Unicode 联盟 定义的 单词边界 划分文本。删除绝大部分标点。最后,将词条小写。它会产生
set, the, shape, to, semi, transparent, by, calling, set_trans, 5
2、简单分析器
POST _analyze
{
"analyzer": "simple",
"text": "Set the shape to semi-transparent by calling set_trans(5)"
}
简单分析器在任何不是字母的地方分隔文本,将词条小写。它会产生
set, the, shape, to, semi, transparent, by, calling, set, trans
3、空格分析器
POST _analyze
{
"analyzer": "whitespace",
"text": "Set the shape to semi-transparent by calling set_trans(5)"
}
空格分析器在空格的地方划分文本。它会产生
Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
4、语言分析器 特定语言分析器可用于 很多语言。它们可以考虑指定语言的特点。例如, 英语 分析器附带了一组英语无用词(常用单词,例如 and 或者 the ,它们对相关性没有多少影响),它们会被删除。 由于理解英语语法的规则,这个分词器可以提取英语单词的 词干
POST _analyze
{
"analyzer": "language",
"text": "Set the shape to semi-transparent by calling set_trans(5)"
}
英语 分词器会产生下面的词条:
set, shape, semi, transpar, call, set_tran, 5
当我们 索引 一个文档,它的全文域被分析成词条以用来创建倒排索引。 但是,当我们在全文域 搜索 的时候,我们需要将查询字符串通过 相同的分析过程 ,以保证我们搜索的词条格式与索引中的词条格式一致。
全文查询,理解每个域是如何定义的,因此它们可以做正确的事:
以下是从主分片或者副本分片检索文档的步骤顺序:
在文档被检索时,已经被索引的文档可能已经存在于主分片上但是还没有复制到副本分片。
在这种情况下,副本分片可能会报告文档不存在,但是主分片可能成功返回文档。 一旦索引请求成功返回给用户,文档在主分片和副本分片都是可用的
使用 mget 取回多个文档的步骤顺序:
以下是使用单个 mget 请求取回多个文档所需的步骤顺序:
1、平台确认 这里我准备了一台Centos7虚拟机, 为方便选择后续安装的版本,所以需要看下系统版本信息。
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
2、安装java环境 安装 Elasticsearch 之前,你需要先安装一个较新的版本的 Java,最好的选择是,你可以从 www.java.com (opens new window) 获得官方提供的最新版本的 Java。本次使用openjdk,安装以后,确认是否安装成功:
[root@localhost ~]# yum install -y java-1.8.0-openjdk.x86_64
[root@localhost ~]# java -version
openjdk version "1.8.0_332"
OpenJDK Runtime Environment (build 1.8.0_332-b09)
OpenJDK 64-Bit Server VM (build 25.332-b09, mixed mode)
3、下载ElasticSearch
curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.2.2-linux-x86_64.tar.gz
4、解压ElasticSearch
[root@localhost ~]# tar zxf elasticsearch-8.2.2-linux-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ll /usr/local/
总用量 0
drwxr-xr-x. 2 root root 28 5月 5 11:42 bin
drwxr-xr-x 9 root root 155 5月 25 23:53 elasticsearch-8.2.2
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 5月 5 11:05 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
5、增加ElasticSearch用户 必须创建一个非root用户来运行ElasticSearch(ElasticSearch5及以上版本,基于安全考虑,强制规定不能以root身份运行
# 增加ElasticSearch用户
[root@localhost ~]# useradd elasticsearch
[root@localhost ~]# passwd elasticsearch
更改用户 elasticsearch 的密码 。
新的 密码:
无效的密码: 密码未通过字典检查 - 过于简单化/系统化
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
# 修改目录权限至新增的elasticsearch用户
[root@localhost ~]# chown -R elasticsearch /usr/local/elasticsearch-8.2.2
# 增加data和log存放区,并赋予elasticsearch用户权限
[root@localhost ~]# mkdir -p /data/es
[root@localhost ~]# chown -R elasticsearch /data/es
[root@localhost ~]# mkdir -p /var/log/es
[root@localhost ~]# chown -R elasticsearch /var/log/es
然后修改上述的data和log路径,关闭安全验证 vim /usr/local/elasticsearch-8.2.2/config/elasticsearch.yml
[root@localhost ~]# vim /usr/local/elasticsearch-8.2.2/config/elasticsearch.yml
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /data/es
#
# Path to log files:
#
path.logs: /var/log/es
# Enable security features
xpack.security.enabled: false
6、修改Linux系统的限制配置
[root@localhost ~]# cat >> /etc/security/limits.conf << EOF
# es limit
elasticsearch soft nofile 65536
elasticsearch hard nofile 65536
elasticsearch soft nproc 4096
elasticsearch hard nproc 4096
EOF
7、启动es
# 配置自动启动
[root@localhost ~]# cat > /usr/lib/systemd/system/elasticsearch.service << EOF
[Unit]
Description=elasticsearch
After=network.target
[Service]
Type=simple
User=elasticsearch
LimitNOFILE=100000
LimitNPROC=100000
Restart=no
ExecStart=/usr/local/elasticsearch-8.2.2/bin/elasticsearch
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[root@localhost ~]# systemctl enable elasticsearch
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
# 启动es
[root@localhost ~]# systemctl start elasticsearch
8、查看es是否启动成功
[root@localhost ~]# netstat -ntlp | grep 9200
tcp6 0 0 :::9200 :::* LISTEN 22558/java
[root@localhost ~]# curl -v 127.0.0.1:9200
* About to connect() to 127.0.0.1 port 9200 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9200 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:9200
> Accept: */*
>
< HTTP/1.1 200 OK
< X-elastic-product: Elasticsearch
< content-type: application/json
< content-length: 544
<
{
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "34WvcK4CRaK-20R-xmdsHA",
"version" : {
"number" : "8.2.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "9876968ef3c745186b94fdabd4483e01499224ef",
"build_date" : "2022-05-25T15:47:06.259735307Z",
"build_snapshot" : false,
"lucene_version" : "9.1.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
* Connection #0 to host 127.0.0.1 left intact
1、下载kibana
[root@localhost ~]# curl -O https://artifacts.elastic.co/downloads/kibana/kibana-8.2.2-linux-x86_64.tar.gz
2、解压kibana
[root@localhost ~]# tar zxf kibana-8.2.2-linux-x86_64.tar.gz -C /usr/local/
3、使用elasticsearch用户权限
[root@localhost ~]# chown -R elasticsearch /usr/local/kibana-8.2.2
# 配置kibana远程访问
[root@localhost ~]# vim /usr/local/kibana-8.2.2/config/kibana.yml
server.host: 0.0.0.0
4、启动kibana
# 配置自动启动
[root@localhost ~]# cat > /usr/lib/systemd/system/kibana.service << EOF
[Unit]
Description=kibana
After=network.target
[Service]
Type=simple
User=elasticsearch
Restart=no
ExecStart=/usr/local/kibana-8.2.2/bin/kibana
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
[root@localhost ~]# systemctl enable kibana
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /usr/lib/systemd/system/kibana.service.
# 启动es
[root@localhost ~]# systemctl start kibana
5、界面访问
停止kibana和elasticsearch服务
将xpack.security.enabled设置添加到ES_PATH_CONF/elasticsearch.yml文件并将值设置为true
启动elasticsearch (./bin/elasticsearch -d)
执行如下密码设置器,./bin/elasticsearch-setup-passwords interactive来设置各个组件的密码
[root@localhost ~]# /usr/local/elasticsearch-8.2.2/bin/elasticsearch-setup-passwords interactive
******************************************************************************
Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This command will be removed in a future release.
******************************************************************************
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
将elasticsearch.username设置添加到KIB_PATH_CONF/kibana.yml 文件并将值设置给elastic用户: elasticsearch.username: “kibana_system”
创建kibana keystore, ./bin/kibana-keystore create
在kibana keystore 中添加密码 ./bin/kibana-keystore add elasticsearch.password
[root@localhost ~]# /usr/local/kibana-8.2.2/bin/kibana-keystore add elasticsearch.password
Enter value for elasticsearch.password: *******
重启kibana 服务即可 nohup ./bin/kibana &
然后就可以使用密码登录了: