Elasticsearch索引模板

目录

1、索引模板是什么

2、索引模板的操作

2.1、定制索引结构

2.2、查询索引模板

2.3、创建索引

2.4、删除索引模板

2.5、es内置的索引模板

3、模板组件

3.1、创建组件模板

3.2、查看组件模板

3.3、使用组件模板

3.4、删除组件模板


1、索引模板是什么

        当需要为同一类索引应用相同的配置、映射、别名时,如果每次创建索引都逐一配置会比较麻烦。索引模板的出现正是为了简化这种操作,使用索引模板你可以方便地为某一类索引自动配置某些共同的参数。

2、索引模板的操作

2.1、定制索引结构

        如果你想在es中创建两个索引order1和order2,这两个索引分别记录了不同业务类型的订单信息,他们的映射结构、分片数、别名都相同。为了达成这个目的,可以创建一个索引模板order-template。

PUT _index_template/order-template
{
  "index_patterns": [
    "order*"
  ],
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "order_id": {
          "type": "keyword"
        },
        "order_name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "create_time": {
          "type": "date",
          "format": ["yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"]
        }
      }
    },
    "aliases": {
      "order": {}
    }
  },
  "version": 3,
  "priority": 100,
  "_meta":{
    "description":"this is order-template"
  }
}

        在上面的配置中,index_patterns用于设置索引模板可以匹配的索引名称,这里使用*号通配符,表示所有以order开头的索引都会使用上面的模板。这个模板还配置了索引的分片数为3、副本分片数为1、字段映射和别名。priority用来设置模板的优先级,其值越大优先级越高。version表示版本号,_meta可以保存一些元数据。当模板order-template已经存在时,再次编辑模板配置并发送上述请求可以修改模板的内容。

2.2、查询索引模板

        如果想查询索引模板信息,可以使用下面的代码

GET _index_template/order-template

2.3、创建索引

        索引模板创建之后,当我们创建索引时,如果索引名称被索引模板匹配到,就会自动加载索引模板的配置到索引映射中。

        创建一个索引,索引名为order001

PUT order001

        创建成功后,查看索引的配置信息

GET order001

        查询成功后,可以在代码运行结果中看到模板中的配置已经在索引order001中得到应用,运行结果如下

{
  "order001" : {
    "aliases" : {
      "order" : { }
    },
    "mappings" : {
      "properties" : {
        "create_time" : {
          "type" : "date",
          "format" : "[yyyy-MM-dd, yyyy-MM-dd HH:mm:ss]"
        },
        "order_id" : {
          "type" : "keyword"
        },
        "order_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1698127116029",
        "number_of_shards" : "3",
        "number_of_replicas" : "1",
        "uuid" : "gZG1JzzrTtGC4OrvPwujuA",
        "version" : {
          "created" : "7090199"
        },
        "provided_name" : "order001"
      }
    }
  }
}

        如果你想在创建索引时自定义一些配置,可以在创建索引映射时指定,这样就可以覆盖掉索引模板的配置。创建一个索引,名称为order002,自定义分片数为5,副本分片数为2,新增一个字段amount,调整create_time字段的时间格式,代码如下

PUT order002
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "amount":{
        "type": "float"
      },
      "create_time":{
        "type": "date",
        "format": ["yyyy/MM/dd HH/mm/ss"]
      }
    }
  }
}

        查看order002的索引映射结果

GET order002

      查询成功后,可以在代码运行结果中看到order002的索引映射中的配置确实成功覆盖了索引模板中的配置

{
  "order002" : {
    "aliases" : {
      "order" : { }
    },
    "mappings" : {
      "properties" : {
        "amount" : {
          "type" : "float"
        },
        "create_time" : {
          "type" : "date",
          "format" : "[yyyy/MM/dd HH/mm/ss]"
        },
        "order_id" : {
          "type" : "keyword"
        },
        "order_name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1698127721114",
        "number_of_shards" : "5",
        "number_of_replicas" : "2",
        "uuid" : "K3NlLyaGTOW9c8W8ZcSO5g",
        "version" : {
          "created" : "7090199"
        },
        "provided_name" : "order002"
      }
    }
  }
}

2.4、删除索引模板

        删除索引模板,执行如下代码

DELETE _index_template/order-template

2.5、es内置的索引模板

        Elasticsearch内置了两个索引模板,索引模板名称分别为metrics和logs,分别用来匹配名称符合logs-*-*和metrics-*-*的索引。在创建索引和索引模板时要特别注意不要匹配错索引模板,以避免最后索引配置的效果达不到预期。

GET _index_template


{
  "index_templates" : [
    {
      "name" : "metrics",
      "index_template" : {
        "index_patterns" : [
          "metrics-*-*"
        ],
        "composed_of" : [
          "metrics-mappings",
          "metrics-settings"
        ],
        "priority" : 100,
        "version" : 0,
        "_meta" : {
          "managed" : true,
          "description" : "default metrics template installed by x-pack"
        },
        "data_stream" : { }
      }
    },
    {
      "name" : "logs",
      "index_template" : {
        "index_patterns" : [
          "logs-*-*"
        ],
        "composed_of" : [
          "logs-mappings",
          "logs-settings"
        ],
        "priority" : 100,
        "version" : 0,
        "_meta" : {
          "managed" : true,
          "description" : "default logs template installed by x-pack"
        },
        "data_stream" : { }
      }
    }
  ]
}

3、模板组件

       按照文章上面的操作我们已经可以快速地创建索引模板,并可以把配置自动应用到匹配的索引中了,但是这样做会使得索引模板的配置内容较多。为了简化索引模板中的配置内容,我们可以把常规的索引设置、映射等内容写成可复用的模板组件,然后在索引模板中引用这些组件,这样模板中的配置内容就会非常简洁,便于移植和管理。

3.1、创建组件模板

        创建组件模板component001和component002

PUT _component_template/component001
{
  "template":{
    "mappings":{
      "properties":{
        "date":{
          "type":"date",
          "format":["yyy-MM-dd HH:mm:ss"]
        }
      }
    }
  }
}

PUT _component_template/component002
{
  "template": {
    "settings": {
      "number_of_shards": "3",
      "number_of_replicas": "1"
    },
    "aliases": {
      "mycomp": {}
    }
  }
}

3.2、查看组件模板

        组件模板创建成功后,我们可以执行代码查看组件模板的端点信息

GET _component_template/component*

{
  "component_templates" : [
    {
      "name" : "component002",
      "component_template" : {
        "template" : {
          "settings" : {
            "index" : {
              "number_of_shards" : "3",
              "number_of_replicas" : "1"
            }
          },
          "aliases" : {
            "mycomp" : { }
          }
        }
      }
    },
    {
      "name" : "component001",
      "component_template" : {
        "template" : {
          "mappings" : {
            "properties" : {
              "date" : {
                "format" : [
                  "yyy-MM-dd HH:mm:ss"
                ],
                "type" : "date"
              }
            }
          }
        }
      }
    }
  ]
}

3.3、使用组件模板

        创建一个索引模板comp-test,把上面两个组件模板加载到索引模板中,索引模板就会匹配所有名称以comp开头的索引。

PUT _index_template/comp-test
{
  "index_patterns": ["comp*"],
  "priority": 100,
  "composed_of": ["component001", "component002"]
}

        创建索引comp001并查看索引,可以在结果中看到两个组件模板中的配置信息

PUT comp001
GET comp001


{
  "comp001" : {
    "aliases" : {
      "mycomp" : { }
    },
    "mappings" : {
      "properties" : {
        "date" : {
          "type" : "date",
          "format" : "[yyy-MM-dd HH:mm:ss]"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1698129925357",
        "number_of_shards" : "3",
        "number_of_replicas" : "1",
        "uuid" : "2CT06CcGTBmHsUFIrmICkQ",
        "version" : {
          "created" : "7090199"
        },
        "provided_name" : "comp001"
      }
    }
  }
}

3.4、删除组件模板

        删除组件模板时,需要先删除相关的索引模板,否则会报错,删除顺序如下

DELETE _index_template/comp-test
DELETE _component_template/component001
DELETE _component_template/component002

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值