目录

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 nofilehard 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。

【原创】《从0开始学Elasticsearch》—初识Elasticsearch的更多相关文章

  1. 0基础学安卓--初识安卓Activity

    知识储备:windows+ Android Studio 等环境安装. 安卓中Activity代表页的意思,也就是☞我们手机上当前的整个界面显示,点击按钮等操作可以跳转到另外一个Activity中. ...

  2. 原创 | 手摸手带您学会 Elasticsearch 单机、集群、插件安装(图文教程)

    欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...

  3. 初识ElasticSearch

    概述 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. 分布式的 ...

  4. 初识 ElasticSearch

    场景:最近有同事分享了ElasticSearch Inverted Index,所以自己也了解一下基于Lucene的ES. 转载自:http://www.jianshu.com/p/05cff7175 ...

  5. Docker部署ELK 7.0.1集群之Elasticsearch安装介绍

    elk介绍这里不再赘述,本系列教程多以实战干货为主,关于elk工作原理介绍,详情查看官方文档. 一.环境规划 主机名 IP 角色 节点名 centos01 10.10.0.10 es node-10 ...

  6. 02-springboot整合elasticsearch初识

    1.ReactiveElasticsearchOperations     根据springboot官网提供的Elasticsearch操作,除了用rest风格的,还有reactiveElasticS ...

  7. 01-springboot整合elasticsearch初识

    1.elasticsearch 1.es简介      Elasticsearch 是一个分布式.高扩展.高实时的搜索与数据分析引擎.它能很方便的使大量数据具有搜索.分析和探索的能力.充分利用Elas ...

  8. ElasticSearch和ElasticSearch Head环境搭建和数据模拟

    首先elasticsearch-6.0.0\bin目录下运行elasticsearch服务 修改elasticsearch-6.0.0\elasticsearch.yml文件 在文件最后加入下面代码, ...

  9. 启动elasticsearch的时候报出Exception in thread "main" SettingsException[Failed to load settings from /usr/local/elasticsearch/config/elasticsearch.yml]; nested: MarkedYAMLException[while scanning a simple ke

    故障现象: [elasticsearch@tiantianml- ~]$ /usr/local/elasticsearch/bin/elasticsearch Exception in thread ...

随机推荐

  1. <项目><day12>通讯录(视频)

    1 需求分析(需求分析师) 功能分析: 1)添加联系人 2)修改联系人 3)删除联系人 4)查询所有联系人 2 需求设计(系统分析师/架构师/资深开发人员) 2.1设计实体(抽象实体) 联系人实体: ...

  2. Linux面试题完整修订附加答案

    册一: 1.Linux挂载Winodws共享文件夹 第一步:先在Windows上创建一个共享目录        Windows系统IP是172.16.18.56;共享文件夹:E:\test       ...

  3. JAVA学习(一):Java介绍及其平台、开发环境的配置与搭建

    Java介绍及其平台.开发环境的配置与搭建 1.Java的介绍 Java是一种面向对象的编程语言,具有跨平台.可移植.分布式.简单.可扩展等诸多特性.Java能够进行桌面应用.Web应用.分布式系统及 ...

  4. redis connetced refused remote

    239down vote I've been stuck with the same issue, and the preceding answer did not help me (albeit w ...

  5. [Bash] Find Files and Folders with `find` in Bash

    find is a powerful tool that can not only find files but it can run a command on each matching file ...

  6. 搜索学术论文訪问google的能用的几个IP地址

    google搜索引擎打不开时的解决的方法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过測试可用的IP,用来在不能域名訪问google的时候进行訪问 更新一个最新的. ...

  7. 为Html.EditorForModel自定义模版

    对于MVC视图渲染来说,大家应该不会陌生,但对于模型的渲染,不知道是否听说过,主要是说Model通过它属性的相关特性(DataType,UIHint)来将它们自动渲染到View上,这是一个比较不错的技 ...

  8. java基础入门-建立能够多client链接的ServerSocket

    承接上一篇文章,今天谈论一下能够多client链接的ServerSocket. 这里面注意涉及到的技术点是: 1.ServerSocket 2.多线程 这次我们分成两个类来实现,先上代码: packa ...

  9. linux文件读写 文件锁、select、poll【转】

    本文转载自:http://blog.csdn.net/fansongy/article/details/6853395 一.文件锁 文件锁用于多个用户共同使用或操作同一个文件.有读锁的时候可以再加读锁 ...

  10. bacth参数说明 cmd parameter

    http://www.robvanderwoude.com/parameters.php Windows NT 4 introduced a set of new features for comma ...