当前位置:   article > 正文

Elasticsearch怎么修改索引字段类型_elasticsearch 修改索引结构

elasticsearch 修改索引结构

由于ElasticSearch没有像数据库一样可以直接字段数据类型的方法,因此需要通过创建中间索引:data_index_1,备份数据到中间索引:data_index_1,然后删除原索引: data_index,重新创建正确数据类型索引:data_index,再把中间索引:data_index_1的数据备份到新创建索引:data_index。语句通过kibana的 dev_tools/console 执行。

索引mapping,可通过到Kibana查看索引的mapping,这是我们需要修改的字段rcvtime的类型

操作系统:CentOS7.4

ES版本:elasticsearch-7.16.3

kibana版本:kibana-7.16.3

操作步骤:

  1. 1.创建一个中间索引
  2. 2.向中间索引备份源索引的数据(mapping)
  3. 3.查询确认数据是否copy过去
  4. 4.删除有问题的索引
  5. 5.重新创建同名的索引(★字段类型修改正确★)
  6. 6.从中间索引还原到源索引的数据
  7. 7.删除中间索引

 参考脚本,在kibana执行:

  1. # 1. 创建一个中间索引
  2. # 创建索引和设置正确的字段Mapping
  3. PUT mail_center.received_1
  4. {
  5. "settings": {
  6. "index": {
  7. "max_result_window" : 100000000
  8. },
  9. "number_of_shards": 3,
  10. "number_of_replicas": 0
  11. },
  12. "mappings": {
  13. "properties": {
  14. "rcvtime": {
  15. "type": "date",
  16. "format": "yyyy-MM-dd HH:mm:ss"
  17. },
  18. "fetchtime": {
  19. "type": "date",
  20. "format": "yyyy-MM-dd HH:mm:ss"
  21. },
  22. "gmcrcvtime": {
  23. "type": "date",
  24. "format": "yyyy-MM-dd HH:mm:ss"
  25. }
  26. }
  27. }
  28. }
  29. # 2. 向中间索引备份源索引的数据
  30. # 重建索引
  31. POST _reindex
  32. {
  33. "source": {
  34. "index": "mail_center.received"
  35. },
  36. "dest": {
  37. "index": "mail_center.received_1"
  38. }
  39. }
  40. # 3.查询确认数据是否copy过去
  41. GET /mail_center.received/_doc/_search
  42. GET /mail_center.received_1/_doc/_search
  43. # 强制执行一次refresh
  44. POST /mail_center.received_1/_refresh
  45. # 4.删除源索引
  46. # 删除源索引
  47. DELETE mail_center.received
  48. # 5.重新创建同名的索引(★字段类型修改正确★)
  49. #创建索引和Mapping
  50. PUT mail_center.received
  51. {
  52. "settings": {
  53. "index": {
  54. "max_result_window" : 100000000
  55. },
  56. "number_of_shards": 3,
  57. "number_of_replicas": 0
  58. },
  59. "mappings": {
  60. "properties": {
  61. "rcvtime": {
  62. "type": "date",
  63. "format": "yyyy-MM-dd HH:mm:ss"
  64. },
  65. "fetchtime": {
  66. "type": "date",
  67. "format": "yyyy-MM-dd HH:mm:ss"
  68. },
  69. "gmcrcvtime": {
  70. "type": "date",
  71. "format": "yyyy-MM-dd HH:mm:ss"
  72. }
  73. }
  74. }
  75. }
  76. # 6. 从中间索引还原到源索引的数据
  77. # 重建索引
  78. POST _reindex
  79. {
  80. "source": {
  81. "index": "mail_center.received_1"
  82. },
  83. "dest": {
  84. "index": "mail_center.received"
  85. }
  86. }
  87. # 7. 删除中间索引
  88. DELETE mail_center.received_1

索引文档及数量可以通过kibana的index management查看

http://local.elasticsearch:5601/app/management/data/index_management/indices

如果显示不准确可以在kibana执行强制刷新 

POST /mail_center.received_1/_refresh

下图是修改后的字段mapping

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号