本文基于 7.1.0 版本的 Elasticsearch 演示如何给已有的索引添加新字段,并对比添加新字段前后的数据情况。(以下 API 调用均在 Kibana 开发工具上操作)

创建索引 user_info_v220101:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PUT user_info_v220101 
{
"mappings": {
"properties": {
"user_id": {
"type": "integer"
},
"phone": {
"type": "keyword"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}

插入一条数据:

1
2
3
4
5
6
PUT user_info_v220101/_doc/2022001
{
"user_id": 2022001,
"phone": "17700009999",
"create_time": "2022-08-12 12:21:32"
}

查询索引数据:

1
2
3
4
5
6
GET user_info_v220101/_search
{
"query": {
"match_all": {}
}
}

该查询返回结果如下:

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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user_info_v220101",
"_type" : "_doc",
"_id" : "2022001",
"_score" : 1.0,
"_source" : {
"user_id" : 2022001,
"phone" : "17700009999",
"create_time" : "2022-08-12 12:21:32"
}
}
]
}
}

查询该索引 Mapping:

1
GET user_info_v220101/_mapping

返回结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"user_info_v220101" : {
"mappings" : {
"properties" : {
"create_time" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"phone" : {
"type" : "keyword"
},
"user_id" : {
"type" : "integer"
}
}
}
}
}

查询索引 settings:

1
GET user_info_v220101/_settings

返回结果如下:

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
{
"user_info_v220101" : {
"settings" : {
"index" : {
"refresh_interval" : "60s",
"number_of_shards" : "3",
"translog" : {
"flush_threshold_size" : "2gb",
"sync_interval" : "100s",
"durability" : "async"
},
"provided_name" : "user_info_v220101",
"merge" : {
"policy" : {
"max_merged_segment" : "1000mb"
}
},
"creation_date" : "1661421690275",
"number_of_replicas" : "1",
"uuid" : "UMFTwtCqTPaFJPacH6zSRg",
"version" : {
"created" : "7010099"
}
}
}
}
}

给索引添加字段(user_type_id, user_type_name):

1
2
3
4
5
6
7
8
9
10
11
GET user_info_v220101/_mapping
{
"properties": {
"user_type_id": {
"type": "integer"
},
"user_type_name": {
"type": "keyword"
}
}
}

重新查询索引 Mapping:

1
GET user_info_v220101/_mapping

返回结果如下:

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

{
"user_info_v220101" : {
"mappings" : {
"properties" : {
"create_time" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"phone" : {
"type" : "keyword"
},
"user_id" : {
"type" : "integer"
},
"user_type_id" : {
"type" : "integer"
},
"user_type_name" : {
"type" : "keyword"
}
}
}
}
}

可以看到,字段 user_type_id, user_type_name 已成功添加。

重新查询索引数据,返回结果如下,已有的文档没有新增的字段:

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
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user_info_v220101",
"_type" : "_doc",
"_id" : "2022001",
"_score" : 1.0,
"_source" : {
"user_id" : 2022001,
"phone" : "17700009999",
"create_time" : "2022-08-12 12:21:32"
}
}
]
}
}

插入一条新数据:

1
2
3
4
5
6
7
8
PUT user_info_v220101/_doc/2022002
{
"user_id": 2022002,
"phone": "17612345678",
"create_time": "2022-08-13 22:54:09",
"user_type_id": 2,
"user_type_name": "SVIP"
}

查询索引数据,结果如下:

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
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user_info_v220101",
"_type" : "_doc",
"_id" : "2022002",
"_score" : 1.0,
"_source" : {
"user_id" : 2022002,
"phone" : "17612345678",
"create_time" : "2022-08-13 22:54:09",
"user_type_id" : 2,
"user_type_name" : "SVIP"
}
},
{
"_index" : "user_info_v220101",
"_type" : "_doc",
"_id" : "2022001",
"_score" : 1.0,
"_source" : {
"user_id" : 2022001,
"phone" : "17700009999",
"create_time" : "2022-08-12 12:21:32"
}
}
]
}
}

给索引添加已经存在字段(user_type_id, user_type_name):

1
2
3
4
5
6
7
8
9
10
11
GET user_info_v220101/_mapping
{
"properties": {
"user_type_id": {
"type": "integer"
},
"user_type_name": {
"type": "keyword"
}
}
}

返回结果仍为:

1
2
3
{
"acknowledged" : true
}

并不会提示错误。

小结

  • 给索引添加新字段,原来的文档记录不会自动加上新增的字段;
  • 给索引添加已经存在的字段时,并不会出错。

(END)