白话Elasticsearch73_ES生产集群中的索引管理02
文章目錄
- 概述
- 官方指導(dǎo)
- 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 使用模板創(chuàng)建索引
- 4.5 模板的使用場(chǎng)景
概述
繼續(xù)跟中華石杉老師學(xué)習(xí)ES,第74篇
課程地址: https://www.roncoo.com/view/55
官方指導(dǎo)
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命令可以讓我們給一個(gè)已有的索引添加一個(gè)新的type,或者修改一個(gè)type,比如給某個(gè)type加一些字段
put mapping: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
下面這個(gè)命令是在創(chuàng)建索引的時(shí)候,直接跟著創(chuàng)建一個(gè)type
curl -XPUT 'http://elasticsearch02:9200/twitter?pretty' -d ' {"mappings": {"tweet": {"properties": {"message": {"type": "text"}}}} }'下面這個(gè)命令是給一個(gè)已有的索引添加一個(gè)type 。 7.x 已經(jīng)取消這個(gè)功能了,了解即可。
curl -XPUT 'http://elasticsearch02:9200/twitter/_mapping/user?pretty' -d ' {"properties": {"name": {"type": "text"}} }'下面這個(gè)命令是給一個(gè)已有的type添加一個(gè)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',上面這行命令可以查看某個(gè)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',這行命令可以看某個(gè)type的某個(gè)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管理是運(yùn)維中,索引管理中,很基礎(chǔ)的一塊
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" } }] }
上面是給某個(gè)index添加和刪除alias的命令,還有重命名alias的命令(先刪除再添加),包括將一個(gè)alias綁定多個(gè)index
POST /_aliases {"actions" : [{"add" : {"index" : "test1","alias" : "alias2","filter" : { "term" : { "user" : "kimchy" } }}}] } DELETE /logs_20162801/_alias/current_day GET /_alias/2016索引別名,還是挺有用的,主要是什么呢,就是說(shuō),可以將一個(gè)索引別名底層掛載多個(gè)索引,比如說(shuō)7天的數(shù)據(jù)
索引別名常常和之前講解的那個(gè)rollover結(jié)合起來(lái),我們?yōu)榱诵阅芎凸芾矸奖?#xff0c;每天的數(shù)據(jù)都rollover出來(lái)一個(gè)索引,但是在對(duì)數(shù)據(jù)分析的時(shí)候,可能是這樣子的,有一個(gè)索引access-log,指向了當(dāng)日最新的數(shù)據(jù),用來(lái)計(jì)算實(shí)時(shí)數(shù)據(jù)的; 有一個(gè)索引access-log-7days,指向了7天的7個(gè)索引,可以讓我們進(jìn)行一些周數(shù)據(jù)的統(tǒng)計(jì)和分析。
3、index settings管理
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#index-settings
3.1 Update index settings API
語(yǔ)法
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'經(jīng)常可能要對(duì)index做一些settings的調(diào)整,常常和之前的index open和close結(jié)合起來(lái)使用
4、index template
4.0 官方文檔
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html#index-templates
我們可以定義一些index template,這樣template會(huì)自動(dòng)應(yīng)用到根據(jù)匹配規(guī)則( based on an index pattern)匹配到的新創(chuàng)建的索引上去。
template中可以包含settings和mappings,還可以包含一個(gè)pattern,決定了template會(huì)被應(yīng)用到哪些index上。
而且template僅僅在index創(chuàng)建的時(shí)候會(huì)被應(yīng)用,修改template,是不會(huì)對(duì)已有的index產(chǎn)生影響的。
4.1 新建/更新模板
語(yǔ)法:
PUT /_template/<index-template>創(chuàng)建或者更新模板
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 使用模板創(chuàng)建索引
curl -XPUT 'http://elasticsearch02:9200/access-log-01?pretty'查看索引, 觀察模板是否被自動(dòng)的關(guān)聯(lián)到了匹配的模板上了。
curl -XGET 'http://elasticsearch02:9200/access-log-01?pretty' # 新建索引 匹配模板的index_patterns PUT test GET test/_mapping4.5 模板的使用場(chǎng)景
index template使用場(chǎng)景: 舉個(gè)例子你可能會(huì)經(jīng)常創(chuàng)建不同的索引,比如說(shuō)商品,分成了多種,每個(gè)商品種類的數(shù)據(jù)都很大,可能就是說(shuō),一個(gè)商品種類一個(gè)索引,但是每個(gè)商品索引的設(shè)置是差不多的,所以干脆可以搞一個(gè)商品索引模板,然后每次新建一個(gè)商品種類索引,直接綁定到模板,引用相關(guān)的設(shè)置。
簡(jiǎn)言之,將公共的東西抽取到模板中,省去了一遍一遍設(shè)置的麻煩。
總結(jié)
以上是生活随笔為你收集整理的白话Elasticsearch73_ES生产集群中的索引管理02的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 白话Elasticsearch73_ES
- 下一篇: Algorithms_二叉树二分搜索树初