elasticsearch 索引模版

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

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

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

html 复制代码
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已经存在时,再次编辑模板配置并发送上述请求可以修改模板的内容。如果想查询索引模板的信息,可以使用以下代码。

html 复制代码
GET /_index_template/service-template

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

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

html 复制代码
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的映射结果如下。

html 复制代码
{
  "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

html 复制代码
PUT _component_template/comp1
{
  "template": {
    "mappings": {
      "properties": {
        "content": {
          "type": "text"
        }
      }
    }
  }
}

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

html 复制代码
PUT _component_template/comp2
{
  "template": {
    "settings": {
      "number_of_shards": "3",
      "number_of_replicas": "2"
    },
    "aliases": {
      "loginfo": {}
    }
  }
}

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

html 复制代码
PUT _index_template/infotmp
{
  "index_patterns": ["loginfo*"],
  "priority": 200,
  "composed_of": ["comp1", "comp2"]
}

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

html 复制代码
PUT loginfo1
GET loginfo1

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

html 复制代码
{
  "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"
      }
    }
  }
}

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

相关推荐
Elasticsearch9 小时前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
Elasticsearch1 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
武子康1 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康2 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库3 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟3 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人3 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法
计算机程序猿学长3 天前
大数据毕业设计-基于django的音乐网站数据分析管理系统的设计与实现(源码+LW+部署文档+全bao+远程调试+代码讲解等)
大数据·django·课程设计
B站计算机毕业设计超人3 天前
计算机毕业设计Django+Vue.js音乐推荐系统 音乐可视化 大数据毕业设计 (源码+文档+PPT+讲解)
大数据·vue.js·hadoop·python·spark·django·课程设计