ElasticSearch 使用动态映射模板定制自己的映射策略

 

一、概述

首先,改索引的映射比较麻烦,实际上ES是不支持无痛修改映射的,如果要修改映射,需要新建一个新的索引,为新的索引自定义映射,然后把旧索引下的数据导入到新索引下面。

需求:

比如说,我们本来没有某个type,或者没有某个field,但是希望在插入数据的时候,es自动为我们做一个识别,动态映射出这个type的mapping,包括每个field的数据类型,一般用的动态映射,dynamic mapping

这里有个问题,如果说,我们其实对dynamic mapping有一些自己独特的需求,比如说,es默认来说,如经过识别到一个数字,field: 10,默认是搞成这个field的数据类型是long,再比如说,如果我们弄了一个field : "10",默认就是text,还会带一个keyword的内置field。我们没法改变。

但是我们现在就是希望动态映射的时候,根据我们的需求去映射,而不是让es自己按照默认的规则去玩儿

二、dyanmic mapping template,动态映射模板

我们自己预先定义一个模板,然后插入数据的时候,相关的field,如果能够根据我们预先定义的规则,匹配上某个我们预定义的模板,那么就会根据我们的模板来进行mapping,决定这个Field的数据类型 0、默认的动态映射的效果咋样

 DELETE /my_index
 PUT /my_index/my_type/1
 {
   "test_string": "hello world",
   "test_number": 10
 }
 GET /my_index/_mapping/my_type
 {
   "my_index": {
     "mappings": {
       "my_type": {
         "properties": {
           "test_number": {
             "type": "long"
           },
           "test_string": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           }
         }
       }
     }
   }
 }
 PUT my_index
 {
   "mappings": {
     "my_type": {
       "dynamic_templates": [
         {
           "integers": {
             "match_mapping_type": "long",
             "mapping": {
               "type": "integer"
             }
           }
         },
         {
           "strings": {
             "match_mapping_type": "string",
             "mapping": {
               "type": "text",
               "fields": {
                 "raw": {
                   "type": "keyword",
                   "ignore_above": 500
                 }
               }
             }
           }
         }
       ]
     }
   }
 }
 PUT /my_index/my_type/1
 {
   "test_long": 1,
   "test_string": "hello world"
 }
 {
   "my_index": {
     "mappings": {
       "my_type": {
         "dynamic_templates": [
           {
             "integers": {
               "match_mapping_type": "long",
               "mapping": {
                 "type": "integer"
               }
             }
           },
           {
             "strings": {
               "match_mapping_type": "string",
               "mapping": {
                 "fields": {
                   "raw": {
                     "ignore_above": 500,
                     "type": "keyword"
                   }
                 },
                 "type": "text"
               }
             }
           }
         ],
         "properties": {
           "test_number": {
             "type": "integer"
           },
           "test_string": {
             "type": "text",
             "fields": {
               "raw": {
                 "type": "keyword",
                 "ignore_above": 500
               }
             }
           }
         }
       }
     }
   }
 PUT /my_index 
 {
   "mappings": {
     "my_type": {
       "dynamic_templates": [
         {
           "string_as_integer": {
             "match_mapping_type": "string",
             "match": "long_*",
             "unmatch": "*_text",
             "mapping": {
               "type": "integer"
             }
           }
         }
       ]
     }
   }
 }
 {
   "my_index": {
     "mappings": {
       "my_type": {
         "dynamic_templates": [
           {
             "string_as_integer": {
               "match": "long_*",
               "unmatch": "*_text",
               "match_mapping_type": "string",
               "mapping": {
                 "type": "integer"
               }
             }
           }
         ],
         "properties": {
           "long_field": {
             "type": "integer"
           },
           "long_field_text": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           }
         }
       }
     }
   }
 }

这些数据,每天的数据都放一个新的type中,每天的数据都会哗哗的往新的tye中写入,此时你就可以定义一个模板,搞一个脚本,每天都预先生成一个新type的模板,里面讲你的各个Field都匹配到一个你预定义的模板中去,就好了

场景,有些时候,dynamic mapping + template,每天有一堆日志,每天有一堆数据

举个例子,field : "10",把类似这种field,弄成long型

2、根据字段名配映射模板

  • 第一种,是根据新加入的field的默认的数据类型,来进行匹配,匹配上某个预定义的模板;

  • 第二种,是根据新加入的field的名字,去匹配预定义的名字,或者去匹配一个预定义的通配符,然后匹配上某个预定义的模板

动态映射模板,有两种方式

1、根据类型匹配映射模板

  • test_number,如果是个数字,我们希望默认就是integer类型的

  • test_string,如果是字符串,我们希望默认是个text,这个没问题,但是内置的field名字,叫做raw,不叫座keyword,类型还是keyword,保留500个字符

我们比如说,现在想要的效果:

这个就是es的默认的动态映射规则,可能就不是我们想要的

es的自动的默认的,动态映射是咋样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值