ElasticSearch 介紹
===

---
# ELK 介紹
- Elasticsearch
- 基於Lucene搜索引擎的NoSQL資料庫
- Logstash
- 處理來自不同來源的數據,並儲存至你喜愛的儲存體
- Kibana
- 提供elasticsearch的儀錶板和進行數據可視化。

<!-- .slide: style="font-size: 24px;" -->
----
<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fpermalink.php%3Fstory_fbid%3D1385281961640196%26id%3D998741260294270&show_text=true&width=500" width="500" height="712" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"></iframe>
---
# ElasticSearch
---
<!-- .slide: style="font-size: 28px;" -->
1. [Lucene 全文檢索引擎](https://zh.wikipedia.org/wiki/Lucene)
2. 使用 Rest API
3. 使用 JSON 作為文檔資料序列化格式
4. Document Oriented
- Elasticsearch是文件導向的服務。
- 可以存儲整個物件(Object)或文檔(Document),提供文檔資料的處理、索引、搜索、排序、過濾。
6. Near RealTime (NRT) 微小延遲接近即時 (normally 1s)
7. 分散式架構
---
# 操作介面介紹
## (Command Line&GUI)
---
## Command Line
- 採用 linux curl 指令對 Elasticsearch REST API 進行操作
:::info
curl -X (http method GET、PUT、POST、DELETE) + HOST:PORT/COMMAND
curl -X (http method GET、PUT、POST、DELETE) + HOST:PORT/index/type/id
:::
<!-- .element: class="alert" style="font-size: 24px;" -->
---
## GUI 介面
- 透過kibana 畫面 對 Elasticsearch REST API 進行操作
```json=
GET {COMMAND}
```
---

---
## cat nodes API
```json=
GET _cat/nodes?v
```
---
## cat indices API
```json=
GET _cat/indices?v
```
---
# REST APIs
---
## Index APIs
---
## Create index
```json=
PUT my-index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
```
:::info
shards
- 將Index細分為多個分片分散儲存在多個節點。
- 提供水平拆分/縮放資料、分片分佈及平行化操作,以提高資料操作性能及吞吐量。
replica
- 為防止Index大量處理資料時,造成分片及節點異常後資料的損失。
- Elasticsearch提供一個或多個副本的索引分片,確保分片及節點發生故障時提供高可用性,並且支持在副本上並行搜索。
:::
<!-- .element: class="alert" style="font-size: 24px;" -->
---
## Get/ Delete Index
```json=
GET my-index
DELETE my-index
```
---
## Get Settings
```json=
GET my-index/_settings
```
---
查看多個索引的設置:
```json=
GET my-index,fisclog-*/_settings
```
---
## Update index (更新 settings)
```json=
PUT my-index/_settings
{
"index": {
"number_of_replicas": 2
}
}
```
---
## Document APIs
---
## Index API
索引API可以添加或者更新特定索引的文檔
新增檔案至my-index,並指定ID為1:
```json=
POST my-index/_doc/1
{
"user" : "yiwei",
"message" : "Hello Word!"
}
```
---
## GET API
通過文檔id從索引中獲取json格式的文檔
```json=
GET my-index/_doc/1
```
---
## Delete API
刪除API可以讓你刪除一個特定id的文檔
```json=
DELETE my-index/_doc/1
```
---
## Update API
Elasticsearch 非關聯式的 UPDATE 可以動態增加一個欄位或針對欄位更新,甚至可以刪除單一欄位,不需受任何限制。
```json=
POST my-index/_update/1
{
"doc":{
"age" : 27
}
}
```
---
## Multi-document APIs
---
## Bulk API
可以把多個action 操作放在單個bulk API中執行。提高索引速度。
bulk API 使用如下的JSON結構:
```json=
action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n
```
---
```json=
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
```
:::info
- 小技巧 可以在 bulk 前面放上index 名稱,這樣就不用在meta_data 加上 _index 名稱。
- 批量請求內容越大,其他的請求就會能獲得的記憶體就會越少。
- 批量請求大小最佳化,可以透過增加批量大小進行測試,當性能開始下降時就代表內容太大,一般來說建議值在5~15MB左右。
:::
<!-- .element: class="alert" style="font-size: 24px;" -->
---
## Index Lifecycle
1. Hot Node
- 因為會處理 Indexing 的請求,所以 JVM heap 會有一定比例拿來處理 Indexing。
2. Warm Node
- 不處理 Indexing 所以只會有一半 JVM heap 拿來處理 query request,剩下的會來給 Lucene 用作暫存。
3. Cold Node
- 不會處理 Indexing,而且針對 query request 所產生的 transient cache 也會一用完就盡快的釋放,減少 heap 的使用。
3. Delete
- 長時間不被使用的Index,可安全被刪除。
[(參考)喬叔教 Elastic - 10](https://ithelp.ithome.com.tw/articles/10243650)
<!-- .slide: style="font-size: 24px;" -->
---
## 建立 Lifecycle Policy
```
PUT /_ilm/policy/housekeeping_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
}
},
"warm": {
"min_age": "30h",
"actions": {
}
},
"cold": {
"min_age": "14d",
"actions": {
}
},
"delete": {
"min_age": "30d",
"actions": {
"delete": {
}
}
}
}
}
}
GET /_ilm/policy/housekeeping_policy?pretty=true
```
---
## Index 套用 Policy
建立 Index Template
```
PUT /_index_template/transaction_template
{
"index_patterns": [
"transaction_message-*",
"application_status_tree-*",
"application_log-*",
"batch_log-*"
],
"priority": 100,
"version": "00001",
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"index.lifecycle.name": "housekeeping_policy"
}
}
}
GET /_index_template?name=transaction_template&pretty=true
```
---
# 敬請指教
{"metaMigratedAt":"2023-06-15T23:48:41.312Z","metaMigratedFrom":"Content","title":"ElasticSearch 介紹","breaks":true,"contributors":"[{\"id\":\"be103b27-62d0-4db7-b9c6-0255f1d0f865\",\"add\":683,\"del\":333},{\"id\":\"565617f1-fa63-410c-9fb9-a9278632bb7a\",\"add\":10429,\"del\":5728}]"}