目录
1. Elasticsearch 是什么2. Elasticsearch 中基本概念3. Elasticsearch 安装4. 访问 Elasticsearch
1. Elasticsearch 是什么
Elasticsearch 是一个基于 Lucene 的实时的分布式搜索分析引擎,开箱即用,整合了全文检索、结构化搜索、分析三大功能。
为什么不直接用 Lucene ?Lucene 只是一个全文检索引擎的架构,提供了大量可用的 API,但其并不是一个完整的全文检索引擎,使用 Lucene 时,你还需要自己写代码,自己去封装成全文检索引擎。
2. Elasticsearch 中基本概念
- field:字段。
- Document :文档,一条数据,用 json 格式表示。一个Document 包含多个field,json 中的 key 即 field 。
- Type:类型,一个 Document 分组,和 mysql 中的 table 类似,但又不完全相同。一个 Type 包含多个Document,同一个 Type 中的 Document 所拥有的 field 可以不同,但最好保持一致。
- Index :索引,类似于 mysql 中的 database。一个 Index 包含多个 Type。默认情况下,Document 中的所有 field 都会被索引,这样这些 field 才会被搜索到。Elasticsearch 中有一个倒排索引(Inverted Index)的概念,可以实现 mysql 中 B+Tree索引加速检索的目的,后面文章我们会详细介绍倒排索引。
- shard:分片。可以将一个 Index 中的数据切分为多个 shard,然后将之存储在多台服务器上,以增大一个 Index 可以存储的数据量,加速检索能力,提升系统性能。
- replica :副本。replica 与 shard 存储的数据是相同的,replica 起到备份的作用。当 shard 发生故障时,可以从 replica 中读取数据,保证系统不受影响。
- Node:节点,单个 Elasticsearch 实例。节点名称默认随机分配。
- Cluster:集群,一组 Elasticsearch 实例。默认集群名称为 elasticsearch。
3. Elasticsearch 安装
前提条件:系统中已成功安装 jdk8
下载并解压:
cd /usr/local
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.tar.gz
tar -zxvf elasticsearch-6.6.0.tar.gz -C .
查看解压后的目录:
[root@153-215 local]# cd elasticsearch-6.6.0
[root@153-215 elasticsearch-6.6.0]# ls
bin config lib LICENSE.txt logs modules NOTICE.txt plugins README.textile
启动 Elasticsearch:
[root@153-215 elasticsearch-6.6.0]# bin/elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d4cc0000, 724828160, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 724828160 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid16393.log
遂,查看 Elasticsearch 的启动脚本,看启动时是否对内存大小有要求:
[root@153-215 elasticsearch-6.6.0]# vim bin/elasticsearch
#!/bin/bash
# CONTROLLING STARTUP:
#
# This script relies on a few environment variables to determine startup
# behavior, those variables are:
#
# ES_PATH_CONF -- Path to config directory
# ES_JAVA_OPTS -- External Java Opts on top of the defaults set
#
# Optionally, exact memory values can be set using the `ES_JAVA_OPTS`. Note that
# the Xms and Xmx lines in the JVM options file must be commented out. Example
# values are "512m", and "10g".
#
# ES_JAVA_OPTS="-Xms8g -Xmx8g" ./bin/elasticsearch
source "`dirname "$0"`"/elasticsearch-env
ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options
JVM_OPTIONS=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_JVM_OPTIONS"`
ES_JAVA_OPTS="${JVM_OPTIONS//${ES_TMPDIR}/$ES_TMPDIR} $ES_JAVA_OPTS"
......
发现 Elasticsearch 启动时,读取了 jvm.options 文件,于是查看该文件:
[root@153-215 elasticsearch-6.6.0]# ls config
elasticsearch.yml jvm.options log4j2.properties role_mapping.yml roles.yml users users_roles
[root@153-215 elasticsearch-6.6.0]# cat config/jvm.options
## JVM configuration
################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms1g
-Xmx1g
......
修改 jvm 的最大可用内存和最小可用内存如下:
-Xms256m
-Xmx256m
再次启动 Elasticsearch:
[root@153-215 elasticsearch-6.6.0]# bin/elasticsearch
[2019-02-13T16:42:53,177][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [unknown] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.6.0.jar:6.6.0]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:116) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.6.0.jar:6.6.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:103) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:170) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[elasticsearch-6.6.0.jar:6.6.0]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-6.6.0.jar:6.6.0]
... 6 more
这段报错信息也就是说,不能以 root 用户的身份启动 Elasticsearch,这一要求也是出于系统安全考虑,所以此处我先将 Elasticsearch 目录及目录内文件的拥有者修改为另一个用户,然后再用另一个用户启动:
[root@153-215 elasticsearch-6.6.0]# cd ..
[root@153-215 local]# chown -R lilinru:lilinru elasticsearch-6.6.0
[root@153-215 local]# su lilinru
[lilinru@153-215 local]$ cd elasticsearch-6.6.0
[lilinru@153-215 elasticsearch-6.6.0]$ bin/elasticsearch
....
[2019-02-13T17:10:23,443][INFO ][o.e.n.Node ] [_xV7bTf] starting ...
[2019-02-13T17:10:23,618][INFO ][o.e.t.TransportService ] [_xV7bTf] publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}
[2019-02-13T17:10:23,636][WARN ][o.e.b.BootstrapChecks ] [_xV7bTf] max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2019-02-13T17:10:23,636][WARN ][o.e.b.BootstrapChecks ] [_xV7bTf] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
....
发现启动时存在两个问题:
问题一: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
。
解决此问题,我们可以编辑 /etc/security/limits.conf
文件最底端 soft nofile
和 hard nofile
的配置为 65536:
[root@153-215 elasticsearch-6.6.0]# vim /etc/security/limits.conf
...
# End of file
...
* soft nofile 65536
* hard nofile 65536
...
问题二:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
。
解决此问题,我们可以编辑 /etc/sysctl.conf
文件,在文件最底端添加如下配置:
vm.max_map_count=262144
注意添加完该配置,还需要执行一下 sysctl -p
命令,重新加载一下 sysctl.conf 配置文件。
解决完上述两个问题,再次重启 Elasticsearch,发现上述两个问题都木有了,且启动成功~
4. 访问 Elasticsearch
打开另外一个窗口,请求 Elasticsearch:
[root@153-215 ~]# curl localhost:9200
{
"name" : "_xV7bTf",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "i3whIPX_Qx2zvaJVZKQY1g",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
可以看到,Elasticsearch 返回了一个 json 对象,其中包含当前节点名称、集群名称、集群 uuid、版本信息、宣传语。
Elasticsearch 的基本认识就先写到这里,后续我们再一步步深入了解 Elasticsearch,使用 Elasticsearch。