關于Elastic Search安裝可以參考《Elastic Search 8.6.2集群安裝部署》及Kibana安裝可以參考《Elastic Search 8.6.2簡單操作》。相關命令將在Kibana工具的Console平臺上執行。
Elastic Search索引操作主要包含:創建、刪除、關閉和打開索引,以及索引別名的操作。其中,索引別名的操作在生產環境中使用比較廣泛,可以和關閉或刪除索引配合使用。在生產環境中使用索引時,都應該特別注意操作不當引起數據丟失或異常的問題。
1.創建索引
使用Elastic Search構建搜索引擎的第一步就是創建索引。創建索引以PUT方式發起請求,命令 PUT /indexName
PUT /customer { "settings":{ "number_of_shards": 5, "number_of_replicas": 2 }, "mAppings":{ "properties":{ "name":{ "type":"text" }, "age":{ "type": "integer" } } } } |
{ "acknowledged": true, "shards_acknowledged": true, "index": "customer" } |
2.刪除索引
刪除索引使用 DELETE /indexName
DELETE /customer |
{ "acknowledged": true } |
3.關閉索引
有些索引可能在暫時不使用,但未來可能還會使用時,就可以關閉該索引。索引關閉時,只能使用Elastic Search的Api或者監控工具來查看該索引的信息。此時對索引的讀寫操作都會報錯:索引關閉異常。
POST /customer/_close |
{ "acknowledged": true, "shards_acknowledged": true, "indices": { "customer": { "closed": true } } } |
4.打開索引
索引關閉了,想重新使用可以再次打開索引。
POST /customer/_open |
{ "acknowledged": true, "shards_acknowledged": true } |
5.索引別名
索引別名一般是通過一個別名關聯一個或多個索引,讓別名與索引之間建立邏輯關系,以地區、時間等劃分索引的場景會使用到。例如日志場景:
PUT /log-20230329 { "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "code": { "type": "keyword" }, "info": { "type": "text" } } } } |
POST /log-20230329/_doc/001 { "title": "日志29", "content":"內容29", "code": "0001", "info":"操作失敗" } |
PUT /log-20230328 |
POST /log-20230328/_doc/001 { "title": "日志28", "content":"內容28", "code": "0000", "info":"操作成功" } |
PUT /log-20230327 |
POST /log-20230327/_doc/001 { "title": "日志27", "content":"內容27", "code": "0000", "info":"操作成功" } |
創建索引別名
POST /_aliases { "actions": [ { "add":{ "index": "log-20230329", "alias": "last_three_day_log" } },{ "add":{ "index": "log-20230328", "alias": "last_three_day_log" } },{ "add":{ "index": "log-20230327", "alias": "last_three_day_log" } } ] } |
{ "acknowledged": true } |
這個時候查詢last_three_day_log就會查詢到關聯的三個索引:
GET /last_three_day_log/_search { "query":{ "match":{ "title":"日志" } } } |
{ "took": 6, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 0.5753642, "hits": [ { "_index": "log-20230327", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志27", "content": "內容27", "code": "0000", "info": "操作成功" } }, { "_index": "log-20230328", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志28", "content": "內容28", "code": "0000", "info": "操作成功" } }, { "_index": "log-20230329", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志29", "content": "內容29", "code": "0001", "info": "操作失敗" } } ] } } |
數據是不能直接寫到別名的索引,如果需要寫入數據則需要在創建別名時添加參數 is_write_index。
別名除了關聯多個索引實現組合查詢外,還可以用來替換索引。實現原理很簡單,例如:當天的日志已經生成了,現在近三天日志,應該將log-20230327刪除,重新添加log-20230330的索引,就實現了替換索引,先創建log-20230330索引:
PUT /log-20230330 { "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "code": { "type": "keyword" }, "info": { "type": "text" } } } } |
POST /log-20230330/_doc/001 { "title": "日志30", "content":"內容30", "code": "0000", "info":"操作成功" } |
再重新定義索引別名
POST /_aliases { "actions": [ { "add":{ "index": "log-20230330", "alias": "last_three_day_log" } },{ "add":{ "index": "log-20230329", "alias": "last_three_day_log" } },{ "add":{ "index": "log-20230328", "alias": "last_three_day_log" } },{ "remove":{ "index": "log-20230327", "alias": "last_three_day_log" } } ] } |
{ "acknowledged": true } |
重新查詢last_three_day_log就會發現沒有log-20230327索引的內容了:
GET /last_three_day_log/_search { "query":{ "match":{ "title":"日志" } } } |
{ "took": 11, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 0.5753642, "hits": [ { "_index": "log-20230328", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志28", "content": "內容28", "code": "0000", "info": "操作成功" } }, { "_index": "log-20230329", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志29", "content": "內容29", "code": "0001", "info": "操作失敗" } }, { "_index": "log-20230330", "_id": "001", "_score": 0.5753642, "_source": { "title": "日志30", "content": "內容30", "code": "0000", "info": "操作成功" } } ] } } |