白话Elasticsearch73_ES生产集群中的索引管理02
文章目錄
- 概述
- 官方指導
- 1、mapping管理
- 2、索引別名管理
- 3、index settings管理
- 3.1 Update index settings API
- 3.2 Get index settings API
- 4、index template
- 4.0 官方文檔
- 4.1 新建/更新模板
- 4.2 刪除模板
- 4.3 查看模板
- 4.4 使用模板創建索引
- 4.5 模板的使用場景
概述
繼續跟中華石杉老師學習ES,第74篇
課程地址: https://www.roncoo.com/view/55
官方指導
Index APIs: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html
1、mapping管理
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#mapping-management
put mapping命令可以讓我們給一個已有的索引添加一個新的type,或者修改一個type,比如給某個type加一些字段
put mapping: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
下面這個命令是在創建索引的時候,直接跟著創建一個type
curl -XPUT 'http://elasticsearch02:9200/twitter?pretty' -d ' {"mappings": {"tweet": {"properties": {"message": {"type": "text"}}}} }'下面這個命令是給一個已有的索引添加一個type 。 7.x 已經取消這個功能了,了解即可。
curl -XPUT 'http://elasticsearch02:9200/twitter/_mapping/user?pretty' -d ' {"properties": {"name": {"type": "text"}} }'下面這個命令是給一個已有的type添加一個field
curl -XPUT 'http://elasticsearch02:9200/twitter/_mapping/tweet?pretty' -d ' {"properties": {"user_name": {"type": "text"}} }'curl -XGET 'http://elasticsearch02:9200/twitter/_mapping/tweet?pretty',上面這行命令可以查看某個type的mapping映射信息
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
curl -XGET 'http://elasticsearch02:9200/twitter/_mapping/tweet/field/message?pretty',這行命令可以看某個type的某個field的映射信息
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
Type exists API https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-types-exists.html
mapping管理是運維中,索引管理中,很基礎的一塊
2、索引別名管理
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#alias-management
curl -XPOST 'http://elasticsearch02:9200/_aliases?pretty' -d ' {"actions" : [{ "add" : { "index" : "twitter", "alias" : "twitter_prod" } }] }' curl -XPOST 'http://elasticsearch02:9200/_aliases?pretty' -d ' {"actions" : [{ "remove" : { "index" : "twitter", "alias" : "twitter_prod" } }] }' POST /_aliases {"actions" : [{ "remove" : { "index" : "test1", "alias" : "alias1" } },{ "add" : { "index" : "test2", "alias" : "alias1" } }] } POST /_aliases {"actions" : [{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }] }
上面是給某個index添加和刪除alias的命令,還有重命名alias的命令(先刪除再添加),包括將一個alias綁定多個index
POST /_aliases {"actions" : [{"add" : {"index" : "test1","alias" : "alias2","filter" : { "term" : { "user" : "kimchy" } }}}] } DELETE /logs_20162801/_alias/current_day GET /_alias/2016索引別名,還是挺有用的,主要是什么呢,就是說,可以將一個索引別名底層掛載多個索引,比如說7天的數據
索引別名常常和之前講解的那個rollover結合起來,我們為了性能和管理方便,每天的數據都rollover出來一個索引,但是在對數據分析的時候,可能是這樣子的,有一個索引access-log,指向了當日最新的數據,用來計算實時數據的; 有一個索引access-log-7days,指向了7天的7個索引,可以讓我們進行一些周數據的統計和分析。
3、index settings管理
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#index-settings
3.1 Update index settings API
語法
PUT /<index>/_settings curl -XPUT 'http://elasticsearch02:9200/twitter/_settings?pretty' -d ' {"index" : {"number_of_replicas" : 1} }'3.2 Get index settings API
curl -XGET 'http://elasticsearch02:9200/twitter/_settings?pretty'經??赡芤獙ndex做一些settings的調整,常常和之前的index open和close結合起來使用
4、index template
4.0 官方文檔
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#index-templates
我們可以定義一些index template,這樣template會自動應用到根據匹配規則( based on an index pattern)匹配到的新創建的索引上去。
template中可以包含settings和mappings,還可以包含一個pattern,決定了template會被應用到哪些index上。
而且template僅僅在index創建的時候會被應用,修改template,是不會對已有的index產生影響的。
4.1 新建/更新模板
語法:
PUT /_template/<index-template>創建或者更新模板
curl -XPUT 'http://elasticsearch02:9200/_template/template_access_log?pretty' -d ' {"template": "access-log-*","settings": {"number_of_shards": 2},"mappings": {"log": {"_source": {"enabled": false},"properties": {"host_name": {"type": "keyword"},"created_at": {"type": "date","format": "EEE MMM dd HH:mm:ss Z YYYY"}}}},"aliases" : {"access-log" : {}} }'4.2 刪除模板
curl -XDELETE 'http://elasticsearch02:9200/_template/template_access_log?pretty' # 刪除模板 DELETE _template/template_1返回
{"acknowledged": true }4.3 查看模板
curl -XGET 'http://elasticsearch02:9200/_template/template_access_log?pretty' # 查看所有的模板 GET _template# 查看特定的模板 GET _template/template_14.4 使用模板創建索引
curl -XPUT 'http://elasticsearch02:9200/access-log-01?pretty'查看索引, 觀察模板是否被自動的關聯到了匹配的模板上了。
curl -XGET 'http://elasticsearch02:9200/access-log-01?pretty' # 新建索引 匹配模板的index_patterns PUT test GET test/_mapping4.5 模板的使用場景
index template使用場景: 舉個例子你可能會經常創建不同的索引,比如說商品,分成了多種,每個商品種類的數據都很大,可能就是說,一個商品種類一個索引,但是每個商品索引的設置是差不多的,所以干脆可以搞一個商品索引模板,然后每次新建一個商品種類索引,直接綁定到模板,引用相關的設置。
簡言之,將公共的東西抽取到模板中,省去了一遍一遍設置的麻煩。
總結
以上是生活随笔為你收集整理的白话Elasticsearch73_ES生产集群中的索引管理02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch73_ES
- 下一篇: MySQL-主从架构探索