文章转载自:https://www.cnblogs.com/uglyliu/p/12331964.html

昨天研发说在kibana中统计userid字段不出图,后来查到该字段显示冲突了,然后再查看了GET test/_mapping下该索引的mapping,发现userid是long类型的,而userid.keyword是string类型的,出现这种情况的根本原因是日志中这个字段存的是数值类型的值,改成字符串类型即可,由于急着用,我司上线一般是下午6点30上线,所以临时修改了下该字段的类型,步骤如下:

整体步骤流程:

1.先获取索引的mapping,修改成适合的字段类型

2.然后创建一个自定义mapping的新索引

3.把旧索引的数据reindex到新索引上(旧索引先停止新数据的写入)

4.删除旧索引

5.按照步骤2创建test索引

6.把test-new索引的数据reindex到test索引上

1、查看旧索引的mapping

  1. GET test/_mapping
  2. 找到userid这个字段,修改类型为keyword,如下:
  3. {
  4. "mappings": {
  5. "doc": {
  6. "properties": {
  7. "@timestamp": {
  8. "type": "date"
  9. },
  10. "@version": {
  11. "type": "text",
  12. "fields": {
  13. "keyword": {
  14. "type": "keyword",
  15. "ignore_above": 256
  16. }
  17. }
  18. },
  19. "beat": {
  20. "properties": {
  21. "hostname": {
  22. "type": "text",
  23. "fields": {
  24. "keyword": {
  25. "type": "keyword",
  26. "ignore_above": 256
  27. }
  28. }
  29. },
  30. "name": {
  31. "type": "text",
  32. "fields": {
  33. "keyword": {
  34. "type": "keyword",
  35. "ignore_above": 256
  36. }
  37. }
  38. },
  39. "version": {
  40. "type": "text",
  41. "fields": {
  42. "keyword": {
  43. "type": "keyword",
  44. "ignore_above": 256
  45. }
  46. }
  47. }
  48. }
  49. },
  50. "code": {
  51. "type": "long"
  52. },
  53. "dip": {
  54. "type": "text",
  55. "fields": {
  56. "keyword": {
  57. "type": "keyword",
  58. "ignore_above": 256
  59. }
  60. }
  61. },
  62. "fields": {
  63. "properties": {
  64. "log_topic": {
  65. "type": "text",
  66. "fields": {
  67. "keyword": {
  68. "type": "keyword",
  69. "ignore_above": 256
  70. }
  71. }
  72. }
  73. }
  74. },
  75. "host": {
  76. "type": "text",
  77. "fields": {
  78. "keyword": {
  79. "type": "keyword",
  80. "ignore_above": 256
  81. }
  82. }
  83. },
  84. "message": {
  85. "type": "text",
  86. "fields": {
  87. "keyword": {
  88. "type": "keyword",
  89. "ignore_above": 256
  90. }
  91. }
  92. },
  93. "method": {
  94. "type": "text",
  95. "fields": {
  96. "keyword": {
  97. "type": "keyword",
  98. "ignore_above": 256
  99. }
  100. }
  101. },
  102. "name": {
  103. "type": "text",
  104. "fields": {
  105. "keyword": {
  106. "type": "keyword",
  107. "ignore_above": 256
  108. }
  109. }
  110. },
  111. "offset": {
  112. "type": "long"
  113. },
  114. "referer": {
  115. "type": "text",
  116. "fields": {
  117. "keyword": {
  118. "type": "keyword",
  119. "ignore_above": 256
  120. }
  121. }
  122. },
  123. "sip": {
  124. "type": "text",
  125. "fields": {
  126. "keyword": {
  127. "type": "keyword",
  128. "ignore_above": 256
  129. }
  130. }
  131. },
  132. "source": {
  133. "type": "text",
  134. "fields": {
  135. "keyword": {
  136. "type": "keyword",
  137. "ignore_above": 256
  138. }
  139. }
  140. },
  141. "tags": {
  142. "type": "text",
  143. "fields": {
  144. "keyword": {
  145. "type": "keyword",
  146. "ignore_above": 256
  147. }
  148. }
  149. },
  150. "time": {
  151. "type": "text",
  152. "fields": {
  153. "keyword": {
  154. "type": "keyword",
  155. "ignore_above": 256
  156. }
  157. }
  158. },
  159. "url": {
  160. "type": "text",
  161. "fields": {
  162. "keyword": {
  163. "type": "keyword",
  164. "ignore_above": 256
  165. }
  166. }
  167. },
  168. "userid": {
  169. "type": "keyword" #修改此处
  170. }
  171. }
  172. }
  173. }
  174. }

2、创建一个自定义mapping的新索引

  1. PUT test-new
  2. {
  3. "mappings": {
  4. "doc": {
  5. "properties": {
  6. "@timestamp": {
  7. "type": "date"
  8. },
  9. "@version": {
  10. "type": "text",
  11. "fields": {
  12. "keyword": {
  13. "type": "keyword",
  14. "ignore_above": 256
  15. }
  16. }
  17. },
  18. "beat": {
  19. "properties": {
  20. "hostname": {
  21. "type": "text",
  22. "fields": {
  23. "keyword": {
  24. "type": "keyword",
  25. "ignore_above": 256
  26. }
  27. }
  28. },
  29. "name": {
  30. "type": "text",
  31. "fields": {
  32. "keyword": {
  33. "type": "keyword",
  34. "ignore_above": 256
  35. }
  36. }
  37. },
  38. "version": {
  39. "type": "text",
  40. "fields": {
  41. "keyword": {
  42. "type": "keyword",
  43. "ignore_above": 256
  44. }
  45. }
  46. }
  47. }
  48. },
  49. "code": {
  50. "type": "long"
  51. },
  52. "dip": {
  53. "type": "text",
  54. "fields": {
  55. "keyword": {
  56. "type": "keyword",
  57. "ignore_above": 256
  58. }
  59. }
  60. },
  61. "fields": {
  62. "properties": {
  63. "log_topic": {
  64. "type": "text",
  65. "fields": {
  66. "keyword": {
  67. "type": "keyword",
  68. "ignore_above": 256
  69. }
  70. }
  71. }
  72. }
  73. },
  74. "host": {
  75. "type": "text",
  76. "fields": {
  77. "keyword": {
  78. "type": "keyword",
  79. "ignore_above": 256
  80. }
  81. }
  82. },
  83. "message": {
  84. "type": "text",
  85. "fields": {
  86. "keyword": {
  87. "type": "keyword",
  88. "ignore_above": 256
  89. }
  90. }
  91. },
  92. "method": {
  93. "type": "text",
  94. "fields": {
  95. "keyword": {
  96. "type": "keyword",
  97. "ignore_above": 256
  98. }
  99. }
  100. },
  101. "name": {
  102. "type": "text",
  103. "fields": {
  104. "keyword": {
  105. "type": "keyword",
  106. "ignore_above": 256
  107. }
  108. }
  109. },
  110. "offset": {
  111. "type": "long"
  112. },
  113. "referer": {
  114. "type": "text",
  115. "fields": {
  116. "keyword": {
  117. "type": "keyword",
  118. "ignore_above": 256
  119. }
  120. }
  121. },
  122. "sip": {
  123. "type": "text",
  124. "fields": {
  125. "keyword": {
  126. "type": "keyword",
  127. "ignore_above": 256
  128. }
  129. }
  130. },
  131. "source": {
  132. "type": "text",
  133. "fields": {
  134. "keyword": {
  135. "type": "keyword",
  136. "ignore_above": 256
  137. }
  138. }
  139. },
  140. "tags": {
  141. "type": "text",
  142. "fields": {
  143. "keyword": {
  144. "type": "keyword",
  145. "ignore_above": 256
  146. }
  147. }
  148. },
  149. "time": {
  150. "type": "text",
  151. "fields": {
  152. "keyword": {
  153. "type": "keyword",
  154. "ignore_above": 256
  155. }
  156. }
  157. },
  158. "url": {
  159. "type": "text",
  160. "fields": {
  161. "keyword": {
  162. "type": "keyword",
  163. "ignore_above": 256
  164. }
  165. }
  166. },
  167. "userid": {
  168. "type": "keyword"
  169. }
  170. }
  171. }
  172. }
  173. }

3、把旧索引的数据reindex到新索引上

注意,旧索引先停止新数据的写入

  1. POST _reindex
  2. {
  3. "source": {
  4. "index": "test"
  5. },
  6. "dest": {
  7. "index": "test-new"
  8. }
  9. }

4、删除旧索引

  1. DELETE test

5、按照步骤2创建test索引

  1. PUT test
  2. {
  3. "mappings": {
  4. "doc": {
  5. "properties": {
  6. "@timestamp": {
  7. "type": "date"
  8. },
  9. "@version": {
  10. "type": "text",
  11. "fields": {
  12. "keyword": {
  13. "type": "keyword",
  14. "ignore_above": 256
  15. }
  16. }
  17. },
  18. "beat": {
  19. "properties": {
  20. "hostname": {
  21. "type": "text",
  22. "fields": {
  23. "keyword": {
  24. "type": "keyword",
  25. "ignore_above": 256
  26. }
  27. }
  28. },
  29. "name": {
  30. "type": "text",
  31. "fields": {
  32. "keyword": {
  33. "type": "keyword",
  34. "ignore_above": 256
  35. }
  36. }
  37. },
  38. "version": {
  39. "type": "text",
  40. "fields": {
  41. "keyword": {
  42. "type": "keyword",
  43. "ignore_above": 256
  44. }
  45. }
  46. }
  47. }
  48. },
  49. "code": {
  50. "type": "long"
  51. },
  52. "dip": {
  53. "type": "text",
  54. "fields": {
  55. "keyword": {
  56. "type": "keyword",
  57. "ignore_above": 256
  58. }
  59. }
  60. },
  61. "fields": {
  62. "properties": {
  63. "log_topic": {
  64. "type": "text",
  65. "fields": {
  66. "keyword": {
  67. "type": "keyword",
  68. "ignore_above": 256
  69. }
  70. }
  71. }
  72. }
  73. },
  74. "host": {
  75. "type": "text",
  76. "fields": {
  77. "keyword": {
  78. "type": "keyword",
  79. "ignore_above": 256
  80. }
  81. }
  82. },
  83. "message": {
  84. "type": "text",
  85. "fields": {
  86. "keyword": {
  87. "type": "keyword",
  88. "ignore_above": 256
  89. }
  90. }
  91. },
  92. "method": {
  93. "type": "text",
  94. "fields": {
  95. "keyword": {
  96. "type": "keyword",
  97. "ignore_above": 256
  98. }
  99. }
  100. },
  101. "name": {
  102. "type": "text",
  103. "fields": {
  104. "keyword": {
  105. "type": "keyword",
  106. "ignore_above": 256
  107. }
  108. }
  109. },
  110. "offset": {
  111. "type": "long"
  112. },
  113. "referer": {
  114. "type": "text",
  115. "fields": {
  116. "keyword": {
  117. "type": "keyword",
  118. "ignore_above": 256
  119. }
  120. }
  121. },
  122. "sip": {
  123. "type": "text",
  124. "fields": {
  125. "keyword": {
  126. "type": "keyword",
  127. "ignore_above": 256
  128. }
  129. }
  130. },
  131. "source": {
  132. "type": "text",
  133. "fields": {
  134. "keyword": {
  135. "type": "keyword",
  136. "ignore_above": 256
  137. }
  138. }
  139. },
  140. "tags": {
  141. "type": "text",
  142. "fields": {
  143. "keyword": {
  144. "type": "keyword",
  145. "ignore_above": 256
  146. }
  147. }
  148. },
  149. "time": {
  150. "type": "text",
  151. "fields": {
  152. "keyword": {
  153. "type": "keyword",
  154. "ignore_above": 256
  155. }
  156. }
  157. },
  158. "url": {
  159. "type": "text",
  160. "fields": {
  161. "keyword": {
  162. "type": "keyword",
  163. "ignore_above": 256
  164. }
  165. }
  166. },
  167. "userid": {
  168. "type": "keyword"
  169. }
  170. }
  171. }
  172. }
  173. }

6、把test-new索引的数据reindex到test索引上

  1. POST _reindex
  2. {
  3. "source": {
  4. "index": "test-new"
  5. },
  6. "dest": {
  7. "index": "test"
  8. }
  9. }

6、查看test索引的mapping

  1. GET test/_mapping,执行命令后,可以看到userid的字段类型为keyword类型了
  2. 然后再打开该索引接收新数据的开关

更改elasticsearch中索引的mapping的更多相关文章

  1. 数组如何在ElasticSearch中索引

    一.简介 在ElasticSearch里没有专门的数组类型,任何一个字段都可以有零个和多个值.当字段值的个数大于1时,字段类型就变成了数组. 下面以视频数据为例,介绍ElasticSearch如何索引 ...

  2. 使用Hive读取ElasticSearch中的数据

    本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...

  3. elasticsearch中的mapping映射配置与查询典型案例

    elasticsearch中的mapping映射配置与查询典型案例 elasticsearch中的mapping映射配置示例比如要搭建个中文新闻信息的搜索引擎,新闻有"标题".&q ...

  4. Elasticsearch 通关教程(二): 索引映射Mapping问题

    数据库建表的时候,我们的DDL语句一般都会指定每个字段的存储类型,例如:varchar,int,datetime等等,目的很明确,就是更精确的存储数据,防止数据类型格式混乱. CREATE TABLE ...

  5. elasticsearch 5.6.4自动创建索引与mapping映射关系 +Java语言

    由于业务上的需求 ,最近在研究elasticsearch的相关知识 ,在网上查略了大部分资料 ,基本上对elasticsearch的数据增删改都没有太大问题 ,这里就不做总结了  .但是,在网上始终没 ...

  6. 一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)

    欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...

  7. Elasticsearch索引的操作,利用kibana 创建/删除一个es的索引及mapping映射

    索引的创建及删除 1. 通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射. 利用Kibana提供的DevTools来执行命令,要创建一个索引 ...

  8. ElasticSearch 中的 Mapping

    公号:码农充电站pro 主页:https://codeshellme.github.io 1,ES 中的 Mapping ES 中的 Mapping 相当于传统数据库中的表定义,它有以下作用: 定义索 ...

  9. 高效管理 Elasticsearch 中基于时间的索引——本质是在利用滚动模式做数据的冷热分离,热索引可以用ssd

    高效管理 Elasticsearch 中基于时间的索引 转自:http://stormluke.me/es-managing-time-based-indices-efficiently/ 用 Ela ...

随机推荐

  1. Cisco Packet Tracer Student(思科网络模拟器)模拟集线器和嗅探攻击

    一.集线器简介 集线器是局域网内的基础设备,工作于OSI中的物理层,作用是将接收的信号进行放大再传输,集线器是纯硬件设施,集线器开发之初就没考虑过软件层面的操作,所以不具备像路由器.交换机等设备那样具 ...

  2. 关于 用fscanf读文件,把文件中用##分割的内容分开

    今天呀,被学弟问了一个问题 文件里存的是"123##456##0##1644444.....##" 为什么用fscanf(fp, "%s##%s......", ...

  3. python获取线程返回值

    python获取线程返回值 前言 工作中的需求 将前端传过来的字符串信息通过算法转换成语音,并将语音文件返回回去 由于算法不是我写的,只需要调用即可,但是算法执行速度相当缓慢 我的优化思路是,将前端的 ...

  4. 小试牛刀:Go 反射帮我把 Excel 转成 Struct

    背景 起因于最近的一项工作:我们会定义一些关键指标来衡量当前系统的健康状态,然后配置对应的报警规则来进行监控报警.但是当前的报警规则会产生大量的误报,需要进行优化.我所负责的是将一些和用户行为指标相关 ...

  5. MySQL sql优化(摘抄自文档)

    前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...

  6. Shell 编程基础语法

    # shell脚本 # 如何运行shell脚本 sh test.sh source test.sh ./test.sh # 需要有执行权限 # source和其他两种的区别是.source不会开新进程 ...

  7. 开发 supermall 的一些

    0.新建项目 1.关联仓库:新建的远程仓库 怎么和已有代码联系起来 a.拉仓库 复制代码进去 b.在已有代码里面配置git: git remote add origin '地址' 然后 git pus ...

  8. MySQL主从复制之并行复制说明

    传统单线程复制说明 众所周知,MySQL在5.6版本之前,主从复制的从节点上有两个线程,分别是I/O线程和SQL线程. I/O线程负责接收二进制日志的Event写入Relay Log. SQL线程读取 ...

  9. CSP-J2021 题解

    分糖果 题意 选择L~R中的某个数 , 使得x mod k的结果最大. 思路 分两种情况考虑: 若 L 和 R 对 K 取模后在同一区间,则必然在 x=R 位置取到最大值: 否则 L~R 必然跨越多个 ...

  10. MyBatis-Plus 配置文件

    MyBatis-Plus在实际工作中常用到的配置,供自己和大家查阅学习. mybatis-plus: mapperPackage: com.**.**.mapper # 对应的 XML 文件位置 ma ...