Elasticsearch入門實踐

一. 系統環境html

操做系統:CentOS release 6.8 (Final)
ES版本:6.1.1java

二. 安裝

先確認安裝了Java運行時環境:node

[es@localhost]$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

解壓ES壓縮包:linux

[es@localhost]$ curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.zip
[es@localhost]$ unzip elasticsearch-6.1.1.zip

三. 啓動

1. 啓動ES單節點

[es@localhost]$ cd elasticsearch-6.1.1
[es@localhost]$ ./bin/elasticsearch      # 在前臺啓動,能夠經過CTRL + C中止
[es@localhost]$ ./bin/elasticsearch -d   # 在後臺以守護進程模式運行,經過信號通知關閉: kill -SIGTERM $pid

固然,對於在後臺以守護進程模式運行的ES,能夠在啓動時將進程ID保存到指定文件,在中止時讀取進程ID,發送SIGTERM信號給進程進行關閉。git

[es@localhost]$ cd elasticsearch-6.1.1
[es@localhost]$ ./bin/elasticsearch -p /tmp/elasticsearch-pid -d
[es@localhost]$ kill -SIGTERM `cat /tmp/elasticsearch-pid`

實際上,咱們還能夠直接將這個啓動和中止ES的操做命令寫到一個腳本文件中來實現,這樣就避免了每次都輸入命令。
注意: 在Shell腳本不能直接使用信號SIGRTMIN,須要使用信號編號15代替,如:程序員

#!/bin/bash
pid=`cat /tmp/elasticsearch-pid`
kill -15 $pid

驗證是否啓動成功:github

[es@localhost]$ curl http://localhost:9200/?pretty
{
  "name" : "4t5PbHS",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "l7iMk0Y2QOWWu5UG-MWlpA",
  "version" : {
    "number" : "6.1.1",
    "build_hash" : "bd92e7f",
    "build_date" : "2017-12-17T20:23:25.338Z",
    "build_snapshot" : false,
    "lucene_version" : "7.1.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

另外,須要注意的是經過上述方式啓動的ES只能在本地訪問,即:只能經過localhost方式訪問。web

[es@localhost]$ netstat -anpt|grep 92
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 192.168.80.133:22           192.168.80.1:52710          ESTABLISHED -                   
tcp        0      0 192.168.80.133:22           192.168.80.1:52730          ESTABLISHED -                   
tcp        0     64 192.168.80.133:22           192.168.80.1:52742          ESTABLISHED -                   
tcp        0      0 ::ffff:127.0.0.1:9200       :::*                        LISTEN      4512/java           
tcp        0      0 ::1:9200                    :::*                        LISTEN      4512/java

一般咱們的ES是須要可以在外部訪問的,因此須要修改elasticsearch.yml中的network.host參數爲network.host: 0.0.0.0算法

2. 啓動ES集羣

節點(node)是一個運行着的Elasticsearch實例。集羣(cluster)是一組具備相同cluster.name的節點集合,他們協同工做,共享數據並提供故障轉移和擴展功能,固然一個節點也能夠組成一個集羣。必須設置一個合適的名字來替代cluster.name的默認值,這樣能夠防止一個新啓動的節點加入到相同網絡中的另外一個同名的集羣中。能夠經過修改文件elasticsearch.yml中參數cluster.name值,而後重啓ES來作到這一點。
ES默認的集羣名稱爲:elasticsearch。spring

四. ES應用實踐

1. 如何與ES進行交互

關於與ES的交互方式,總結起來爲2種:Java API和RESTful接口。
其中,Java API比較混亂,在不一樣的版本中叫法還不一樣。下面,咱們對在不一樣版本的客戶端進行詳細說明。

1.1 Java API

(1)[v0.90, v2.2]:在ES2.2及以前的版本中,ES爲Java用戶提供了兩種內置客戶端:

  • 節點客戶端(Node Client)
    節點客戶端以無數據節點身份加入集羣,換言之,它本身不存儲任何數據,可是它知道數據在集羣中的具體位置,而且可以直接轉發請求到對應的節點上。

  • 傳輸客戶端(Transport Client)
    這個更輕量的傳輸客戶端可以發送請求到遠程集羣。它本身不加入集羣,只是簡單轉發請求給集羣中的節點。

兩種Java客戶端都經過9300端口與集羣交互,使用ES傳輸協議(ElasticsearchTransport Protocol)。集羣中的節點之間也經過9300端口進行通訊。若是此端口未開放,你的節點將不能組成集羣。
具體來講這兩種Java客戶端的使用方式不相同:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>4.5.1</version>
</dependency>

使用節點客戶端:

// Embedded node clients behave just like standalone nodes,
// which means that they will leave the HTTP port open!
Node node = new NodeBuilder()
        .settings(Settings.settingsBuilder().put("http.enabled", false).put("path.home","D:\\elasticsearch-2.2.2"))
        .clusterName("elasticsearch")
        .client(true) // should not hold data
        .node();
Client client = node.client();

// 添加文檔
Map<String, Object> source = new HashMap<String, Object>();
source.put("first_name", "John");
source.put("last_name", "Smith");
source.put("age", 25);
source.put("about", "I love to go rock climbing");
source.put("interests", Arrays.asList(new String[] {"sports", "music"}));
IndexRequest req = new IndexRequest().index("megacorp").type("employee")
        .id("1")
        .source(source);
client.index(req);

// on shutdown
node.close();

關於節點客戶端,須要作幾點說明:

  1. 節點客戶端將做爲一個節點加入ES集羣,故須要明確設置屬性:"cluster.name": "elasticsearch"。
  2. 一般咱們不須要使用在該節點上存儲數據,設置屬性"node.client": "true"。
  3. 默認配置下節點客戶端將要負責響應外部對於ES集羣的請求,即:全部其餘客戶端對ES集羣的操做都須要節點客戶端進行響應,若是不但願承擔這樣的工做,須要設置屬性:"http.enabled": false。
    技術分享圖片
    另外,使用節點客戶端還須要注意一個問題:頻繁起動和中止一個或多個節點客戶端會致使沒必要要的ES集羣抖動。

使用傳輸客戶端:

Settings settings = Settings.settingsBuilder()
        .put("cluster.name", "elasticsearch")
        .put("client.transport.sniff", true)
        .build();
Client client = TransportClient.builder().settings(settings).build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        //.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("test.com"), 9300));

// 添加文檔
Map<String, Object> source = new HashMap<String, Object>();
source.put("first_name", "Jane");
source.put("last_name", "Smith");
source.put("age", 32);
source.put("about", "I like to collect rock albums");
source.put("interests", Arrays.asList(new String[] {"music"}));
IndexRequest req = new IndexRequest().index("megacorp").type("employee")
        .id("2")
        .source(source);
client.index(req);

// on shutdown
client.close();

很顯然,傳輸客戶端實際上是使用輪訓算法與ES集羣中的全部節點創建鏈接,從而達到負載均衡的效果。
技術分享圖片

(2)[v2.3, v2.4]:在ES2.3和2.4版本中,ES提供的Java客戶端被分別叫作:

  • Transport Client
  • Client Node

這裏的Transport Client與以前的傳輸客戶端是同一個東西。相比起ES2.2及以前的版本,在ES2.3和ES2.4版本中引入了一個專門用於路由請求的客戶端節點「Client Node」,暫且稱之爲「路由客戶端節點」。這個「路由客戶端節點」與以前的節點客戶端相似,須要加入ES集羣,可是其不能參與Master選舉,也不能保存數據,本質上來說它就是一個輕量級的「節點客戶端」。可是它不能獨立爲客戶端服務,而是經過「Transport Client」鏈接到「Client Node」。
「路由客戶端節點」的使用模式:
技術分享圖片

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>4.5.1</version>
</dependency>
/**
 * 啓動路由客戶端節點
 */
public static void startClientNode() {
    Node node = new NodeBuilder()
            .settings(Settings.settingsBuilder().put("node.master", false).put("node.data", false).put("path.home","D:\\elasticsearch-2.4.1"))
            .clusterName("elasticsearch")
            .node();
    node.start();
    System.out.println("Client Node Started");
}

/**
 * 傳輸客戶端與客戶端節點通訊
 * @throws UnknownHostException
 */
public static void transportClient() throws UnknownHostException {
    System.out.println("Do transport client");
    // 傳輸客戶端直接與"路由客戶端節點"通訊
    Client client = TransportClient.builder().build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301));

    // 添加文檔
    Map<String, Object> source = new HashMap<String, Object>();
    source.put("first_name", "John");
    source.put("last_name", "Smith");
    source.put("age", 25);
    source.put("about", "I love to go rock climbing");
    source.put("interests", Arrays.asList(new String[] {"sports","music"}));
    IndexRequest req = new IndexRequest().index("megacorp").type("employee")
            .id("1")
            .source(source);
    client.index(req);

    // on shutdown
    client.close();
}

(3)[v5.0,v6.1]:從ES5.0版本開始,到如今的6.1版本,ES提供的Java客戶端被統一爲以下2種:

  • Transport Client
  • Coordinating Only Node

從ES5.0版本開始,引入了一個新的協調節點:「Coordinating Only Node」,專門用於路由請求,分發批量索引操做。與「Client Node」相似,該協調節點不能參與master選舉,也不能保存數據。
技術分享圖片

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.1.1</version>
</dependency>
/**
 * 從5.0版本開始,傳輸客戶端的使用方式與以前不一樣(不一樣版本的ES Java API使用方式可能不一樣)
 * @throws UnknownHostException
 */
public static void transportClient() throws UnknownHostException {
    System.out.println("Do transport client");
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

    // 添加文檔
    Map<String, Object> source = new HashMap<String, Object>();
    source.put("first_name", "John");
    source.put("last_name", "Smith");
    source.put("age", 25);
    source.put("about", "I love to go rock climbing");
    source.put("interests", Arrays.asList(new String[] {"sports","music"}));
    IndexRequest req = new IndexRequest().index("megacorp").type("employee")
            .id("1")
            .source(source);
    client.index(req);
    
    // on shutdown
    client.close();
}

總結起來,關於ES提供的Java API,到目前(v6.1.1)爲止一共提供了4種客戶端:Transport Client,Node Client,Client Node,Coordinating Only Node,他們分別對應在不一樣的版本中使用。其中,Transport Client和Node Client均可以獨立使用,而Client Node和Coordinating Only Node都不能獨立提供查詢服務,他們做爲一個功能節點加入ES集羣,而後經過Transport Client進行鏈接。

注意:

  • 如上所說的Java API版本必須與集羣中ES節點版本一致,不然,它們可能互相沒法識別。
  • 根據ES官方的計劃,TransportClient客戶端將在7.0版本被標記爲廢棄,在8.0版本將完全被移除,官方建議使用Java High Level REST Client。

另外,從ES5.0版本開始,還提供了一個「Java REST Client」,這個客戶端能夠兼容全部的ES版本。而且,從ES5.6版本開始,這個「Java REST Client」又細分爲兩個版本:「Java Low Level REST Client」和「Java High Level REST Client」。
其中,「Java Low Level REST Client」能兼容全部ES版本;而「Java High Level REST Client」只能兼容主版本號與之相同的ES版本,而且ES的小版本號必須大於等於「Java High Level REST Client」的對應小版本號。舉個例子,6.0版本的「Java High Level REST Client」能夠兼容全部6.x版本的ES,可是6.1版本的「Java High Level REST Client」可能沒法兼容6.0版本的ES,由於ES的小版本號(0)小於「Java High Level REST Client」的小版本號(1)。關於更多「Java REST Client」信息,詳見:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.0/index.html

Java API雖然對Java程序員提供了必定的便利性,可是並不友好,對於非Java棧的應用來講就不能使用Java API。與之相對應的,RESTful風格的API就不能再這個限制,並且更加靈活,自由度更大。固然,ES官方提供了多種程序語言的客戶端,如:Groovy,JavaScript,.NET,PHP,Perl,Python以及Ruby。關於Java API的更多信息詳見:https://www.elastic.co/guide/en/elasticsearch/client/index.html

1.2 RESTful API

基於HTTP協議,以JSON爲數據交互格式的RESTful API。
其餘全部程序語言均可以使用RESTful API,經過9200端口的與ES進行通訊,可使用任何你喜歡的WEB客戶端。事實上,如你所見,你甚至能夠經過curl命令與ES通訊。

1.3 其餘交互方式

上面談到的ES交互方式都是ES官方提供的API或接口,基於這些API或接口還存在一些第三方組件,對於在應用開發或調試過程當中很是有用。
(1)spring-data-elasticsearch
該組件是Spring官方提供的一個與ES交互的組件庫,便於在基於Spring框架的應用程序中操做ES。
詳見:https://github.com/spring-projects/spring-data-elasticsearch

(2)ElasticSearch Query Builder
這是一個Chrome插件,在進行簡單的調試查詢時使用該插件很是便利和高效。
https://chrome.google.com/webstore/detail/elasticsearch-query-build/cioobgbmiehdijndbmjkbjlkdeijdibp?utm_source=chrome-ntp-icon

(3)ElasticSearch Head
Chrome插件,用於編寫DSL查詢,對於學習DSL查詢很是有幫助。
https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm

2. ES操做實踐

2.1 添加文檔

以下示例均已RESTful接口說明。
語法:PUT http://host:port/index/type/id,文檔內容使用json格式做爲http請求body。

curl -i -XPUT http://localhost:9200/megacorp/employee/1 -d ‘
{
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
        "sports",
        "music"
    ]
}
‘

2.2 搜索文檔

2.2.1 簡單搜索

  • 語法1:GET http://host:port/index/type/id,查詢指定id的文檔
curl -i -XGET http://localhost:9200/megacorp/employee/1

返回:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 205

{
  "_index": "megacorp",
  "_type": "employee",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "about": "I love to go rock climbing",
    "last_name": "Smith",
    "interests": [
      "sports",
      "music"
    ],
    "first_name": "John",
    "age": 25
  }
}
  • 語法2:GET http://host:port/index/type/_search,查詢所有文檔
curl -i -XGET http://localhost:9200/megacorp/employee/_search

返回:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 501

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "megacorp",
        "_type": "employee",
        "_id": "2",
        "_score": 1,
        "_source": {
          "about": "I like to collect rock albums",
          "last_name": "Smith",
          "interests": [
            "music"
          ],
          "first_name": "Jane",
          "age": 32
        }
      }
    ]
  }
}

2.2.2 查詢字符串搜索

語法:http://host:port/index/type/_search?q=querystring

示例:查詢姓氏爲Smith的員工

curl -i -XGET http://localhost:9200/megacorp/employee/_search?q=last_name:Smith

返回:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 522

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.30685282,
    "hits": [
      {
        "_index": "megacorp",
        "_type": "employee",
        "_id": "2",
        "_score": 0.30685282,
        "_source": {
          "about": "I like to collect rock albums",
          "last_name": "Smith",
          "interests": [
            "music"
          ],
          "first_name": "Jane",
          "age": 32
        }
      }
    ]
  }
}

2.2.3 使用DSL語句查詢

查詢字符串搜索便於經過命令行完成特定(ad hoc)的搜索,可是它也有侷限性。Elasticsearch提供豐富且靈活的查詢語言叫作DSL查詢(Query DSL),它容許你構建更加複雜、強大的查詢。DSL(Domain Specific Language特定領域語言)以JSON請求體的形式出現。咱們能夠這樣表示以前關於「Smith」的查詢:

curl -i -H ‘Content-Type: application/json‘ -XPOST http://localhost:9200/megacorp/employee/_search -d ‘
{
    "query": {
        "match": {
            "last_name": "Smith"
        }
    }
}
‘

這會返回與以前查詢相同的結果。你能夠看到有些東西改變了,咱們再也不使用查詢字符串作爲參數,而是使用請求體代替。這個請求體使用JSON表示,其中使用了match語句。
顯然,在DSL查詢中,須要傳遞消息體,因此只能使用POST方法。

五. 啓動報錯&解決方案

Q1. can not run elasticsearch as root

[root@localhost ~]# ./bin/elasticsearch
[2017-12-22T19:08:28,283][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] 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:125) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.1.1.jar:6.1.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:322) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.1.1.jar:6.1.1]
    ... 6 more

緣由: ES不能使用root用戶啓動
解決: 使用非ROOT用戶登陸並啓動ES便可

Q2: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed

[es@localhost]$ ./bin/elasticsearch
[2017-12-22T19:20:25,868][WARN ][o.e.b.JNANatives         ] unable to install syscall filter: 
java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed
    at org.elasticsearch.bootstrap.SystemCallFilter.linuxImpl(SystemCallFilter.java:341) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.SystemCallFilter.init(SystemCallFilter.java:616) ~[elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.JNANatives.tryInstallSystemCallFilter(JNANatives.java:258) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Natives.tryInstallSystemCallFilter(Natives.java:113) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:109) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:322) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) [elasticsearch-cli-6.1.1.jar:6.1.1]
    at org.elasticsearch.cli.Command.main(Command.java:90) [elasticsearch-cli-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) [elasticsearch-6.1.1.jar:6.1.1]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) [elasticsearch-6.1.1.jar:6.1.1]

當咱們使用非root用戶啓動ES時,在啓動日誌中看到一段異常日誌:CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed

緣由: seccomp是linux kernel從2.6.23版本開始所支持的一種安全機制,詳見:https://en.wikipedia.org/wiki/Seccomp
而個人主機操做系統是:CentOS release 6.8 (Final)

[root@localhost ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[root@localhost ~]# uname -a
Linux centosx64_tomcat1 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

雖然我使用的CentOS 6.8 Final使用的內核版本爲2.6.32-642,可是彷佛沒有支持seccomp。然而ES默認將利用內核的seccomp機制,因此報錯,詳見:https://github.com/elastic/elasticsearch/issues/22899

解決: ES是經過配置參數bootstrap.system_call_filter: true使用內核seccomp機制的,在開發環境下能夠將該參數值設爲false。

[es@localhost]$ vim config/elasticsearch.yml
bootstrap.system_call_filter: false # 默認該參數值不在elasticsearch.yml配置文件中,添加並設置爲false便可。

Q3:bootstrap checks failed
當修改了參數network.host: 0.0.0.0以後再啓動ES時將會報錯:

[es@localhost]$ ./bin/elasticsearch
[2017-12-22T23:16:23,511][INFO ][o.e.n.Node               ] initialized
[2017-12-22T23:16:23,515][INFO ][o.e.n.Node               ] [4t5PbHS] starting ...
[2017-12-22T23:16:24,421][INFO ][o.e.t.TransportService   ] [4t5PbHS] publish_address {192.168.80.133:9300}, bound_addresses {[::]:9300}
[2017-12-22T23:16:24,494][INFO ][o.e.b.BootstrapChecks    ] [4t5PbHS] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
[2017-12-22T23:16:24,509][ERROR][o.e.b.Bootstrap          ] [4t5PbHS] node validation exception
[3] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2017-12-22T23:16:24,524][INFO ][o.e.n.Node               ] [4t5PbHS] stopping ...
[2017-12-22T23:16:24,620][INFO ][o.e.n.Node               ] [4t5PbHS] stopped
[2017-12-22T23:16:24,620][INFO ][o.e.n.Node               ] [4t5PbHS] closing ...
[2017-12-22T23:16:24,672][INFO ][o.e.n.Node               ] [4t5PbHS] closed

緣由: 在啓動日誌中給出了很是清楚的提示:

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

詳見:https://serverfault.com/questions/681745/unable-to-change-vm-max-map-count-for-elasticsearch

解決: 以root用戶身份登陸

  1. 修改最大文件描述符數
    ```shell

    先查看一下當前的最大文件描述符數

    [root@localhost]# ulimit -n
    1024

修改最大文件描述符數

[root@localhost]# vim /etc/security/limits.conf

添加以下2行配置

es hard nofile 65536
es soft nofile 65536
```

  1. 修改最大線程數:非root用戶容許的最大線程數默認爲1024。

    [root@localhost]# vim /etc/security/limits.conf
    # 添加如2行配置
    es               hard    nproc           4096
    es               soft    nproc           4096

    上述2項修改完畢以後須要重啓系統。

  2. 修改vm.max_map_count:在虛擬機上運行ES時才須要修改這個值。
    ```shell

    先查看當前值

    [root@localhost]# cat /proc/sys/vm/max_map_count
    65530

修改

[root@localhost]# echo 262144 > /proc/sys/vm/max_map_count
```
該參數在修改以後重啓系統後又恢復爲默認值了,每次都須要從新設置。

總結

  1. ES基於Luence,可是使用上比Luence更加簡單,存儲文檔對象。
  2. ES天生就是分佈式的,易於擴展,具有良好的容錯性,很是適合用於存儲並檢索海量數據的場景,如構建日誌分析系統。

【參考】
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html Elasticsearch: 權威指南
https://www.gitbook.com/book/looly/elasticsearch-the-definitive-guide-cn/details es gitbook
https://www.gitbook.com/book/fuxiaopang/learnelasticsearch/details Elasticsearch 權威指南
http://itindex.net/detail/54168-elasticsearch-%E4%BC%98%E5%8C%96 億級規模的Elasticsearch優化實戰
https://www.elastic.co/guide/index.html Elastic Stack and Product Documentation

Elasticsearch入門實踐

標籤:sea   album   基於   調試   移除   unknown   pytho   內核版本   count   

原文地址:https://www.cnblogs.com/nuccch/p/8207147.html

相關文章
相關標籤/搜索