Elasticsearch之索引模板

解决的问题

  当索引类型和配置信息都一样,就可以使用索引模板来处理,不然我们就会手动创建索引。

创建索引模板

PUT _template/2019
{
  "index_patterns": ["20*", "product1*"],   
  "settings":{   
    "number_of_shards": 2,
    "number_of_replicas": 1
  },
  "mappings":{  
    "doc":{
      "properties":{
        "ip":{
          "type":"keyword"
        },
        "method":{
          "type": "keyword"
        }
      }
    }
  }
}

# index_patterns是索引模式,指当创建以20和product1开头的索引时,使用该索引模板
# 在settings设置中,我们自定义为该索引分配3个主分片。复制分片不变
# mappings中指定映射关系

查看索引模板

GET _cat/templates
GET _template
GET _template/2019
GET _template/20*

索引模板的使用

添加数据并且查询模板是否使用上

PUT 20190101/doc/1
{
  "ip": "127.0.0.1",
  "method":"GET"
}

PUT 20190102/doc/2
{
  "ip":"192.168.1.1",
  "method":"POST"
}

PUT product1_log/doc/1
{
  "ip":"127.0.0.1",
  "method":"GET"
}

GET 2019*/doc/_search
{
  "query": {
    "match_all": {}
  }
}

GET 20190101

查询结果模板使用上了

{
  "20190101" : {
    "aliases" : { },
    "mappings" : {
      "doc" : {
        "properties" : {
          "ip" : {
            "type" : "keyword"
          },
          "method" : {
            "type" : "keyword"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1566821645952",
        "number_of_shards" : "2",
        "number_of_replicas" : "1",
        "uuid" : "Tzqx1mKvTmiBMfaOfhQAwg",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "20190101"
      }
    }
  }
}  

多模板匹配

PUT _template/2018_1
{
  "index_patterns": ["2018*"],
  "order":0,
  "settings":{
    "number_of_shards": 2
  }
}


PUT 2018010101/doc/1
{
  "method":"GET"
}
GET 2018010101/_settings

删除模板

DELETE _template/2018*

猜你喜欢

转载自www.cnblogs.com/Alexephor/p/11414741.html