Elasticsearch是著名的開源分佈式搜索和數據處理平臺,是一個基於Lucene的分佈式、實時、全文搜索系統,其穩定、可靠、高可用、可大規模擴展等特性,使得Elasticsearch的應用十分廣泛。特別是結合Logstash、Kibana形成的ELK套件,更是在日誌收集和可視化場景被大規模應用。
本文將從零開始,介紹Elasticsearch的核心概念、安裝和基本使用,目標是看完本文能夠快速入門Elasticsearch。
一、核心概念
索引(index)
一個索引是一些具有相似特徵的文檔的集合,例如一個用戶信息的索引,一個學生成績的索引,一個索引在Elasticsearch中由一個名字指定,名字由小寫字母組成。
類型(type)
在一個索引中,可以定義一種或者多種類型,類型指的是一個索引上的邏輯分類,一般來說會為一組具有共同字段的文檔定義類型,例如保存一個保存用戶數據的索引,為會員用戶創建一個類型,為普通用戶創建一個類型。類型在最新版的Elasticsearch 7.X版本中已經被去掉了。
文檔(document)
一個文檔是以可被Elasticsearch索引的基礎信息單元,文檔以通用的數據交換格式JSON表示,存儲於索引之中,理論上一個索引中可以存儲任意多的文檔。
分片(shards)
一個索引理論上可以存放任意多的文檔,但是實際情況下單臺服務器的容量有限,無法存放所有的數據。例如100億的文檔,單臺服務器存儲不下。為了解決這種情況,Elasticsearch提供了將一個索引的數據切分成多份存放到多個服務器的功能,每一份就是一個分片。在創建索引的時候可以指定分片的數量,默認會有5個分片。一般來說指定以後不能更改(更改的代價太大),索引需要提前進行容量的規劃。分片的設計一方面讓Elasticsearch具備了水平擴展的能力,另一方面多個分片可以並行提供查詢和索引服務,大大提高系統的性能。
複製(replicas)一個健壯的系統必須具備高可用性,複製就是Elasticsearch高可用性的體現。當某一個分片出現問題掉線的情況下,必須要有一個”備份”可以進行故障轉移,這個備份就是”複製”分片。Elasticsearch允許對某一個主分片創建多個複製分片,默認為1個複製分片。特別需要注意的是,複製分片不能與主分片在同一個節點,否則就失去了高可用的能力。
綜上,複製分片的作用:
- 提供Elasticsearch的高可用性
- 多個複製分片並行提供搜索功能,提升Elasticsearch的搜索能力。
集群(cluster)Elasticsearch集群由一個或者多個節點組成,共同承擔所有的數據存儲和搜索功能。集群由一個唯一的名字進行區分,默認為”elasticsearch”,集群中的節點通過整個唯一的名字加入集群。
節點(node)節點是Elasticsearch集群的一部分,每個節點也有一個唯一的名稱,只要多個節點在同個網絡中,節點就可以通過指定集群的名稱加入某個集群,與集群中的其他節點相互感知。
近實時(near real-time)
Elasticsearch從存儲文檔到文檔可以被索引查詢會存在短暫的延時,延時時間一般在1秒以內,所以是近實時的。
二、安裝
2.1 下載Elasticsearch安裝包
下載Elasticsearch安裝包,並解壓縮,本文以6.5.2版本為例。
<code>$ wgethttps:
//artifacts.elastic.co/downloads
/elasticsearch/elasticsearch
-6.5
.2
.tar.gz $ tar -zxvf elasticsearch-6.5
.2
.tar.gz/<code>
2.2 修改配置文件(可選)
修改配置文件是可選的一個步驟,只是為了向大家展示Elasticsearch一些基本的配置項,大家可以選擇性配置,亦可以跳過使用默認配置。
<code>cluster.name:
my-application
node.name:
node-1
node.attr.rack:
r1
path.data:
/home/elastic/data
path.logs:
/home/elastic/logs
network.host:
192.168
.56
.3
http.port:
9200
xpack.security.enabled:
false
http.cors.enabled:
true
http.cors.allow-origin:
"*"
/<code>
2.3 修改系統參數
2.3.1 修改最大文件描述符和最大線程數配置
切換到root用戶,修改/etc/security/limits.conf配置文件,添加以下內容並保存。
<code>#添加如下內容並保存
*
soft nofile 65536*
hard nofile 131072*
soft nproc 4096*
hard nproc 4096/<code>
以上的配置是因為ElasticSearch的運行對最大的文件描述符以及最大線程數有要求,默認值4096和2048太小了,若無以上配置,啟動過程中會報如下錯誤。
<code>max
file
descriptors
[4096]
for
elasticsearch
process
is
too
low
,increase
to
at
least
[65536]
[1]
:max
number
of
threads
[2048]
for
user
[elastic]
is
too
low
,increase
to
at
least
[4096]
/<code>
2.3.2 修改max_map_count參數打開/etc/sysctl.conf配置文件,添加如下內容並保存,執行sysctl -p命令生效。
<code>vm.max_map_count
=262144
/<code>
以上的配置也是因為Elasticsearch對次參數有要求,設置太小啟動將會報如下錯誤
<code>max
virtual
memory
areas
vm
.max_map_count
[65530]
is
too
low
,increase
to
at
least
[262144]
/<code>
2.3.3 關閉系統防火牆(可選)
<code>$ systemctl stop firewalld.service $ systemctl status firewalld.service/<code>
2.4 啟動Elasticsearch
經過以上配置,就可以執行以下命令啟動Elasticsearch,進入Elasticsearch根目錄,執行以下命令
<code>$ bin/elasticsearch/<code>
如果看到以下日誌,代表已經正常啟動
<code>[2019-01-13T08:41:29,796
][INFO
][c.f.s.h.SearchGuardHttpServerTransport
] [node-1] publish_address {10.0.2.15:9200}, bound_
addresses {[::]:9200} [2019-01-13T08:41:29,796
][INFO
][o.e.n.Node
] [node-1] started/<code>
2.5 驗證Elasticsearch
使用curl命令或者執行在瀏覽器輸入如下URL,若有正常輸出Elasticsearch集群信息,證明已經正常運行。
<code>$ curl http: 或者 $ curl http:/<code>
<code>{"name"
:"node-1"
,"cluster_name"
:"my-application"
,"cluster_uuid"
:"C2ILS_NVRM-S-JPFFsHhUg"
,"version"
: {"number"
:"6.5.2"
,"build_flavor"
:"default"
,"build_type"
:"zip"
,"build_hash"
:"424e937"
,"build_date"
:"2018-06-11T23:38:03.357887Z"
,"build_snapshot"
:false
,"lucene_version"
:"7.3.1"
,"minimum_wire_compatibility_version"
:"5.6.0"
,"minimum_index_compatibility_version"
:"5.0.0"
},"tagline"
:"You Know, for Search"
}/<code>
三、索引的操作
Elasticsearch提供一整套的RESTful的API用以支持各種索引、文檔、搜索等操作。這裡我們簡單以索引的創建、查詢和刪除為例子來了解如何操作Elasticsearch的索引。
3.1 新建索引
使用HTTP PUT方法可以新建一個索引,如下創建一個名字為customer的索引,pretty參數表示response以方便讀取的JSON格式返回。
<code>$ curl -X PUT"localhost:9200/customer?pretty"
/<code>
返回值如下,表示索引已經創建成功。
<code>{"acknowledged"
:true
,"shards_acknowledged"
:true
}/<code>
3.2 查詢索引
<code>$
curl
http://localhost:9200/_cat/indices?v
health
status
index
uuid
pri
rep
docs.count
docs.deleted
store.size
pri.store.size
yellow
open
customer
IpQSnki7S1eQfYH6BgDd0Q
5
1
2
0
7.
7kb
7.
7kb
/<code>
使用HTTP GET方法訪問_cat API的indices接口可以查詢到上一步中創建的索引,可以看到customer索引有5個primary分片,有1個複製分片。
3.3 刪除索引
使用HTTP DELETE方法可以刪除一個索引。
<code>$ curl -X DELETE"localhost:9200/customer?pretty"
/<code>
返回值如下,代表已經刪除成功,也可以重新使用索引查詢方法進行查詢,可以發現索引已被刪除。
<code>{"acknowledged"
:true
}/<code>
四、文檔的操作
Elasticsearch對於文檔的操作也包括了一系列的_doc API,我們這裡同樣使用Document的增刪改查為例來講解如何操作Document(嘗試這一步之前前面創建的索引不能刪除)
4.1 創建一個Document
使用HTTP PUT方法可以新增一個Document,如下指定Document ID為1創建Document
<code>$ curl -X PUT"localhost:9200/customer/_doc/1?pretty"
-H'Content-Type: application/json'
-d' { "name": "Liu Jintao" } '
/<code>
返回值如下,代表已經成功創建了文檔。
<code>{"_index"
:"customer"
,"_type"
:"_doc"
,"_id"
:"1"
,"_version"
:2
,"result"
:"updated"
,"_shards"
: {"total"
:2
,"successful"
:1
,"failed"
:0
},"_seq_no"
:1
,"_primary_term"
:2
}/<code>
4.2 查詢一個Document
使用HTTP GET方法,通過Document ID查詢Document
<code>$ curl -X GET"localhost:9200/customer/_doc/1?pretty"
/<code>
返回值如下,可以發現實際存儲的內容放在了source字段。
<code>{"_index"
:"customer"
,"_type"
:"_doc"
,"_id"
:"1"
,"_version"
:2
,"found"
:true
,"_source"
: {"name"
:"Liu Jintao"
} }/<code>
4.3 修改一個Document
使用HTTP POST請求修改一個Document,例如將上面創建的Document name的值改為“Test Name”
<code>$ curl -X POST"localhost:9200/customer/_doc/1/_update?pretty"
-H'Content
-Type: application/json' -d' {"doc"
: {"name"
:"Test Name"
} } '/<code>
返回值如下,可以發現_version字段的值已經改變了,證明我們的更新成功了,也可以使用查詢API重新查詢確認。
<code>{"_index"
:"customer"
,"_type"
:"_doc"
,"_id"
:"1"
,"_version"
:3
,"result"
:"updated"
,"_shards"
: {"total"
:2
,"successful"
:1
,"failed"
:0
},"_seq_no"
:2
,"_primary_term"
:2
}/<code>
4.4 刪除一個Document
使用HTTP DELETE方法可以刪除一個Document
<code>$ curl -X DELETE"localhost:9200/customer/_doc/1?pretty"
/<code>
返回值如下代表刪除成功
<code>{"_index"
:"customer"
,"_type"
:"_doc"
,"_id"
:"1"
,"_version"
:4
,"result"
:"deleted"
,"_shards"
: {"total"
:2
,"successful"
:1
,"failed"
:0
},"_seq_no"
:3
,"_primary_term"
:2
}/<code>
五、數據搜索
數據搜索是Elasticsearch的重頭戲,與Document、Index一樣,Elasticsearch有一套專門的_search API來支持搜索功能。數據搜索基本使用HTTP GET方法,一般有兩種方式:
- 使用Request URI,將查詢參數放到URI上
- 使用Request Body,將查詢參數放到Request Body下(推薦)
方式一
使用_search API,q=*代表查詢customer索引下所有的Document
<code>curl
-X GET"localhost:9200/customer/_search?q=*&pretty"
/<code>
返回值如下,_shards.total代表總共5個分片,_shards.successful為5代表5個分片全部成功進行了查詢,hits段代表查詢的結果,hits.total為1代表符合條件的Document數量為1。
<code>{"took"
:17
,"timed_out"
:false
,"_shards"
: {"total"
:5
,"successful"
:5
,"skipped"
:0
,"failed"
:0
},"hits"
: {"total"
:1
,"max_score"
:1.0
,"hits"
: [ {"_index"
:"customer"
,"_type"
:"_doc"
,"_id"
:"2"
,"_score"
:1.0
,"_source"
: {"name"
:"Liu jintao"
} } ] } }/<code>
方式二
使用Request Body的方式進行查詢,使用query參數,匹配條件為match_all,結果與上一步的查詢結果一致,不再贅述。這種查詢方式使用的是Elasticsearch的 query DSL語法,後續文章會詳細講解這種語法。
<code>$ curl -X GET"localhost:9200/bank/_search?pretty"
-H'Content-Type: application/json'
-d' { "query": { "match_all": {} } } '
/<code>