elasticsearch修改mapping重建索引

最近需要对elasticsearch中某个index的mapping进行修改,整理一下流程。

整个流程包括以下几个步骤:

  1. 修改index template
  2. 进行reindex
  3. 删除旧index
  4. 创建alias

修改index template

主要修改了dynamic templates中对string类型的修改,不进行索引,设置ignore_above等。

在reindex时出现错误max_bytes_length_exceeded_exception,这是由于Lucene字节长度限制引起的,添加ignore_above即可解决,参考http://www.jianshu.com/p/133a0f49311a

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
"order": 0,
"template": "datapt-buriedtool*",
"settings": {
"index": {
"number_of_shards": "6",
"number_of_replicas": "1",
"mapper": {
"dynamic": "true"
}
}
},
"mappings": {
"_default_": {
"_ttl": {
"default": "3d",
"enabled": true
},
"_source": {
"enabled": true
},
"dynamic_templates": [
{
"str": {
"mapping": {
"ignore_above": 10922,
"index": "not_analyzed",
"type": "string",
"doc_values": true
},
"match_mapping_type": "string"
}
},
{
"num": {
"mapping": {
"type": "long",
"doc_values": true
},
"match_mapping_type": "long"
}
},
{
"date": {
"mapping": {
"type": "date",
"doc_values": true
},
"match": "stamp*"
}
}
]
}
},
"aliases": {}
}

进行reindex

在reindex时,可以进行过滤,也有许多其他操作,具体参考https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html

注意curl超时时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
curl -m 10000000 -XPOST 'http://elasticsearch_ip:9200/_reindex' -d '{
"source" : {
"index" : "datapt-buriedtool",
"query" : {
"bool" : {
"filter" : {
"range" : {
"stamp": {
"gte" : "2016-05-11"
}
}
}
}
}
},
"dest" : {
"index" : "datapt-buriedtool_v1"
}
}'

创建alias

在创建alias之前先将旧的index删除。
alias操作参考https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

1
2
3
4
5
6
curl -XPOST 'http://elasticsearch_ip:9200/_aliases' -d '
{
"actions" : [
{ "add" : { "index" : "datapt-buriedtool_v1", "alias" : "datapt-buriedtool" } }
]
}'

至此完成。