【原創】《從0開始學Elasticsearch》—初識Elasticsearch

目錄

1. Elasticsearch 是什麼2. Elasticsearch 中基本概念3. Elasticsearch 安裝4. 訪問 Elasticsearchcss

1. Elasticsearch 是什麼

Elasticsearch 是一個基於 Lucene 的實時的分佈式搜索分析引擎,開箱即用,整合了全文檢索、結構化搜索、分析三大功能。
爲何不直接用 Lucene ?Lucene 只是一個全文檢索引擎的架構,提供了大量可用的 API,但其並非一個完整的全文檢索引擎,使用 Lucene 時,你還須要本身寫代碼,本身去封裝成全文檢索引擎。html

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
下載並解壓:java

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 .

查看解壓後的目錄:mysql

[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:sql

[root@153-215 elasticsearch-6.6.0]# bin/elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d4cc00007248281600) 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 的啓動腳本,看啓動時是否對內存大小有要求:json

[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 文件,因而查看該文件:bootstrap

[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 的最大可用內存和最小可用內存以下:vim

-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 目錄及目錄內文件的擁有者修改成另外一個用戶,而後再用另外一個用戶啓動:bash

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

相關文章
相關標籤/搜索