ES集群中的索引管理(2)

发布 : 2017-11-02 分类 : elasticsearch 浏览 :

mapping管理

1
put mapping命令可以让我们给一个已有的索引添加一个新的type,或者修改一个type,比如给某个type加一些字段

删除索引

1
curl -DELETE 'http://localhost:9201/twitter?pretty'

All text

创建索引的时候,创建一个type

1
2
3
4
5
6
7
8
9
10
11
12
curl -XPUT 'http://localhost:9201/twitter?pretty' -d ' 
{
"mappings": {
"tweet": {
"properties": {
"message": {
"type": "text"
}
}
}
}
}'

给一个已有的索引添加一个type

1
2
3
4
5
6
7
8
curl -XPUT 'http://localhost:9201/twitter/_mapping/user?pretty' -d ' 
{
"properties": {
"name": {
"type": "text"
}
}
}'

All text

下面这个命令是给一个已有的type添加一个field

1
2
3
4
5
6
7
8
curl -XPUT 'http://localhost:9201/twitter/_mapping/tweet?pretty' -d '
{
"properties": {
"user_name": {
"type": "text"
}
}
}'

All text

查看某个type的mapping映射信息

1
curl -XGET 'http://localhost:9201/twitter/_mapping/tweet?pretty'

All text

查看某个type的某个field的映射信息

1
curl -XGET 'http://localhost:9201/twitter/_mapping/tweet/field/message?pretty'

索引别名管理

1
2
3
4
5
6
curl -XPOST 'http://localhost:9201/_aliases?pretty' -d '
{
"actions" : [
{ "add" : { "index" : "twitter", "alias" : "twitter_prod" } }
]
}'

All text

1
2
3
4
5
6
curl -XPOST 'http://localhost:9201/_aliases?pretty' -d '
{
"actions" : [
{ "remove" : { "index" : "twitter", "alias" : "twitter_prod" } }
]
}'

All text

1
2
3
4
5
6
7
curl -XPOST 'http://localhost:9201/_aliases?' -d '
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}'
1
2
3
4
5
6
curl -XPOST 'http://localhost:9201/_aliases?' -d '
{
"actions" : [
{ "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
]
}'

上面是给某个index添加和删除alias的命令,还有重命名alias的命令(先删除再添加),包括将一个alias绑定多个index

1
2
3
4
5
6
7
8
9
10
11
12
curl -XPOST 'http://localhost:9201/_aliases?' -d '
{
"actions" : [
{
"add" : {
"index" : "test1",
"alias" : "alias2",
"filter" : { "term" : { "user" : "kimchy" } }
}
}
]
}'
1
curl -DELETE 'http://localhost:9201/logs_20162801/_alias/current_day'
1
curl -XGET 'http://localhost:9201/_alias/2016'
1
一个索引别名底层挂载多个索引,比如说7天的数据
1
2
3
为了性能和管理方便,每天的数据都rollover出来一个索引,但是在对数据分析的时候
有一个索引access-log,指向了当日最新的数据,用来计算实时数据的
有一个索引access-log-7days,指向了7天的7个索引,可以让我们进行一些周数据的统计和分析

index settings管理

1
2
3
4
5
6
curl -XPUT 'http://localhost:9201/twitter/_settings?pretty' -d '
{
"index" : {
"number_of_replicas" : 1
}
}'

All text

1
curl -XGET 'http://localhost:9201/twitter/_settings?pretty'

All text

index template管理

index template会自动应用到新创建的索引上去

1
template中可以包含settings和mapping、pattern,决定了template会被应用到哪些index上,template仅仅在index创建的时候会被应用,修改template,是不会对已有的index产生影响的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -XPUT 'http://localhost:9201/_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" : {}
}
}'

All text

1
curl -XDELETE 'http://localhost:9201/_template/template_access_log?pretty'

All text

1
curl -XGET 'http://localhost:9201/_template/template_access_log?pretty'

All text

1
curl -XPUT 'http://localhost:9201/access-log-01?pretty'

All text

1
curl -XGET 'http://localhost:9201/access-log-01?pretty'

All text

1
index template,是你可能会经常创建不同的索引,比如说商品,分成了多种,每个商品种类的数据都很大,一个商品种类一个索引,但是每个商品索引的设置是差不多的,所以干脆可以搞一个商品索引模板,然后每次新建一个商品种类索引,直接绑定到模板,引用相关的设置
本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/11/02/ES集群中的索引管理(2)/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹