es通过dynamic templates自动创建自动适配类型的索引

基本思路

创建与索引名关联的templates,写入的时候指定索引名,根据es自动创建索引的特性+templates自动创建自己需要的索引

es动态mapping

dynamic templates

Dynamic templates 用于自定义在动态添加field的时候自动给field设置的数据类型, 给什么类型基于:

  • datatype detected by Elasticsearch
  • the name or the field
  • the full dotted path to the field

创建dynamic templates

Dynamic templates are specified as an array of named objects:

   "dynamic_templates": [
    {
      "my_template_name": { 
        ...  match conditions ... 
        "mapping": { ... } 
      }
    },
    ...
  ]
  1. my_template_name can be any string value
  2. The match conditions can include any of : match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch.
  3. The mapping that the matched field should use

Templates将会顺序执行,直到新增的字段value某一个templates的match condition。可以通过PUT mappingAPI向templates list中追加templates,如果新增的templates和某个旧的重名,它将会替代那个旧的。

templates中的match conditions的类型

match_mapping_type

match_mapping_type用于匹配被dynamic field mapping识别到的es数据类型,换句话说,是es对这个field应该是什么类型的猜测。只有以下数据类型能被es自动识别:

boolean, date, double, long, object, string

可以在定义match_mapping_type的时候用*来表示匹配所有数据类型。
示例:

把自动识别为long类型的field定义为interger类型,把自动识别为string类型的field同时定义为analyzed和not_analyzed:
PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "integers": {
            "match_mapping_type": "long",
            "mapping": {
              "type": "integer"
            }
          }
        },
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "string",
              "fields": {
                "raw": {
                  "type":  "string",
                  "index": "not_analyzed",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}
match and unmatch

match和unmatch定义应用于filedname的pattern。
示例:

定义一个匹配所有以long_开头且不以_text结束的string类型的模板
PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "longs_as_strings": {
            "match_mapping_type": "string",
            "match":   "long_*",
            "unmatch": "*_text",
            "mapping": {
              "type": "long"
            }
          }
        }
      ]
    }
  }
}
match_pattern

用于调整match参数的行为,比如当match_pattern的值为regex时,match可以支持完整的java正则表达式而不是简单的通配符。

path_match and path_unmatch

用于object类型

{name}和{dynamic_type}

{name}和{dynamic_type}占位符放在mapping中,值为field name和field被es识别的数据类型

覆盖default template

可以通过设置_default_类型的mapping覆盖默认的template,影响范围为所有的indices和types


结合动态mapping和auto create index自动创建索引

  1. 通过index template api创建索引模板
  2. 指定索引名写入数据,或者用索引名创建索引

示例如下

(5.x+):
PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
(2.3)
PUT /_template/template_1
{
  "template": "te*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值