## 一:簡介
ElasticSearch是一個基于Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發的,并作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用于云計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。結合logstash,kibana使用,可以用于大型分布式系統數據分析,展現。
官網:https://www.elastic.co/
## 二:ElasticSearch的特性:
1.開源的(不解釋)
2.它是分布式和高度可用的搜索引擎。
2.1每個索引根據配置分片的數量完成完全分片。
2.2每個碎片都可以有一個或多個副本。
2.3讀/搜索操作可以在任何復制分片上執行。
3.支持多租戶與多種類型。
3.1支持一個以上的索引。
3.2支持一個以上的每個索引類型。
3.3指數級配置(數量的碎片,索引存儲,…)。
4.各種組api
4.1HTTP RESTful API
4.2本地Java API。
4.3所有api執行自動從路由節點操作。
5.面向文檔的
6.可靠異步寫操作長期持續。
7.(附近)實時搜索。
8.基于Lucene
8.1每個碎片都是一個功能齊全的Lucene索引
8.2所有Lucene通過簡單的配置/插件非常容易的暴露出來。
9.每個操作的一致性 ,單文檔級別操作都是原子的、一致的、孤立的和持久的。
## 三:安裝elasticsearch單點
### 3.1 下載
官方下載地址:https://www.elastic.co/downloads/elasticsearch
```
wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.1/elasticsearch-2.4.1.tar.gz;
```
### 3.2 單點運行
```
nohup ./bin/elasticsearch &
```
### 3.3 測試
```
~/elk/elasticsearch-2.4.0$ curl -X GET http://localhost:9200/
{
"name" : "Hack",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "2.4.0",
"build_hash" : "ce9f0c7394dee074091dd1bc4e9469251181fc55",
"build_timestamp" : "2016-08-29T09:14:17Z",
"build_snapshot" : false,
"lucene_version" : "5.5.2"
},
"tagline" : "You Know, for Search"
}
```
當看到類似以上的返回結果,表示elasticsearch單點安裝成功!
### 3.4 目錄結構

* bin:可以執行文件
* config:配置文件
* data:es存放數據的文件
* lib:運行所需類庫
* logs:日志文件
* modules:加載模塊列表(其實是必要插件)
* plugins:插件文件(需要自定義安裝)
> 剛解壓的ES并沒有這么多目錄,只有bin、config、lib、modules,其他都是在第一次運行之后自動生成的。
## 四:集群的搭建
elasticsearch集群的搭建特別的簡單:
1. 在集群的每個節點上,將elasticsearch的單點安裝好
2. 修改配置文件elasticsearch.yml的cluster.name(集群名稱)配置,要求所有節點配置一致
3. 修改配置文件elasticsearch.yml的http.port為9200,所有節點配置統一
> 只要集群名相同,且機器處于同一局域網同一網段,es會自動去發現其他的節點
## 五:elasticsearch.yml中文版
```
# ======================== Elasticsearch Configuration =========================
#
# 注意:大部分的Elasticsearch默認配置都是相對合理的.在你著手調整和優化配置,確保你
#了解什么是你想實現的以及后果是什么。
# 修改節點配置的主要方式就是通過本文件,下面的模板列出了最重要的一些設置,你可以通過它配置一個生產環境集群
# 關于更多的配置項及詳細信息請參考文檔:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration.html>
#
# ---------------------------------- 集群配置 -----------------------------------
# 集群的名稱描述,集群內統一:
cluster.name: aexit-elastic
#
# ------------------------------------節點配置 ------------------------------------
#
# 本節點的名稱描述:
node.name: node-1
#
# 為節點添加自定義的屬性:
# node.rack: r1
#
# ----------------------------------- 路徑 ------------------------------------
#
# 數據存儲路徑 (多個路徑用逗號分割):
# path.data: /path/to/data
#
# 日志文件路徑:
# path.logs: /path/to/logs
#
# ----------------------------------- 內存 -----------------------------------
#
# 在啟動過程中是否為內存加鎖:
# bootstrap.memory_lock: true
#
# 請保證 `ES_HEAP_SIZE` 環境變量的設置大約為系統可用內存的一半
# and that the owner of the process is allowed to use this limit.
#
# 當發生swapping the memory, Elasticsearch 的執行效率很差.
#
# ---------------------------------- 網絡 -----------------------------------
#
# 設置綁定的本機ip地址 (IPv4 or IPv6):
network.host: 192.168.1.158
#
# 自定義HTTP端口:
http.port: 9200
#
# 關于更多的信息請參考:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#
# --------------------------------- 服務發現 ----------------------------------
#
# 當新節點加入的時候,配置一個初始化主機列表用于節點發現
# 默認的主機列表是 ["127.0.0.1", "[::1]"]
discovery.zen.ping.unicast.hosts: ["192.168.1.158", "192.168.1.60", "192.168.1.24"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of nodes / 2 + 1):
# 為了避免腦裂的發生,使用如下配置(數值為節點總數/2 + 1):
discovery.zen.minimum_master_nodes: 2
#
# 關于更多的信息請參考:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery.html>
#
# ---------------------------------- 網關 -----------------------------------
#
# 如果集群發生重啟,直到N個節點啟動完成,才能開始進行集群初始化恢復動作
gateway.recover_after_nodes: 3
#
# 關于更多的信息請參考:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html>
#
# ---------------------------------- 其他 -----------------------------------
#
# 禁止在一個操作系統啟動多個節點:
node.max_local_storage_nodes: 1
#
# 刪除索引時,需要明確的名稱
# action.destructive_requires_name: true
```
## 參考:
http://blog.csdn.net/jiuqiyuliang/article/details/51245335
* 本文沒有涉及到的elasticsearch head插件的安裝(單點即可)
http://blog.csdn.net/jiuqiyuliang/article/details/51240800