• Elasticsearch: Index Template


    序言

    索引模板用于我们在新建索引的时候,不用重复的去设置相关的配置新.而是直接沿用现成的.

    同时根据索引模板已经建成的索引,不会因为模板的修改而发生改变cuiyaonan2000@163.com

    create index API请求中指定的设置和映射会覆盖索引模板中指定的任何设置或映射,即索引模板配置设置优先级不是最高的.

    Mapping&Setting

    首先创建一个索引

    PUT cuiyaonan_index

    然后过去该索引的配置信息即:Mapping 和Setting信息

    GET cuiyaonan_index

    返回如下内容

    1. {
    2. "cuiyaonan_index" : {
    3. "aliases" : { },
    4. "mappings" : { },
    5. "settings" : {
    6. "index" : {
    7. "routing" : {
    8. "allocation" : {
    9. "include" : {
    10. "_tier_preference" : "data_content"
    11. }
    12. }
    13. },
    14. "number_of_shards" : "1",
    15. "provided_name" : "cuiyaonan_index",
    16. "creation_date" : "1660703634990",
    17. "number_of_replicas" : "1",
    18. "uuid" : "dNyxiwJhSDGO--UFd71jVA",
    19. "version" : {
    20. "created" : "7140099"
    21. }
    22. }
    23. }
    24. }
    25. }

    因为是新建的空白表所以没有mapping.如下的是logstash的索引配置信息,可以对照着看一下cuiyaonan2000@163.com

    1. {
    2. "my-filebeat-5-logstash-2022.08.10" : {
    3. "aliases" : { },
    4. "mappings" : {
    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. "agent" : {
    19. "properties" : {
    20. "ephemeral_id" : {
    21. "type" : "text",
    22. "fields" : {
    23. "keyword" : {
    24. "type" : "keyword",
    25. "ignore_above" : 256
    26. }
    27. }
    28. },
    29. "hostname" : {
    30. "type" : "text",
    31. "fields" : {
    32. "keyword" : {
    33. "type" : "keyword",
    34. "ignore_above" : 256
    35. }
    36. }
    37. },
    38. "id" : {
    39. "type" : "text",
    40. "fields" : {
    41. "keyword" : {
    42. "type" : "keyword",
    43. "ignore_above" : 256
    44. }
    45. }
    46. },
    47. "name" : {
    48. "type" : "text",
    49. "fields" : {
    50. "keyword" : {
    51. "type" : "keyword",
    52. "ignore_above" : 256
    53. }
    54. }
    55. },
    56. "type" : {
    57. "type" : "text",
    58. "fields" : {
    59. "keyword" : {
    60. "type" : "keyword",
    61. "ignore_above" : 256
    62. }
    63. }
    64. },
    65. "version" : {
    66. "type" : "text",
    67. "fields" : {
    68. "keyword" : {
    69. "type" : "keyword",
    70. "ignore_above" : 256
    71. }
    72. }
    73. }
    74. }
    75. },
    76. "ecs" : {
    77. "properties" : {
    78. "version" : {
    79. "type" : "text",
    80. "fields" : {
    81. "keyword" : {
    82. "type" : "keyword",
    83. "ignore_above" : 256
    84. }
    85. }
    86. }
    87. }
    88. },
    89. "host" : {
    90. "properties" : {
    91. "architecture" : {
    92. "type" : "text",
    93. "fields" : {
    94. "keyword" : {
    95. "type" : "keyword",
    96. "ignore_above" : 256
    97. }
    98. }
    99. },
    100. "containerized" : {
    101. "type" : "boolean"
    102. },
    103. "hostname" : {
    104. "type" : "text",
    105. "fields" : {
    106. "keyword" : {
    107. "type" : "keyword",
    108. "ignore_above" : 256
    109. }
    110. }
    111. },
    112. "id" : {
    113. "type" : "text",
    114. "fields" : {
    115. "keyword" : {
    116. "type" : "keyword",
    117. "ignore_above" : 256
    118. }
    119. }
    120. },
    121. "ip" : {
    122. "type" : "text",
    123. "fields" : {
    124. "keyword" : {
    125. "type" : "keyword",
    126. "ignore_above" : 256
    127. }
    128. }
    129. },
    130. "mac" : {
    131. "type" : "text",
    132. "fields" : {
    133. "keyword" : {
    134. "type" : "keyword",
    135. "ignore_above" : 256
    136. }
    137. }
    138. },
    139. "name" : {
    140. "type" : "text",
    141. "fields" : {
    142. "keyword" : {
    143. "type" : "keyword",
    144. "ignore_above" : 256
    145. }
    146. }
    147. },
    148. "os" : {
    149. "properties" : {
    150. "codename" : {
    151. "type" : "text",
    152. "fields" : {
    153. "keyword" : {
    154. "type" : "keyword",
    155. "ignore_above" : 256
    156. }
    157. }
    158. },
    159. "family" : {
    160. "type" : "text",
    161. "fields" : {
    162. "keyword" : {
    163. "type" : "keyword",
    164. "ignore_above" : 256
    165. }
    166. }
    167. },
    168. "kernel" : {
    169. "type" : "text",
    170. "fields" : {
    171. "keyword" : {
    172. "type" : "keyword",
    173. "ignore_above" : 256
    174. }
    175. }
    176. },
    177. "name" : {
    178. "type" : "text",
    179. "fields" : {
    180. "keyword" : {
    181. "type" : "keyword",
    182. "ignore_above" : 256
    183. }
    184. }
    185. },
    186. "platform" : {
    187. "type" : "text",
    188. "fields" : {
    189. "keyword" : {
    190. "type" : "keyword",
    191. "ignore_above" : 256
    192. }
    193. }
    194. },
    195. "type" : {
    196. "type" : "text",
    197. "fields" : {
    198. "keyword" : {
    199. "type" : "keyword",
    200. "ignore_above" : 256
    201. }
    202. }
    203. },
    204. "version" : {
    205. "type" : "text",
    206. "fields" : {
    207. "keyword" : {
    208. "type" : "keyword",
    209. "ignore_above" : 256
    210. }
    211. }
    212. }
    213. }
    214. }
    215. }
    216. },
    217. "input" : {
    218. "properties" : {
    219. "type" : {
    220. "type" : "text",
    221. "fields" : {
    222. "keyword" : {
    223. "type" : "keyword",
    224. "ignore_above" : 256
    225. }
    226. }
    227. }
    228. }
    229. },
    230. "log" : {
    231. "properties" : {
    232. "file" : {
    233. "properties" : {
    234. "path" : {
    235. "type" : "text",
    236. "fields" : {
    237. "keyword" : {
    238. "type" : "keyword",
    239. "ignore_above" : 256
    240. }
    241. }
    242. }
    243. }
    244. },
    245. "flags" : {
    246. "type" : "text",
    247. "fields" : {
    248. "keyword" : {
    249. "type" : "keyword",
    250. "ignore_above" : 256
    251. }
    252. }
    253. },
    254. "offset" : {
    255. "type" : "long"
    256. }
    257. }
    258. },
    259. "message" : {
    260. "type" : "text",
    261. "fields" : {
    262. "keyword" : {
    263. "type" : "keyword",
    264. "ignore_above" : 256
    265. }
    266. }
    267. },
    268. "mytime" : {
    269. "type" : "text",
    270. "fields" : {
    271. "keyword" : {
    272. "type" : "keyword",
    273. "ignore_above" : 256
    274. }
    275. }
    276. },
    277. "tags" : {
    278. "type" : "text",
    279. "fields" : {
    280. "keyword" : {
    281. "type" : "keyword",
    282. "ignore_above" : 256
    283. }
    284. }
    285. }
    286. }
    287. },
    288. "settings" : {
    289. "index" : {
    290. "routing" : {
    291. "allocation" : {
    292. "include" : {
    293. "_tier_preference" : "data_content"
    294. }
    295. }
    296. },
    297. "number_of_shards" : "1",
    298. "provided_name" : "my-filebeat-5-logstash-2022.08.10",
    299. "creation_date" : "1660704979105",
    300. "number_of_replicas" : "1",
    301. "uuid" : "ZstZ-zJISsGx5zTM5h3OMA",
    302. "version" : {
    303. "created" : "7140099"
    304. }
    305. }
    306. }
    307. }
    308. }

    Mapping

    Mapping 是用于定义 Document 及其包含的字段类型索引方式相关的信息,但在Elasticsearch中,可以动态的识别新增的字段值来推测出字段类型动态增加

    1、定义索引中字段的名称

    2、定义字段的数据类型,如:字符串、数字、boolean等

    3、可对字段设置倒排索引的相关配置,如是否需要分词,使用什么分词器

    1. "mappings" : { #mappings 关键字
    2. "properties" : { #关键字
    3. "end_time" : { #自定义的document中的字段
    4. "type" : "long" #为该字段定义的字段类型
    5. },
    6. "endpoint" : {
    7. "type" : "keyword"
    8. }
    9. }

    如上end_time中的其它可以选设置如下所示:

    1. "field": {
    2. "type": "text", //文本类型 ,指定类型
    3. "index": "false"// ,设置成false,字段将不会被索引
    4. "analyzer":"ik"//指定分词器
    5. "boost":1.23//字段级别的分数加权
    6. "doc_values":false//对not_analyzed字段,默认都是开启,analyzed字段不能使用,对排序和聚合能提升较大性能,节约内存,如果您确定不需要对字段进行排序或聚合,或者从script访问字段值,则可以禁用doc值以节省磁盘空间:
    7. "fielddata":{"loading" : "eager" }//Elasticsearch 加载内存 fielddata 的默认行为是 延迟 加载 。 当 Elasticsearch 第一次查询某个字段时,它将会完整加载这个字段所有 Segment 中的倒排索引到内存中,以便于以后的查询能够获取更好的性能。
    8. "fields":{"keyword": {"type": "keyword","ignore_above": 256}} //可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词
    9. "ignore_above":100 //超过100个字符的文本,将会被忽略,不被索引
    10. "include_in_all":ture//设置是否此字段包含在_all字段中,默认是true,除非index设置成no选项
    11. "index_options":"docs"//4个可选参数docs(索引文档号) ,freqs(文档号+词频),positions(文档号+词频+位置,通常用来距离查询),offsets(文档号+词频+位置+偏移量,通常被使用在高亮字段)分词字段默认是position,其他的默认是docs
    12. "norms":{"enable":true,"loading":"lazy"}//分词字段默认配置,不分词字段:默认{"enable":false},存储长度因子和索引时boost,建议对需要参与评分字段使用 ,会额外增加内存消耗量
    13. "null_value":"NULL"//设置一些缺失字段的初始化值,只有string可以使用,分词字段的null值也会被分词
    14. "position_increament_gap":0//影响距离查询或近似查询,可以设置在多值字段的数据上火分词字段上,查询时可指定slop间隔,默认值是100
    15. "store":false//是否单独设置此字段的是否存储而从_source字段中分离,默认是false,只能搜索,不能获取值
    16. "search_analyzer":"ik"//设置搜索时的分词器,默认跟ananlyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能
    17. "similarity":"BM25"//默认是TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效
    18. "term_vector":"no"//默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用
    19. }

    注意事项

    • mapping中字段类型一旦设定后 禁止直接修改。因为lucene实现的倒排索引生成后不允许修
    • Elasticsearch有动态映射功能(Dynamic Mapping ):在写入文档时,如果索引不存在,会自动创建索引 .这种机制,使得我们无需手动定义mappings。Elasticsearch会自动根据文档信息,推算出字段的类型

    Setting

    Setting 是用于定义 Index 分片数量及副本数量等相关的配置信息,其中分片数量在设定后不能修改,而副本数量可以在运行过程中根据业务需要动态调整。因此Setting的配置分为静态配置和动态配置

    静态配置

    所谓的静态配置即索引创建后不能修改,静态配置只能在创建索引时或者在状态为 closed index的索引(闭合的索引)上设置

    1. index.number_of_shards :主分片数,默认为5.只能在创建索引时设置,不能修改
    2. index.shard.check_on_startup :是否在索引打开前检查分片是否损坏,当检查到分片损坏将禁止分片被打开
    3. 可选值:false:不检测;checksum:只检查物理结构;true:检查物理和逻辑损坏,相对比较耗CPU;fix:类同与false,7.0版本后将废弃。默认值:false
    4. index.codec:数据存储的压缩算法,默认算法为LZ4,也可以设置成best_compression,best_compression压缩比较好,但存储性能比LZ4差
    5. index.routing_partition_size :路由分区数,默认为 1,只能在索引创建时设置。此值必须小于index.number_of_shards,如果设置了该参数,其路由算法为: (hash(_routing) + hash(_id) % index.routing_parttion_size ) % number_of_shards。如果该值不设置,则路由算法为 hash(_routing) % number_of_shardings,_routing默认值为_id。

    动态配置

    1. index.number_of_replicas :每个主分片的副本数,默认为 1,该值必须大于等于0
    2. index.auto_expand_replicas :基于可用节点的数量自动分配副本数量,默认为 false(即禁用此功能)
    3. index.refresh_interval :执行刷新操作的频率,这使得索引的最近更改可以被搜索。默认为 1s。可以设置为 -1 以禁用刷新。
    4. index.max_result_window :用于索引搜索的 from+size 的最大值。默认为 10000
    5. index.max_rescore_window : 在搜索此索引中 rescore 的 window_size 的最大值
    6. index.blocks.read_only :设置为 true 使索引和索引元数据为只读,false 为允许写入和元数据更改。
    7. index.blocks.read_only_allow_delete:与index.blocks.read_only基本类似,唯一的区别是允许删除动作。
    8. index.blocks.read :设置为 true 可禁用对索引的读取操作
    9. index.blocks.write :设置为 true 可禁用对索引的写入操作。
    10. index.blocks.metadata :设置为 true 可禁用索引元数据的读取和写入。
    11. index.max_refresh_listeners :索引的每个分片上可用的最大刷新侦听器数
    12. index.max_docvalue_fields_search:一次查询最多包含开启doc_values字段的个数,默认为100。

    常用配置

    1. # 设置分片数量
    2. number_of_shards
    3. # 设置副本数量
    4. number_of_replicas
    5. # 设置自动扩展副本数量
    6. index.auto_expand_replicas
    7. # 设置 Index Buffer 刷新间隔
    8. refresh_interval
    9. # 设置自定义分词器
    10. analysis

    创建模板

    使用如下命令创建

    PUT /_template/<index-template>

    查看索引模板

    GET 索引名称

  • 相关阅读:
    基苯乙烯微球支载L-脯氨酸磺酰胺/水滑石@磺化聚苯乙烯微球/石蜡聚苯乙烯微球的制备
    java计算机毕业设计网站内容管理系统(附源码、数据库)
    perf工具与perf report children的含义
    uniapp 之 获取底部安全距离,状态栏高度等
    第一章 计算机系统概述
    4.后端·新建子模块与开发(传统模式)
    Frechet distance距离计算原理及python实现
    Vue3中的setup函数
    jenkins自动化脚本集成时钉钉消息未发送
    Nodejs path模块常用方法
  • 原文地址:https://blog.csdn.net/cuiyaonan2000/article/details/126378924