ES添加新的模板

curl -XPUT elastic:changeme@192.168.232.141:9200/_template/nginx -d '
{
    "template" : "nginx*",
    "order": 0, 
    "index_patterns": [
      "nginx*" 
    ], 
    "settings" : {
      "refresh_interval": "30s",
      "merge.policy.max_merged_segment": "1000mb",
      "translog.durability": "async",
      "translog.flush_threshold_size": "2gb",
      "translog.sync_interval": "100s",
      "index" : {
        "number_of_shards" : "11",
        "number_of_replicas" : "0"
      }
    },
    "mappings" : {
      "_default_" : {
        "_all" : {
          "enabled" : true,
          "norms" : false
        },
        "dynamic_templates" : [
          {
            "message_field" : {
              "path_match" : "message",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "text",
                "norms" : false
              }
            }
          },
          {
            "string_fields" : {
              "match" : "*",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "text",
                "norms" : false,
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 1024
                  }
                }
              }
            }
          }
        ],
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "keyword"
          },
          "url_args": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "keyword"
              },
              "value": {
                "type": "keyword"
              }
            }
          }
      }
      }
    }
}

在es服务器上执行上面,根据需求修改模板信息

查询模板可以看到test已经生成
curl -XGET http://elastic:changeme@192.168.232.141:9200/_cat/templates

template_1              zhouls*                  0          
test                    logstash-*               0          50001
logstash                logstash-*               0          50001
.ml-anomalies-          .ml-anomalies-*          0          5060899
.ml-notifications       .ml-notifications        0          5060899
.monitoring-alerts      .monitoring-alerts-6     0          5050099
.ml-meta                .ml-meta                 0          5060899
template_2              test*                    0          
triggered_watches       .triggered_watches*      2147483647 
.ml-state               .ml-state                0          5060899
security_audit_log      .security_audit_log*     2147483647 
.monitoring-logstash    .monitoring-logstash-6-* 0          5050099
security-index-template .security                1000       
watches                 .watches*                2147483647 
.monitoring-kibana      .monitoring-kibana-6-*   0          5050099
.monitoring-es          .monitoring-es-6-*       0          5050099
.watch-history-6        .watcher-history-6*      2147483647 


查看test模板信息
curl -XGET http://elastic:changeme@192.168.232.141:9200/_template/test


说明:  (1)模板template_2匹配所有的以test开头的索引。
    (2)索引模板是template_2,索引是test*。
  
  
    
删除模板
curl -XDELETE http://elastic:changeme@192.168.232.141:9200/_template/template_1
### 如何在 Elasticsearch 中添加索引模板 要在 Elasticsearch 中添加索引模板,可以通过 HTTP 请求的方式向集群发送 JSON 配置文件。以下是详细的说明以及示例代码。 #### 添加索引模板的请求结构 通过 `PUT` 方法将索引模板定义提交到 `_index_template` 或 `_template` API 端点。以下是一个典型的索引模板配置: ```json { "name": "order-template", "index_patterns": ["orders-*"], "priority": 100, "version": 3, "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "properties": { "customer_id": { "type": "keyword" }, "order_date": { "type": "date" } } }, "aliases": { "all-orders": {} } }, "_meta": { "description": "this is order-template" } } ``` 此配置中: - **`index_patterns`** 定义了哪些索引会应用该模板,支持通配符模式[^3]。 - **`priority`** 设置模板优先级,数值越大优先级越高。 - **`template.settings`** 和 **`template.mappings`** 分别指定索引的基础设置和字段映射规则[^1]。 - **`_meta`** 可选部分,用于存储额外的信息或描述。 #### 使用 cURL 发送请求 以下是如何使用 cURL 命令来创建索引模板的示例: ```bash curl -X PUT "http://localhost:9200/_index_template/order-template" -H 'Content-Type: application/json' -d' { "index_patterns": ["orders-*"], "priority": 100, "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "properties": { "customer_id": { "type": "keyword" }, "order_date": { "type": "date" } } } }, "_meta": { "description": "Template for orders indices" } }' ``` 执行以上命令后,如果成功,则返回状态码为 `200 OK` 并附带确认消息。 #### 查询已存在的索引模板 要查看当前 Elasticsearch 实例中存在的索引模板及其详情,可以运行如下命令: ```bash curl -X GET "http://localhost:9200/_index_template/" ``` 这将列出所有可用的索引模板及其具体配置信息。 --- ### 注意事项 1. 自 Elasticsearch 7.x 版本起,移除了多类型映射的支持,因此不再推荐在同一索引下定义多个文档类型[^5]。 2. 如果需要定期自动创建新索引(例如按日期分区),则应提前规划好相应的索引命名约定,并利用索引生命周期管理 (ILM) 功能配合索引模板实现自动化操作[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值