es经过dynamic templates自动建立自动适配类型的索引

基本思路

建立与索引名关联的templates,写入的时候指定索引名,根据es自动建立索引的特性+templates自动建立本身须要的索引html

es动态mapping

dynamic templates

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

  • 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和某个旧的重名,它将会替代那个旧的。api

templates中的match conditions的类型

match_mapping_type

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

boolean, date, double, long, object, string

能够在定义match_mapping_type的时候用*来表示匹配全部数据类型。
示例:elasticsearch

把自动识别为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。
示例:ide

定义一个匹配全部以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正则表达式而不是简单的通配符。ui

path_match and path_unmatch

用于object类型code

{name}和{dynamic_type}

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

覆盖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"
        }
      }
    }
  }
}
相关文章
相关标签/搜索