Elasticsearch学习---Index Templates(索引模板)

前言

索引模板的功能可以让你在创建索引时依据自定义模板的规则来生成,这样可以有效的帮助你管理索引,模板内容可以包含setting和mapping,以及控制哪些索引需要按照模板生成,哪些不需要。

  • 模板仅仅在索引被创建时生效,修改模板不会影响已经被创建的索引
  • 当使用正常创建索引API时,作为创建索引调用一部分定义的setting/mapping将优先于模板中定义的任何匹配setting/mapping。

创建索引模板

定义一个名为template_1的模板,setting和mapping将应用于匹配te或bar模式的任何索引名。

模板定义主分片和副本分片都为1,mapping设置了source为不可见,并且分别约定了host_name和created_at两个属性的type和format。

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas":1
  },
  "mappings": {
    "_doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

创建一个名为test的索引,符合te*的规则

PUT /test/
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test"
}

查看索引信息

GET /test/
{
  "test": {
    "aliases": {},
    "mappings": {
      "_doc": {
        "_source": {
          "enabled": false
        },
        "properties": {
          "created_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "host_name": {
            "type": "keyword"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1622597378559",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "7l-6e-rxSOC2RCeTNciWmg",
        "version": {
          "created": "6040099"
        },
        "provided_name": "test"
      }
    }
  }
}

插入一条文档

PUT /test/_doc/1
{
  "created_at":"2020-01-01 00:00:00"
}
{
  "_index": "test",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

查看索引信息

GET /test/_doc/_search
{
  "query": {
    "match_all": {}
  }
}

source信息不可见

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_score": 1
      }
    ]
  }
}

查看模板信息

GET /_template/template_1
{
  "template_1": {
    "order": 0,
    "index_patterns": [
      "te*",
      "bar*"
    ],
    "settings": {
      "index": {
        "number_of_shards": "1",
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "_doc": {
        "_source": {
          "enabled": false
        },
        "properties": {
          "host_name": {
            "type": "keyword"
          },
          "created_at": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          }
        }
      }
    },
    "aliases": {}
  }
}

判断模板是否存在

HEAD _template/template_1
200 - OK

删除模板

DELETE /_template/template_1
{
  "acknowledged": true
}

再判断是否存在,返回404

404 - Not Found

多索引文档匹配

当有多个索引都匹配到了索引模板时,可以利用order来控制具体应用的模板,order大的会覆盖order小的。

举个例子,template_1指定source不可见,template_2指定可见


PUT /_template/template_1
{
    "index_patterns" : ["*"],
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "_source" : { "enabled" : false }
        }
    }
}

PUT /_template/template_2
{
    "index_patterns" : ["te*"],
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "_source" : { "enabled" : true }
        }
    }
}

创建并查询mytest索引,source不可见

PUT /mytest/

GET /mytest/
{
  "mytest": {
    "aliases": {},
    "mappings": {
      "_doc": {
        "_source": {
          "enabled": false
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1622599058715",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "CLZtSzAVQbWq7j9ILNoizg",
        "version": {
          "created": "6040099"
        },
        "provided_name": "mytest"
      }
    }
  }
}

创建并查看test索引,source可见

PUT /test/

GET /test/
{
  "test": {
    "aliases": {},
    "mappings": {
      "_doc": {
        "properties": {
          "host_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1622599082908",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "Pc3oZ3cAQuWjDSeV-nyPVg",
        "version": {
          "created": "6040099"
        },
        "provided_name": "test"
      }
    }
  }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值