elasticsearch 模板

elasticsearch 模板分为动态模板和索引模板

1. Index Template(索引模板)

设定mapping和setting,并按照一定的规则自动匹配到新创建的索引上。

  • 模板仅会在第一个索引被新创建的时候才会起作用,修改模板对已经创建的索引没有影响。
  • 创建多个索引模板,模板配置会合并
  • 可以通过指定order的数值,控制合并的过程

当索引的名字是以test开头的时候,将主分片设置2,副本分片的数量设置为3,关闭日期检测功能,打开数字检测功能。

PUT /_template/template_default
{
  "index_patterns":["test*"],
  "order":1,
  "settings":{
    "number_of_shards":2,
    "number_of_replicas":3
  },
  "mappings":{
    "date_detection":false,
    "numeric_detection":true
  }
}

当一个索引被创建的时候,工作模式如下

  • 应用ES默认的settings和mappings
  • 应用order数值低的index template的设定
  • 应用高的index template的设定,并覆盖之前的设定。
  • 应用创建索引时,用户所指定的settings和mappings,并覆盖之前模板中的设定。
POST test_template/_doc
{
  "number":"1",
  "date":"2020/01/01"
}
GET test_template/_mapping

 查看mapping结果符合预期

{
  "test_template" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "date" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "number" : {
          "type" : "long"
        }
      }
    }
  }
}

2.Dynamic Template(动态模板)

用于根据ES识别的数据类型,结合字段名称,来动态的设定字段类型。比如:

  • 所有的字符串类型都可以设定为keyword,或者关闭keyword字段。
  • is开头的设置为boolean
  • long开头的设置为long类型     

说明:

  • dynamic template是定义在某个索引的mapping中的
  • template有一个名称
  • 匹配规则是一个数组
  • 为匹配到的字段设置mapping

语法规则

"dynamic_templates": [
    {
      "my_template_name": { 
        ...  match conditions ... 
        "mapping": { ... } 
      }
    },
    ...
  ]

1,)my_template_name:模板的名称

2,)match conditions:匹配规则

3,)mapping:匹配后的mapping规则

常见匹配规则

   1.match_mapping_type

    本来ES觉得这个字段应该映射成long,那么我们可以修改成integer。

PUT my_index
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "integers": {
            "match_mapping_type": "long",
            "mapping": {
              "type": "integer"
            }
          }
        },
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "fields": {
                "raw": {
                  "type":  "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

   2.match and unmatch(这里主要是对字段名称进行匹配处理。例如我们想对所有string类型、以long开头、并不以text结尾的字段改成long类型)。

PUT my_index
{
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "longs_as_strings": {
            "match_mapping_type": "string",
            "match":   "long_*",
            "unmatch": "*_text",
            "mapping": {
              "type": "long"
            }
          }
        }
      ]
    }
  }
}

   3. 正则

PUT my_index
{
    "mappings": {
        "dynamic_templates": [
            {
                "longs_as_strings": {
                    "match_pattern": "regex",
                    "match": "^profit_\\d+$",
                    "mapping": {
                        "type": "long",
                        "index": false,
                        "norms": false,
                        "doc_values": false
                    }
                }
            }
        ]
    }
}

名称为dynamic_templates,第一个表示把is开头的字符串转换boolean值处理,第二个表示把其他的字段当成keyword。

 


版权声明:本文为azh89125原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。