ElasticSearch-update-mapping
2025-01-22 08:19:30    973 字   
This post is also available in English and alternative languages.

ElasticSearch版本:6.5.0(点击跳转官方文档)


1. ElasticSearch 更新 mapping

已存在的索引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
26
27
PUT /my_test_index
{
"settings": {
"number_of_shards": 4
},
"mappings": {
"my_test_type": {
"properties": {
"goodsCmdCode": {
"type": "keyword"
},
"goodsPriceUrl": {
"type": "keyword"
},
"goodsTitle": {
"type": "text"
},
"userId": {
"type": "keyword"
},
"userPrice": {
"type": "integer"
}
}
}
}
}

插入测试数据

1
2
3
4
5
6
7
8
9
10
11
POST /my_test_index/my_test_type/_bulk
{"index":{"_id":1}}
{"goodsCmdCode":"000001","goodsPriceUrl":"https://baidu.com","goodsTitle":"商品1","userId":"987987","userPrice":12}
{"index":{"_id":2}}
{"goodsCmdCode":"000002","goodsPriceUrl":"https://baidu.com","goodsTitle":"商品2","userId":"876876","userPrice":14}
{"index":{"_id":3}}
{"goodsCmdCode":"000003","goodsPriceUrl":"https://baidu.com","goodsTitle":"商品3","userId":"765765","userPrice":19}
{"index":{"_id":4}}
{"goodsCmdCode":"000004","goodsPriceUrl":"https://baidu.com","goodsTitle":"商品4","userId":"654654","userPrice":11}
{"index":{"_id":5}}
{"goodsCmdCode":"000005","goodsPriceUrl":"https://baidu.com","goodsTitle":"商品5","userId":"543543","userPrice":9}

按照两篇文章(参考资料)内容的意思,先更新mapping,添加指定字段,并设置字段类型;然后通过’update_by_query’,对字段值统一进行初始化。

假设要添加一个keyword字段:goodsStatus。

  1. 添加mapping中字段类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    PUT /my_test_index/_mapping/my_test_type
    {
    "properties": {
    "goodsStatus":{
    "type":"keyword",
    "null_value": "NULL"
    }
    }
    }

    通过’GET /my_test_index/_mapping’命令,可以确认mapping新增字段成功。

  2. 通过’update_by_query’API,更新所有数据,对新增字段进行初始化

    1
    2
    3
    4
    5
    6
    7
    POST /my_test_index/_update_by_query
    {
    "script": {
    "lang": "painless",
    "inline": "if (ctx._source.goodsStatus == null) {ctx._source.goodsStatus='01'}"
    }
    }

    通过查询命令,发现更新成功了,新增goodsStatus字段,并对其进行初始化。

    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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    {
    "took" : 13,
    "timed_out" : false,
    "_shards" : {
    "total" : 4,
    "successful" : 4,
    "skipped" : 0,
    "failed" : 0
    },
    "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
    {
    "_index" : "my_test_index",
    "_type" : "my_test_type",
    "_id" : "3",
    "_score" : 1.0,
    "_source" : {
    "goodsCmdCode" : "000003",
    "goodsStatus" : "01",
    "goodsPriceUrl" : "https://baidu.com",
    "userPrice" : 19,
    "goodsTitle" : "商品3",
    "userId" : "765765"
    }
    },
    {
    "_index" : "my_test_index",
    "_type" : "my_test_type",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
    "goodsCmdCode" : "000001",
    "goodsStatus" : "01",
    "goodsPriceUrl" : "https://baidu.com",
    "userPrice" : 12,
    "goodsTitle" : "商品1",
    "userId" : "987987"
    }
    },
    {
    "_index" : "my_test_index",
    "_type" : "my_test_type",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
    "goodsCmdCode" : "000002",
    "goodsStatus" : "01",
    "goodsPriceUrl" : "https://baidu.com",
    "userPrice" : 14,
    "goodsTitle" : "商品2",
    "userId" : "876876"
    }
    },
    {
    "_index" : "my_test_index",
    "_type" : "my_test_type",
    "_id" : "4",
    "_score" : 1.0,
    "_source" : {
    "goodsCmdCode" : "000004",
    "goodsStatus" : "01",
    "goodsPriceUrl" : "https://baidu.com",
    "userPrice" : 11,
    "goodsTitle" : "商品4",
    "userId" : "654654"
    }
    },
    {
    "_index" : "my_test_index",
    "_type" : "my_test_type",
    "_id" : "5",
    "_score" : 1.0,
    "_source" : {
    "goodsCmdCode" : "000005",
    "goodsStatus" : "01",
    "goodsPriceUrl" : "https://baidu.com",
    "userPrice" : 9,
    "goodsTitle" : "商品5",
    "userId" : "543543"
    }
    }
    ]
    }
    }

2. Reference