本文介绍: elasticsearch 索引模版和组件模版

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

  1. 使用索引模版定制索引结构

假如你想在Elasticsearch中创建两个索引service-log1和service-log2,这两个索引分别记录了不同年份的服务日志数据,它们的映射结构是相同的,也具有相同的分片数和别名。为了实现这一效果,你可以先创建一个索引模板service-template。

PUT _index_template/service-template
{
  "index_patterns": [
    "service-log*"
  ],
  "template": {
    "settings": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "serviceid": {
          "type": "keyword"
        },
        "content": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "created_at": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    },
    "aliases": {
      "service-logs": {}
    }
  },
  "priority": 200,
  "version": 3,
  "_meta": {
    "description": "my custom"
  }
}

在上述的配置中,index_patterns用于设置索引模板可以匹配的索引名,这里配置了所有以service-log开头的索引都会“命中”此模板。该模板还配置了索引的分片数、副本分片数、字段映射和别名。priority用来设置模板的优先级,其值越大优先级越高。version表示版本号,_meta可以保存一些元数据。当模板service-template已经存在时,再次编辑模板配置并发送上述请求可以修改模板的内容。如果想查询索引模板的信息,可以使用以下代码。

GET /_index_template/service-template

当索引名称匹配索引模版中指定的模版pattern时就会自动加载索引模版中的数据, 如果想要覆盖就可以直接添加指定的配置到创建索引时的模板当中去

可以从上述代码运行结果中看到模板中的配置已经自动在service-log1中得到应用。如果你想在索引service-log2中自定义某些配置,可以在创建索引映射的时候指明,这样就能把模板的配置覆盖掉。

PUT service-log2
{
  "settings": {
    "number_of_shards": "3",
    "number_of_replicas": "2"
  },
  "mappings": {
    "properties": {
      "level": {
        "type": "text"
      },
      "serviceid": {
        "type": "long"
      }
    }
  }
}

上述代码在索引service-log2中设置了分片数和每个主分片的副本分片数分别为3和2,添加了一个字段level,又把serviceid字段设置为long类型,运行上述代码后可以发现索引映射中的配置确实成功覆盖掉了索引模板中的配置。查看service-log2的映射结果如下。

{
  "service-log2" : {
    "aliases" : {
      "service-logs" : { }
    },
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "created_at" : {
          "type" : "date",
          "format" : "yyyy-MM-dd HH:mm:ss"
        },
        "level" : {
          "type" : "text"
        },
        "serviceid" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1601965153703",
        "number_of_shards" : "3",
        "number_of_replicas" : "2",
        "uuid" : "0tEAWaSkS3Cqh3LioDJfbA",
        "version" : {
          "created" : "7090199"
        },
        "provided_name" : "service-log2"
      }
    }
  }
}
  1. 使用模版组件简化模版配置

为了简化索引模板中的配置内容,你可以把常规的索引设置、映射等内容写成可复用的模板组件,然后在索引模板中引用这些组件,这样模板中的配置内容就会非常简洁,便于移植和管理。

先创建组件模版 comp1, 具有字段 content

PUT _component_template/comp1
{
  "template": {
    "mappings": {
      "properties": {
        "content": {
          "type": "text"
        }
      }
    }
  }
}

再创建一个组件模板comp2,它配置了别名loginfo,主分片数为3,每个主分片的副本分片数为2。

PUT _component_template/comp2
{
  "template": {
    "settings": {
      "number_of_shards": "3",
      "number_of_replicas": "2"
    },
    "aliases": {
      "loginfo": {}
    }
  }
}

然后创建一个索引模板infotmp,把上述两个组件模板加载到索引模板中,索引模板会匹配所有名称以loginfo开头的索引。

PUT _index_template/infotmp
{
  "index_patterns": ["loginfo*"],
  "priority": 200,
  "composed_of": ["comp1", "comp2"]
}

可以创建一个索引loginfo1然后查看结果。

PUT loginfo1
GET loginfo1

从以下返回结果可以看出索引loginfo1已经获得了两个组件模板中配置的映射、别名和分片数。

{
  "loginfo1" : {
    "aliases" : {
      "loginfo" : { }
    },
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1601967493187",
        "number_of_shards" : "3",
        "number_of_replicas" : "2",
        "uuid" : "zbvp-P-SSAq7hXUGFLl1Aw",
        "version" : {
          "created" : "7090199"
        },
        "provided_name" : "loginfo1"
      }
    }
  }
}

下一章节我们讲解索引的监控

原文地址:https://blog.csdn.net/lyzchengxuyuan/article/details/136018318

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_65649.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注