1. 程式人生 > >Elasticsearch全文檢索學習

Elasticsearch全文檢索學習

ElasticSearch官方網址:https://www.elastic.co

ElasticSearch官方網址(中文):https://www.elastic.co/cn/

Elasticsearch 權威指南(中文版文件,線上觀看):https://es.xiaoleilu.com/

1、ElasticSearch是一個基於Lucene的搜尋伺服器。它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java語言開發的,並作為Apache許可條款下的開放原始碼釋出,是一種流行的企業級搜尋引擎。ElasticSearch用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。官方客戶端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和許多其他語言中都是可用的。根據DB-Engines的排名顯示,Elasticsearch是最受歡迎的企業搜尋引擎,其次是Apache Solr,也是基於Lucene。

2、Elastic Stack生態(簡稱ELK Stack,包括 Elasticsearch、Kibana、Beats 和 Logstash)。Elasticsearch是與名為Logstash的資料收集和日誌解析引擎以及名為Kibana的分析和視覺化平臺一起開發的。

  a、Elasticsearch 是一個分散式的 RESTful 搜尋和分析引擎,可用來集中儲存您的資料,以便您對形形色色、規模不一的資料進行搜尋、索引和分析。Elasticsearch 是一個基於 JSON 的分散式搜尋和分析引擎。

  b、Logstash 是一個開源的資料採集引擎,具有實時管道傳輸功能。Logstash 能夠將來自單獨資料來源的資料動態集中到一起,對這些資料加以標準化並傳輸到您所選的地方。清理並民主化您的全部資料,將其用於多樣化的高階下游分析和視覺化用例。Logstash 是動態資料收集管道,擁有可擴充套件的外掛生態系統。

  c、Kibana,您可以對自己的 Elasticsearch 進行視覺化,還可以在 Elastic Stack 中進行導航,這樣您便可以進行各種操作了,從跟蹤查詢負載,到理解請求如何流經您的整個應用,都能輕鬆完成。Kibana 可謂 Elastic Stack 的窗戶。探索資料並管理堆疊。

  d、Beats 是一個面向輕量型採集器的平臺,這些採集器可從邊緣機器傳送資料。Beats 平臺集合了多種單一用途資料採集器。它們從成百上千或成千上萬臺機器和系統向 Logstash 或 Elasticsearch 傳送資料。

3、ElasticSearch的下載安裝。這裡使用elasticsearch-5.4.3.tar.gz,使用CentOS7進行安裝。版本進行學習,其實截至目前,Elasticsearch已經發布到Elasticsearch 7.4.0版本了。

將下載好的elasticsearch-5.4.3.tar.gz開始進行安裝。

1 [root@slaver4 package]# tar -zxvf elasticsearch-5.4.3.tar.gz -C /home/hadoop/soft/

可以檢視其目錄結構資訊,如下所示:

 1 [root@slaver4 soft]# ls
 2 elasticsearch-5.4.3
 3 [root@slaver4 soft]# cd elasticsearch-5.4.3/
 4 [root@slaver4 elasticsearch-5.4.3]# ls
 5 bin  config  lib  LICENSE.txt  modules  NOTICE.txt  plugins  README.textile
 6 [root@slaver4 elasticsearch-5.4.3]# ll
 7 total 224
 8 drwxr-xr-x.  2 root root   4096 Oct  9 22:06 bin       #啟動指令碼目錄
 9 drwxr-xr-x.  2 root root     75 Jun 22  2017 config     #配置檔案目錄
10 drwxr-xr-x.  2 root root   4096 Jun 22  2017 lib       #依賴jar包目錄
11 -rw-r--r--.  1 root root  11358 Jun 22  2017 LICENSE.txt
12 drwxr-xr-x. 12 root root    217 Jun 22  2017 modules    #模組目錄 
13 -rw-r--r--.  1 root root 194187 Jun 22  2017 NOTICE.txt
14 drwxr-xr-x.  2 root root      6 Jun 22  2017 plugins    #第三方外掛的目錄
15 -rw-r--r--.  1 root root   9548 Jun 22  2017 README.textile
16 [root@slaver4 elasticsearch-5.4.3]#

啟動ElasticSearch,如下所示:

啟動過程中我的報錯了,分析一下錯誤原因。

1 [root@slaver4 elasticsearch-5.4.3]# sh ./bin/elasticsearch
2 Java HotSpot(TM) Server VM warning: INFO: os::commit_memory(0x6ac00000, 2080374784, 0) failed; error='Cannot allocate memory' (errno=12)
3 #
4 # There is insufficient memory for the Java Runtime Environment to continue.
5 # Native memory allocation (mmap) failed to map 2080374784 bytes for committing reserved memory.
6 # An error report file with more information is saved as:
7 # /home/hadoop/soft/elasticsearch-5.4.3/hs_err_pid11491.log

由於elasticsearch5.x版本預設分配jvm空間大小為2g,修改jvm空間分配。(備註:我的虛擬機器給了1G的記憶體,如果硬體較好,可以多給記憶體)。

再次啟動報如下所示錯誤:

 1 [root@slaver4 elasticsearch-5.4.3]# sh ./bin/elasticsearch
 2 [2019-10-09T22:29:26,389][WARN ][o.e.b.Natives            ] unable to load JNA native support library, native methods will be disabled.
 3 java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/linux-x86/libjnidispatch.so) not found in resource path ([file:/home/hadoop/soft/elasticsearch-5.4.3/lib/elasticsearch-5.4.3.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-core-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-analyzers-common-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-backward-codecs-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-grouping-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-highlighter-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-join-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-memory-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-misc-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-queries-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-queryparser-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-sandbox-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-spatial-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-spatial-extras-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-spatial3d-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/lucene-suggest-6.5.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/securesm-1.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jopt-simple-5.0.2.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/hppc-0.7.1.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/joda-time-2.9.5.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/snakeyaml-1.15.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jackson-core-2.8.6.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jackson-dataformat-smile-2.8.6.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jackson-dataformat-yaml-2.8.6.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jackson-dataformat-cbor-2.8.6.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/t-digest-3.0.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/HdrHistogram-2.1.9.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/spatial4j-0.6.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jts-1.13.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/log4j-api-2.8.2.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/log4j-core-2.8.2.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/log4j-1.2-api-2.8.2.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/jna-4.4.0.jar, file:/home/hadoop/soft/elasticsearch-5.4.3/lib/java-version-checker-5.4.3.jar])
 4     at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:962) ~[jna-4.4.0.jar:4.4.0 (b0)]
 5     at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:922) ~[jna-4.4.0.jar:4.4.0 (b0)]
 6     at com.sun.jna.Native.<clinit>(Native.java:190) ~[jna-4.4.0.jar:4.4.0 (b0)]
 7     at java.lang.Class.forName0(Native Method) ~[?:1.8.0_191]
 8     at java.lang.Class.forName(Class.java:264) ~[?:1.8.0_191]
 9     at org.elasticsearch.bootstrap.Natives.<clinit>(Natives.java:45) [elasticsearch-5.4.3.jar:5.4.3]
10     at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) [elasticsearch-5.4.3.jar:5.4.3]
11     at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:194) [elasticsearch-5.4.3.jar:5.4.3]
12     at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:350) [elasticsearch-5.4.3.jar:5.4.3]
13     at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) [elasticsearch-5.4.3.jar:5.4.3]
14     at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) [elasticsearch-5.4.3.jar:5.4.3]
15     at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67) [elasticsearch-5.4.3.jar:5.4.3]
16     at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) [elasticsearch-5.4.3.jar:5.4.3]
17     at org.elasticsearch.cli.Command.main(Command.java:88) [elasticsearch-5.4.3.jar:5.4.3]
18     at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) [elasticsearch-5.4.3.jar:5.4.3]
19     at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) [elasticsearch-5.4.3.jar:5.4.3]
20 [2019-10-09T22:29:26,399][WARN ][o.e.b.Natives            ] cannot check if running as root because JNA is not available
21 [2019-10-09T22:29:26,399][WARN ][o.e.b.Natives            ] cannot install system call filter because JNA is not available
22 [2019-10-09T22:29:26,400][WARN ][o.e.b.Natives            ] cannot register console handler because JNA is not available
23 [2019-10-09T22:29:26,401][WARN ][o.e.b.Natives            ] cannot getrlimit RLIMIT_NPROC because JNA is not available
24 [2019-10-09T22:29:26,401][WARN ][o.e.b.Natives            ] cannot getrlimit RLIMIT_AS beacuse JNA is not available
25 [2019-10-09T22:29:26,764][INFO ][o.e.n.Node               ] [] initializing ...
26 [2019-10-09T22:29:26,884][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [10.9gb], net total_space [17.7gb], spins? [unknown], types [rootfs]
27 [2019-10-09T22:29:26,884][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] heap size [505.6mb], compressed ordinary object pointers [unknown]
28 [2019-10-09T22:29:26,886][INFO ][o.e.n.Node               ] node name [rYUCm8s] derived from node ID [rYUCm8sMRzaXu84zRj3duQ]; set [node.name] to override
29 [2019-10-09T22:29:26,886][INFO ][o.e.n.Node               ] version[5.4.3], pid[11871], build[eed30a8/2017-06-22T00:34:03.743Z], OS[Linux/3.10.0-957.el7.x86_64/i386], JVM[Oracle Corporation/Java HotSpot(TM) Server VM/1.8.0_191/25.191-b12]
30 [2019-10-09T22:29:26,887][INFO ][o.e.n.Node               ] JVM arguments [-Xms512m, -Xmx512m, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+DisableExplicitGC, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/home/hadoop/soft/elasticsearch-5.4.3]
31 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [aggs-matrix-stats]
32 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [ingest-common]
33 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-expression]
34 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-groovy]
35 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-mustache]
36 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-painless]
37 [2019-10-09T22:29:29,878][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [percolator]
38 [2019-10-09T22:29:29,879][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [reindex]
39 [2019-10-09T22:29:29,879][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty3]
40 [2019-10-09T22:29:29,879][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty4]
41 [2019-10-09T22:29:29,879][INFO ][o.e.p.PluginsService     ] [rYUCm8s] no plugins loaded
42 [2019-10-09T22:29:34,820][INFO ][o.e.d.DiscoveryModule    ] [rYUCm8s] using discovery type [zen]
43 [2019-10-09T22:29:36,045][INFO ][o.e.n.Node               ] initialized
44 [2019-10-09T22:29:36,045][INFO ][o.e.n.Node               ] [rYUCm8s] starting ...
45 [2019-10-09T22:29:36,285][INFO ][o.e.t.TransportService   ] [rYUCm8s] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
46 [2019-10-09T22:29:36,341][WARN ][o.e.b.BootstrapChecks    ] [rYUCm8s] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
47 [2019-10-09T22:29:36,341][WARN ][o.e.b.BootstrapChecks    ] [rYUCm8s] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
48 [2019-10-09T22:29:36,341][WARN ][o.e.b.BootstrapChecks    ] [rYUCm8s] system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
49 [2019-10-09T22:29:39,434][INFO ][o.e.c.s.ClusterService   ] [rYUCm8s] new_master {rYUCm8s}{rYUCm8sMRzaXu84zRj3duQ}{NzmJVJKWRTaua11gn8v4Tw}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
50 [2019-10-09T22:29:39,542][INFO ][o.e.g.GatewayService     ] [rYUCm8s] recovered [0] indices into cluster_state
51 [2019-10-09T22:29:39,559][INFO ][o.e.h.n.Netty4HttpServerTransport] [rYUCm8s] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
52 [2019-10-09T22:29:39,585][INFO ][o.e.n.Node               ] [rYUCm8s] started

解決方法如下所示:

直接刪除或備份Elasticsearch-a.b.c/lib目錄下面的jna檔案:

然後在/home/hadoop/soft/elasticsearch-5.4.3/lib目錄下載這個包,wget http://repo1.maven.org/maven2/net/java/dev/jna/jna/4.4.0/jna-4.4.0.jar。

 1 [root@slaver4 lib]# mv jna-4.4.0.jar jna-4.4.0.jar.bak
 2 [root@slaver4 lib]# wget http://repo1.maven.org/maven2/net/java/dev/jna/jna/4.4.0/jna-4.4.0.jar
 3 --2019-10-09 22:36:07--  http://repo1.maven.org/maven2/net/java/dev/jna/jna/4.4.0/jna-4.4.0.jar
 4 Resolving repo1.maven.org (repo1.maven.org)... 151.101.24.209
 5 Connecting to repo1.maven.org (repo1.maven.org)|151.101.24.209|:80... connected.
 6 HTTP request sent, awaiting response... 200 OK
 7 Length: 1091208 (1.0M) [application/java-archive]
 8 Saving to: ‘jna-4.4.0.jar’
 9 
10 100%[==========================================================================================>] 1,091,208    129KB/s   in 7.0s   
11 
12 2019-10-09 22:36:15 (151 KB/s) - ‘jna-4.4.0.jar’ saved [1091208/1091208]
13 
14 [root@slaver4 lib]# 

再次啟動ElasticSearch,報瞭如下所示的錯誤。

 1 [root@slaver4 elasticsearch-5.4.3]# sh ./bin/elasticsearch
 2 [2019-10-09T22:36:38,150][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
 3 org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
 4     at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[elasticsearch-5.4.3.jar:5.4.3]
 5     at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[elasticsearch-5.4.3.jar:5.4.3]
 6     at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67) ~[elasticsearch-5.4.3.jar:5.4.3]
 7     at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[elasticsearch-5.4.3.jar:5.4.3]
 8     at org.elasticsearch.cli.Command.main(Command.java:88) ~[elasticsearch-5.4.3.jar:5.4.3]
 9     at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[elasticsearch-5.4.3.jar:5.4.3]
10     at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[elasticsearch-5.4.3.jar:5.4.3]
11 Caused by: java.lang.RuntimeException: can not run elasticsearch as root
12     at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.4.3.jar:5.4.3]
13     at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:194) ~[elasticsearch-5.4.3.jar:5.4.3]
14     at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:350) ~[elasticsearch-5.4.3.jar:5.4.3]
15     at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) ~[elasticsearch-5.4.3.jar:5.4.3]
16     ... 6 more
17 [root@slaver4 elasticsearch-5.4.3]#

這應該是ElasticSearch不允許使用root使用者進行啟動。所以先建立一個使用者組elsearch,再建立使用者elsearch,再將解壓縮後的目錄許可權賦予這個新使用者即可。更改elasticsearch資料夾及內部檔案的所屬使用者及組為elsearch:elsearch。切換到elsearch使用者再啟動。

 1 [root@slaver4 ~]# groupadd elsearch
 2 [root@slaver4 ~]# useradd elsearch -g elsearch 
 3 [root@slaver4 ~]# passwd elsearch
 4 Changing password for user elsearch.
 5 New password: 
 6 BAD PASSWORD: The password contains the user name in some form
 7 Retype new password: 
 8 passwd: all authentication tokens updated successfully.
 9 [root@slaver4 ~]# cd /home/hadoop/soft/
10 [root@slaver4 soft]# ls
11 elasticsearch-5.4.3
12 [root@slaver4 soft]# chown -R elsearch:elsearch elasticsearch-5.4.3/
13 [root@slaver4 soft]# su elsearch
14 [elsearch@slaver4 soft]$ ls
15 elasticsearch-5.4.3
16 [elsearch@slaver4 soft]$ cd elasticsearch-5.4.3/
17 [elsearch@slaver4 elasticsearch-5.4.3]$ ls
18 bin  config  data  hs_err_pid11491.log  lib  LICENSE.txt  logs  modules  NOTICE.txt  plugins  README.textile

再次啟動如下所示:

如果啟動成功是可以看到started的。127.0.0.1:9200。ElasticSearch預設監聽的就是9200埠號的。

 1 [elsearch@slaver4 elasticsearch-5.4.3]$ sh ./bin/elasticsearch
 2 [2019-10-09T22:44:57,760][INFO ][o.e.n.Node               ] [] initializing ...
 3 [2019-10-09T22:44:57,938][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [10.9gb], net total_space [17.7gb], spins? [unknown], types [rootfs]
 4 [2019-10-09T22:44:57,938][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] heap size [505.6mb], compressed ordinary object pointers [unknown]
 5 [2019-10-09T22:44:57,944][INFO ][o.e.n.Node               ] node name [rYUCm8s] derived from node ID [rYUCm8sMRzaXu84zRj3duQ]; set [node.name] to override
 6 [2019-10-09T22:44:57,945][INFO ][o.e.n.Node               ] version[5.4.3], pid[12234], build[eed30a8/2017-06-22T00:34:03.743Z], OS[Linux/3.10.0-957.el7.x86_64/i386], JVM[Oracle Corporation/Java HotSpot(TM) Server VM/1.8.0_191/25.191-b12]
 7 [2019-10-09T22:44:57,945][INFO ][o.e.n.Node               ] JVM arguments [-Xms512m, -Xmx512m, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+DisableExplicitGC, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/home/hadoop/soft/elasticsearch-5.4.3]
 8 [2019-10-09T22:45:01,169][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [aggs-matrix-stats]
 9 [2019-10-09T22:45:01,180][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [ingest-common]
10 [2019-10-09T22:45:01,180][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-expression]
11 [2019-10-09T22:45:01,180][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-groovy]
12 [2019-10-09T22:45:01,180][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-mustache]
13 [2019-10-09T22:45:01,181][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-painless]
14 [2019-10-09T22:45:01,181][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [percolator]
15 [2019-10-09T22:45:01,181][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [reindex]
16 [2019-10-09T22:45:01,181][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty3]
17 [2019-10-09T22:45:01,181][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty4]
18 [2019-10-09T22:45:01,182][INFO ][o.e.p.PluginsService     ] [rYUCm8s] no plugins loaded
19 [2019-10-09T22:45:05,825][INFO ][o.e.d.DiscoveryModule    ] [rYUCm8s] using discovery type [zen]
20 [2019-10-09T22:45:06,893][INFO ][o.e.n.Node               ] initialized
21 [2019-10-09T22:45:06,894][INFO ][o.e.n.Node               ] [rYUCm8s] starting ...
22 [2019-10-09T22:45:07,126][INFO ][o.e.t.TransportService   ] [rYUCm8s] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
23 [2019-10-09T22:45:07,143][WARN ][o.e.b.BootstrapChecks    ] [rYUCm8s] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
24 [2019-10-09T22:45:07,143][WARN ][o.e.b.BootstrapChecks    ] [rYUCm8s] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
25 [2019-10-09T22:45:10,263][INFO ][o.e.c.s.ClusterService   ] [rYUCm8s] new_master {rYUCm8s}{rYUCm8sMRzaXu84zRj3duQ}{2aE_CPSiTZqCbaR6IU4B2g}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
26 [2019-10-09T22:45:10,333][INFO ][o.e.g.GatewayService     ] [rYUCm8s] recovered [0] indices into cluster_state
27 [2019-10-09T22:45:10,351][INFO ][o.e.h.n.Netty4HttpServerTransport] [rYUCm8s] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
28 [2019-10-09T22:45:10,394][INFO ][o.e.n.Node               ] [rYUCm8s] started

elasticsearch在後臺啟動如下所示,可以使用命令檢視啟動程序資訊。

1 [elsearch@slaver4 elasticsearch-5.4.3]$ ./bin/elasticsearch -d
2 [elsearch@slaver4 elasticsearch-5.4.3]$ ps -aux | grep elasticsearch
3 elsearch  12529 83.5 62.9 923084 627032 pts/2   Sl   22:57   0:11 /usr/local/soft/jdk1.8.0_191/bin/java -Xms512m -Xmx512m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -server -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/home/hadoop/soft/elasticsearch-5.4.3 -cp /home/hadoop/soft/elasticsearch-5.4.3/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
4 elsearch  12571  0.0  0.0 112708   992 pts/2    R+   22:57   0:00 grep --color=auto elasticsearch
5 [elsearch@slaver4 elasticsearch-5.4.3]$

使用curl localhost:9200可以看到訪問成功了。也說明你的ElasticSearch啟動成功了。

 1 [elsearch@slaver4 elasticsearch-5.4.3]$ curl localhost:9200
 2 {
 3   "name" : "rYUCm8s",
 4   "cluster_name" : "elasticsearch",
 5   "cluster_uuid" : "-VfRdnF3Syad_6QMsa7v3A",
 6   "version" : {
 7     "number" : "5.4.3",
 8     "build_hash" : "eed30a8",
 9     "build_date" : "2017-06-22T00:34:03.743Z",
10     "build_snapshot" : false,
11     "lucene_version" : "6.5.1"
12   },
13   "tagline" : "You Know, for Search"
14 }

外網訪問,在防火牆開啟9200埠命令。或者直接禁掉防火牆,如果本地學習的話。外網還無法訪問的話就需要改下配置檔案了。

1 [elsearch@slaver4 elasticsearch-5.4.3]$ ls
2 bin  config  data  hs_err_pid11491.log  lib  LICENSE.txt  logs  modules  NOTICE.txt  plugins  README.textile
3 [elsearch@slaver4 elasticsearch-5.4.3]$ cd config/
4 [elsearch@slaver4 config]$ ls
5 elasticsearch.yml  jvm.options  log4j2.properties  scripts
6 [elsearch@slaver4 config]$ vim elasticsearch.yml

在配置檔案,vim elasticsearch.yml,新增如下所示內容即可。

1 network.host: 0.0.0.0

再次啟動報瞭如下所示的錯誤:

下面使用紅色標記起來的就是錯誤。

 1 [elsearch@slaver4 bin]$ ./elasticsearch
 2 [2019-10-10T10:14:14,226][INFO ][o.e.n.Node               ] [] initializing ...
 3 [2019-10-10T10:14:14,371][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [10.9gb], net total_space [17.7gb], spins? [unknown], types [rootfs]
 4 [2019-10-10T10:14:14,372][INFO ][o.e.e.NodeEnvironment    ] [rYUCm8s] heap size [505.6mb], compressed ordinary object pointers [unknown]
 5 [2019-10-10T10:14:14,373][INFO ][o.e.n.Node               ] node name [rYUCm8s] derived from node ID [rYUCm8sMRzaXu84zRj3duQ]; set [node.name] to override
 6 [2019-10-10T10:14:14,373][INFO ][o.e.n.Node               ] version[5.4.3], pid[14069], build[eed30a8/2017-06-22T00:34:03.743Z], OS[Linux/3.10.0-957.el7.x86_64/i386], JVM[Oracle Corporation/Java HotSpot(TM) Server VM/1.8.0_191/25.191-b12]
 7 [2019-10-10T10:14:14,374][INFO ][o.e.n.Node               ] JVM arguments [-Xms512m, -Xmx512m, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+DisableExplicitGC, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/home/hadoop/soft/elasticsearch-5.4.3]
 8 [2019-10-10T10:14:17,064][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [aggs-matrix-stats]
 9 [2019-10-10T10:14:17,064][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [ingest-common]
10 [2019-10-10T10:14:17,065][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-expression]
11 [2019-10-10T10:14:17,065][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-groovy]
12 [2019-10-10T10:14:17,065][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-mustache]
13 [2019-10-10T10:14:17,065][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [lang-painless]
14 [2019-10-10T10:14:17,065][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [percolator]
15 [2019-10-10T10:14:17,066][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [reindex]
16 [2019-10-10T10:14:17,066][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty3]
17 [2019-10-10T10:14:17,066][INFO ][o.e.p.PluginsService     ] [rYUCm8s] loaded module [transport-netty4]
18 [2019-10-10T10:14:17,067][INFO ][o.e.p.PluginsService     ] [rYUCm8s] no plugins loaded
19 [2019-10-10T10:14:21,970][INFO ][o.e.d.DiscoveryModule    ] [rYUCm8s] using discovery type [zen]
20 [2019-10-10T10:14:23,169][INFO ][o.e.n.Node               ] initialized
21 [2019-10-10T10:14:23,169][INFO ][o.e.n.Node               ] [rYUCm8s] starting ...
22 [2019-10-10T10:14:23,666][INFO ][o.e.t.TransportService   ] [rYUCm8s] publish_address {192.168.110.133:9300}, bound_addresses {[::]:9300}
23 [2019-10-10T10:14:23,684][INFO ][o.e.b.BootstrapChecks    ] [rYUCm8s] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
24 ERROR: [2] bootstrap checks failed
25 [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
26 [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
27 [2019-10-10T10:14:23,697][INFO ][o.e.n.Node               ] [rYUCm8s] stopping ...
28 [2019-10-10T10:14:23,736][INFO ][o.e.n.Node               ] [rYUCm8s] stopped
29 [2019-10-10T10:14:23,736][INFO ][o.e.n.Node               ] [rYUCm8s] closing ...
30 [2019-10-10T10:14:23,765][INFO ][o.e.n.Node               ] [rYUCm8s] closed
31 [elsearch@slaver4 bin]$ 

上面的兩個錯誤解決方法如下所示:

錯誤一,[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]。解決方法如下所示:

錯誤原因,每個程序最大同時開啟檔案數太小,可通過下面2個命令檢視當前數量。

注意,切記,如果按照此方法修改,必須重啟你的虛擬機器,而且是root使用者修改的配置檔案,不然你解決完報的這兩個錯誤,再次啟動elasticsearch還是會報錯誤一,但是不會報錯誤二,所以重啟虛擬機器以後解決這兩個錯誤。

1 [root@slaver4 ~]# vim /etc/security/limits.conf

新增如下所示內容:

1 *               soft    nofile          65536
2 *               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生效。

1 [root@slaver4 ~]# vim /etc/sysctl.conf
2 [root@slaver4 ~]# sysctl -p
3 vm.max_map_count = 262144
4 [root@slaver4 ~]# 

新增內容如下所示:

1 vm.max_map_count=262144

操作如下所示:

解決完上述兩個錯誤以後,使用瀏覽器訪問也出現了正常,如下所示:

使用命令列,訪問出現如下所示表示正常啟動elasticsearch。

如何停止你的elasticsearch呢,如下所示:

方式一,如果是前臺啟動方式,即進入/home/hadoop/soft/elasticsearch-5.4.3/bin/目錄直接使用./elasticsearch啟動,直接ctrl+c就可以退出了elasticsearch。

方式二,如果是後臺啟動elasticsearch的話。./elasticsearch -d。這種後臺方式啟動。使用jps命令檢視程序號,然後使用kill -9 程序號。直接強制殺死的命令。

4、ElasticSearch的實用外掛Head的安裝。

  通過瀏覽器訪問http://192.168.110.133:9200/。返回的是json格式的資料,顯示效果不是很友好的,通過Head外掛提供的友好的web介面,解決了介面顯示的問題。同時Head外掛還實現了基本資訊的檢視,rest請求的模擬,以及資料的基本檢索。

使用命令下載elasticsearch head外掛,wget https://github.com/mobz/elasticsearch-head/archive/master.zip。

或者使用這種方式安裝elasticsearch-head外掛。

1 [elsearch@slaver4 package]$ yum install –y git
2 [elsearch@slaver4 package]$ git clone https://github.com/mobz/elasticsearch-head.git

我是使用wget下載zip包,直接解壓縮操作,安裝的elasticearch-head外掛。

 1 [root@slaver4 ~]# cd /home/hadoop/package/
 2 [root@slaver4 package]# ls
 3 elasticsearch-5.4.3.tar.gz           erlang-18.3-1.el7.centos.x86_64.rpm  haproxy-1.6.5.tar.gz      phantomjs-2.1.1-linux-x86_64.tar.bz2        rabbitmq-server-3.6.5-1.noarch.rpm
 4 elasticsearch-analysis-ik-5.4.3.zip  es-sql-site-standalone.zip           keepalived-1.2.18.tar.gz  rabbitmq_delayed_message_exchange-0.0.1.ez  socat-1.7.3.2-1.1.el7.x86_64.rpm
 5 [root@slaver4 package]# wget https://github.com/mobz/elasticsearch-head/archive/master.zip
 6 --2019-10-10 14:49:21--  https://github.com/mobz/elasticsearch-head/archive/master.zip
 7 Resolving github.com (github.com)... 13.229.188.59
 8 Connecting to github.com (github.com)|13.229.188.59|:443... connected.
 9 HTTP request sent, awaiting response... 302 Found
10 Location: https://codeload.github.com/mobz/elasticsearch-head/zip/master [following]
11 --2019-10-10 14:49:22--  https://codeload.github.com/mobz/elasticsearch-head/zip/master
12 Resolving codeload.github.com (codeload.github.com)... 13.250.162.133
13 Connecting to codeload.github.com (codeload.github.com)|13.250.162.133|:443... connected.
14 HTTP request sent, awaiting response... 200 OK
15 Length: 1353444 (1.3M) [application/zip]
16 Saving to: ‘master.zip’
17 
18 100%[=======================================================================================================================================================================>] 1,353,444    139KB/s   in 10s    
19 
20 2019-10-10 14:49:34 (126 KB/s) - ‘master.zip’ saved [1353444/1353444]
21 
22 [root@slaver4 package]# ls
23 elasticsearch-5.4.3.tar.gz           es-sql-site-standalone.zip  master.zip                                  rabbitmq-server-3.6.5-1.noarch.rpm
24 elasticsearch-analysis-ik-5.4.3.zip  haproxy-1.6.5.tar.gz        phantomjs-2.1.1-linux-x86_64.tar.bz2        socat-1.7.3.2-1.1.el7.x86_64.rpm
25 erlang-18.3-1.el7.centos.x86_64.rpm  keepalived-1.2.18.tar.gz    rabbitmq_delayed_message_exchange-0.0.1.ez
26 [root@slaver4 package]# 

下載好進行解壓縮操作,如下所示:

1 [root@slaver4 package]# unzip master.zip

由於head外掛本質上還是一個nodejs的工程,因此需要安裝node,使用npm來安裝依賴的包。

下載安裝node,wget https://nodejs.org/dist/latest-v8.x/node-v8.16.2-linux-x64.tar.xz,下載完畢進行解壓縮操作。

 1 [root@slaver4 package]# wget https://nodejs.org/dist/latest-v8.x/node-v8.16.2-linux-x64.tar.xz
 2 [root@slaver4 package]# ls
 3 elasticsearch-5.4.3.tar.gz           es-sql-site-standalone.zip  master.zip                            rabbitmq_delayed_message_exchange-0.0.1.ez
 4 elasticsearch-analysis-ik-5.4.3.zip  haproxy-1.6.5.tar.gz        node-v8.16.2-linux-x64.tar.xz         rabbitmq-server-3.6.5-1.noarch.rpm
 5 erlang-18.3-1.el7.centos.x86_64.rpm  keepalived-1.2.18.tar.gz    phantomjs-2.1.1-linux-x86_64.tar.bz2  socat-1.7.3.2-1.1.el7.x86_64.rpm
 6 [root@slaver4 package]#
 7 [root@slaver4 package]# tar -zxvf node-v8.16.2-linux-x64.tar.xz -C /home/hadoop/soft/
 8 [root@slaver4 package]# cd ../soft/
 9 [root@slaver4 soft]# ls
10 elasticsearch-5.4.3  elasticsearch-head-master  node-v8.16.2-linux-x64
11 [root@slaver4 soft]#

解壓縮完畢,進行配置環境變數。新增完Node環境變數以後重新整理配置檔案source /etc/profile。

1 [root@slaver4 soft]# vim /etc/profile
2 [root@slaver4 soft]# source /etc/profile

配置如下所示:

1 export NODE_HOME=/home/hadoop/soft/node-v8.16.2-linux-x64
2 export PATH=$PATH:$NODE_HOME/bin

然後進行檢視Node版本驗證。如下所示:

注意:Node的版本必須大於等於node6.0版本。

1 [root@slaver4 soft]# node -v
2 v8.16.2
3 [root@slaver4 soft]# npm -v
4 6.4.1
5 [root@slaver4 soft]#

執行一下npm,npm是Node的一個包安裝工具。

 1 [root@slaver4 soft]# ls
 2 elasticsearch-5.4.3  elasticsearch-head-master  node-v8.16.2-linux-x64
 3 [root@slaver4 soft]# cd elasticsearch-head-master/
 4 [root@slaver4 elasticsearch-head-master]# ls
 5 crx                elasticsearch-head.sublime-project  index.html    plugin-descriptor.properties  _site
 6 Dockerfile         Gruntfile.js                        LICENCE       proxy                         src
 7 Dockerfile-alpine  grunt_fileSets.js                   package.json  README.textile                test
 8 [root@slaver4 elasticsearch-head-master]# np
 9 npm    nproc  npx    
10 [root@slaver4 elasticsearch-head-master]# npm install

執行npm install報瞭如下所示的錯誤,真是坎坷萬千啊。

 1 [root@slaver4 elasticsearch-head-master]# npm install
 2 npm WARN deprecated [email protected]: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
 3 npm WARN deprecated [email protected]: Use the built-in module in node 9.0.0 or newer, instead
 4 npm WARN deprecated [email protected]: this package is now deprecated
 5 npm WARN deprecated [email protected]: Please use the native JSON object instead of JSON 3
 6 npm WARN deprecated [email protected]: Please use the native JSON object instead of JSON 3
 7 
 8 > [email protected] install /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt
 9 > node install.js
10 
11 PhantomJS not found on PATH
12 Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
13 Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
14 Receiving...
15   [========================================] 99%
16 Received 22866K total.
17 Extracting tar contents (via spawned process)
18 Removing /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom
19 Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570691935036/phantomjs-2.1.1-linux-x86_64 -> /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom
20 Phantom installation failed { Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570691935036/phantomjs-2.1.1-linux-x86_64' -> '/home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom'
21   errno: -13,
22   code: 'EACCES',
23   syscall: 'link',
24   path: '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570691935036/phantomjs-2.1.1-linux-x86_64',
25   dest: '/home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570691935036/phantomjs-2.1.1-linux-x86_64' -> '/home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom'
26 npm WARN [email protected] license should be a valid SPDX license expression
27 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
28 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
29 
30 npm ERR! code ELIFECYCLE
31 npm ERR! errno 1
32 npm ERR! [email protected] install: `node install.js`
33 npm ERR! Exit status 1
34 npm ERR! 
35 npm ERR! Failed at the [email protected] install script.
36 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
37 
38 npm ERR! A complete log of this run can be found in:
39 npm ERR!     /root/.npm/_logs/2019-10-10T07_18_58_885Z-debug.log

應該是缺少類庫,使用如下所示的命令npm install phantomjs-prebuilt -save進行安裝。

 1 [elsearch@slaver4 elasticsearch-head-master]$ npm install phantomjs-prebuilt -save
 2 npm WARN deprecated [email protected]: this package is now deprecated
 3 
 4 > [email protected] install /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt
 5 > node install.js
 6 
 7 PhantomJS not found on PATH
 8 Download already available at /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
 9 Verified checksum of previously downloaded file
10 Extracting tar contents (via spawned process)
11 Removing /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom
12 Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1570692541363/phantomjs-2.1.1-linux-x86_64 -> /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom
13 Writing location.js file
14 Done. Phantomjs binary available at /home/hadoop/soft/elasticsearch-head-master/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs
15 npm notice created a lockfile as package-lock.json. You should commit this file.
16 npm WARN [email protected] license should be a valid SPDX license expression
17 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
18 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
19 
20 + [email protected]
21 added 62 packages from 64 contributors, removed 4 packages and audited 1734 packages in 137.541s
22 found 37 vulnerabilities (18 low, 2 moderate, 17 high)
23   run `npm audit fix` to fix them, or `npm audit` for details
24 [elsearch@slaver4 elasticsearch-head-master]$ 

安裝完成以後,執行一下,使用如下命令npm run start,如下所示:

 1 [elsearch@slaver4 elasticsearch-head-master]$ npm run start
 2 
 3 > [email protected] start /home/hadoop/soft/elasticsearch-head-master
 4 > grunt server
 5 
 6 >> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
 7 
 8 Running "connect:server" (connect) task
 9 Waiting forever...
10 Started connect web server on http://localhost:9100

可以看到head外掛預設佔用9100埠,瀏覽器訪問http://192.168.110.133:9100/,如下所示。即可顯示head外掛介面。

由於elasticsearch和elasticsearch-head外掛是屬於兩個不同的程序的,他們之間的訪問是存在跨域問題的。

1 [root@slaver4 ~]# cd /home/hadoop/soft/elasticsearch-
2 -bash: cd: /home/hadoop/soft/elasticsearch-: No such file or directory
3 [root@slaver4 ~]# cd /home/hadoop/soft/elasticsearch-
4 elasticsearch-5.4.3/       elasticsearch-head-master/ 
5 [root@slaver4 ~]# cd /home/hadoop/soft/elasticsearch-5.4.3/
6 [root@slaver4 elasticsearch-5.4.3]# ls
7 bin  config  data  hs_err_pid11491.log  lib  LICENSE.txt  logs  modules  NOTICE.txt  plugins  README.textile
8 [root@slaver4 elasticsearch-5.4.3]# vim config/elasticsearch.yml

在/home/hadoop/soft/elasticsearch-5.4.3/config/elasticsearch.yml配置檔案中新增如下所示配置。解決跨域訪問的問題、支援所有的域名。

1 # 是否支援跨域
2 http.cors.enabled: true
3 #
4 # *表示支援所有域名
5 http.cors.allow-origin: "*"

此時,後臺啟動elasticsearch(切記,使用自己建立的使用者,不能使用root使用者哦,./elasticsearch -d),啟動elasticsearch-head外掛(切記,使用自己建立的使用者,npm run start)。

通過head外掛連線elasticsearch服務。輸入elasticsearch服務地址http://192.168.110.133:9200/,點選連線,右邊顯示黃色資訊說明連線成功,至此elasticsearch-head外掛安裝成功。

注意:green綠色代表叢集健康,正常執行。yellow黃色代表叢集健康不是很好,但是叢集可以使用。red紅色代表叢集狀態很差,搜尋可以使用,但是可能出現丟失資料的情況。

安裝grunt,可以使用後臺啟動elasticsearch-head外掛,操作如下所示:

安裝grunt,如下所示:

[elsearch@slaver4 elasticsearch-head-master]$ cd elasticsearch-head-master
[elsearch@slaver4 elasticsearch-head-master]$ npm install -g grunt --registry=https://registry.npm.taobao.org

在elasticsearch-head目錄下node_modules/grunt下如果沒有grunt二進位制程式,需要執行:

[elsearch@slaver4 elasticsearch-head-master]$ cd elasticsearch-head-master
[elsearch@slaver4 elasticsearch-head-master]$ npm install grunt --save

 1 [elsearch@slaver4 soft]$ cd elasticsearch-head-master/
 2 [elsearch@slaver4 elasticsearch-head-master]$ ls
 3 crx         Dockerfile-alpine                   Gruntfile.js       index.html  node_modules  package-lock.json             proxy           _site  test
 4 Dockerfile  elasticsearch-head.sublime-project  grunt_fileSets.js  LICENCE     package.json  plugin-descriptor.properties  README.textile  src
 5 [elsearch@slaver4 elasticsearch-head-master]$ clear
 6 [elsearch@slaver4 elasticsearch-head-master]$ ls
 7 crx         Dockerfile-alpine                   Gruntfile.js       index.html  node_modules  package-lock.json             proxy           _site  test
 8 Dockerfile  elasticsearch-head.sublime-project  grunt_fileSets.js  LICENCE     package.json  plugin-descriptor.properties  README.textile  src
 9 [elsearch@slaver4 elasticsearch-head-master]$ npm install -g grunt --registry=https://registry.npm.taobao.org
10 /home/hadoop/soft/node-v8.16.2-linux-x64/bin/grunt -> /home/hadoop/soft/node-v8.16.2-linux-x64/lib/node_modules/grunt/bin/grunt
11 + [email protected]
12 added 97 packages from 63 contributors in 10.985s
13 [elsearch@slaver4 elasticsearch-head-master]$ npm install grunt --save
14 npm WARN deprecated [email protected]: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
15 npm WARN [email protected] license should be a valid SPDX license expression
16 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
17 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
18 
19 + [email protected]
20 updated 1 package and audited 1734 packages in 88.081s
21 found 37 vulnerabilities (18 low, 2 moderate, 17 high)
22   run `npm audit fix` to fix them, or `npm audit` for details

下面的配置可以修改也可以不修改的,我這裡修改了。

修改elasticsearch-head下Gruntfile.js檔案,預設監聽在127.0.0.1下9200埠:

新增hostname: '192.168.110.133',這個配置。

 1 connect: {
 2         server: {
 3                 options: {
 4                         hostname: '192.168.110.133',
 5                         port: 9100,
 6                         base: '.',
 7                         keepalive: true
 8                 }
 9         }
10 }

修改 _site/app.js,修改http://localhost:9200欄位到本機ES埠與IP:

在命令模式,即可以使用:/localhost搜尋找到localhost,然後修改如下配置。顯示行號,也是在命令模式,使用:set nu既可以顯示行號。

1 將此配置
2 "http://localhost:9200";
3 
4 修改為了
5 "http://192.168.110.133:9200";

Vim的三種工作模式。

後臺啟動elasticsearch-head外掛,如下所示:

在/home/hadoop/soft/elasticsearch-head-master/node_modules/grunt/bin目錄下面,使用此命令後臺啟動。./grunt server &

 1 [elsearch@slaver4 elasticsearch-head-master]$ ls
 2 crx                elasticsearch-head.sublime-project  index.html    package.json                  proxy           src
 3 Dockerfile         Gruntfile.js                        LICENCE       package-lock.json             README.textile  test
 4 Dockerfile-alpine  grunt_fileSets.js                   node_modules  plugin-descriptor.properties  _site
 5 [elsearch@slaver4 elasticsearch-head-master]$ cd node_modules/gr
 6 graceful-fs/            grunt-contrib-concat/   grunt-contrib-watch/    grunt-legacy-log/       
 7 grunt/                  grunt-contrib-connect/  grunt-karma/            grunt-legacy-log-utils/ 
 8 grunt-contrib-clean/    grunt-contrib-copy/     grunt-known-options/    grunt-legacy-util/      
 9 [elsearch@slaver4 elasticsearch-head-master]$ cd node_modules/grunt/bin/
10 [elsearch@slaver4 bin]$ ls
11 grunt
12 [elsearch@slaver4 bin]$ ./grunt server &
13 [1] 10709
14 [elsearch@slaver4 bin]$ >> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
15 
16 Running "connect:server" (connect) task
17 Waiting forever...
18 Started connect web server on http://192.168.110.133:9100

作者:別先生

部落格園:https://www.cnblogs.com/biehongli/

如果您想及時得到個人撰寫文章以及著作的訊息推送,可以掃描上方二維碼,關注個人公眾號哦。

&n