es如何创建索引和mapping

其他教程   发布日期:2023年07月10日   浏览次数:401

本文小编为大家详细介绍“es如何创建索引和mapping”,内容详细,步骤清晰,细节处理妥当,希望这篇“es如何创建索引和mapping”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    es创建索引和mapping

    索引和type分开创建

    1、创建index

    1. http://127.0.0.1:9200/
    2. negative/ put
    3. {
    4. "settings": {
    5. "index": {
    6. "search": {
    7. "slowlog": {
    8. "threshold": {
    9. "fetch": {
    10. "debug": "5s"
    11. },
    12. "query": {
    13. "warn": "20s"
    14. }
    15. }
    16. }
    17. },
    18. "indexing": {
    19. "slowlog": {
    20. "threshold": {
    21. "index": {
    22. "info": "20s"
    23. }
    24. }
    25. }
    26. },
    27. "number_of_shards": "1",
    28. "number_of_replicas": "0"
    29. }
    30. }
    31. }

    2、创建mapping

    1. http://127.0.0.1:9200/
    2. negative/negative/_mapping post
    3. {"properties":{
    4. "id": {
    5. "type": "long"
    6. },
    7. "yjlb": {
    8. "type": "text",
    9. "fields": {
    10. "keyword": {
    11. "type": "keyword",
    12. "ignore_above": 256
    13. }
    14. }
    15. },
    16. "ejlb": {
    17. "type": "text",
    18. "fields": {
    19. "keyword": {
    20. "type": "keyword",
    21. "ignore_above": 256
    22. }
    23. }
    24. },
    25. "sjlb": {
    26. "type": "text",
    27. "fields": {
    28. "keyword": {
    29. "type": "keyword",
    30. "ignore_above": 256
    31. }
    32. }
    33. },
    34. "detail": {
    35. "type": "text",
    36. "fields": {
    37. "keyword": {
    38. "type": "keyword",
    39. "ignore_above": 256
    40. }
    41. }
    42. },
    43. "ssyj": {
    44. "type": "text",
    45. "fields": {
    46. "keyword": {
    47. "type": "keyword",
    48. "ignore_above": 256
    49. }
    50. }
    51. }
    52. }}

    索引和type一次创建

    (注意:mapping下面一层的key值 是type名称)

    1. http://192.168.0.213:9200/
    2. announcement/ put
    3. {
    4. "settings": {
    5. "index": {
    6. "search": {
    7. "slowlog": {
    8. "threshold": {
    9. "fetch": {
    10. "debug": "5s"
    11. },
    12. "query": {
    13. "warn": "20s"
    14. }
    15. }
    16. }
    17. },
    18. "indexing": {
    19. "slowlog": {
    20. "threshold": {
    21. "index": {
    22. "info": "20s"
    23. }
    24. }
    25. }
    26. },
    27. "number_of_shards": "1",
    28. "number_of_replicas": "0"
    29. }
    30. },
    31. "mappings": {
    32. "announcement": {
    33. "properties": {
    34. "id": {
    35. "type": "keyword"
    36. },
    37. "createtime": {
    38. "type": "date",
    39. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    40. },
    41. "creatby": {
    42. "type": "keyword"
    43. },
    44. "updatetime": {
    45. "type": "date",
    46. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    47. },
    48. "type": {
    49. "type": "keyword"
    50. },
    51. "status": {
    52. "type": "keyword"
    53. },
    54. "title": {
    55. "type": "text",
    56. "fields": {
    57. "keyword": {
    58. "type": "keyword",
    59. "ignore_above": 256
    60. }
    61. }
    62. },
    63. "cont": {
    64. "type": "text",
    65. "fields": {
    66. "keyword": {
    67. "type": "keyword",
    68. "ignore_above": 256
    69. }
    70. }
    71. },
    72. "files": {
    73. "type": "nested",
    74. "properties": {
    75. "id": {
    76. "type": "keyword"
    77. },
    78. "filename": {
    79. "type": "text",
    80. "fields": {
    81. "keyword": {
    82. "type": "keyword",
    83. "ignore_above": 256
    84. }
    85. }
    86. }
    87. }
    88. }
    89. }
    90. }
    91. }
    92. }

    更改elasticsearch中索引的mapping

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

    查看旧索引的mapping

      1. GET test
      /
      1. _mapping

    找到userid这个字段,修改类型为keyword,如下:

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

    创建一个自定

    以上就是es如何创建索引和mapping的详细内容,更多关于es如何创建索引和mapping的资料请关注九品源码其它相关文章!