Elasticsearch 介绍 Elasticsearch 7:快速上手 Elasticsearch 7:关于 Index、Type、Document Elasticsearch 7:安装与启动 Elasticsearch 7:Kibana 的使用 Elasticsearch 7:下载历史版本 Elasticsearch 7:文档唯一性 Elasticsearch 7:默认端口与端口设置 Elasticsearch 7:创建和删除索引 Elasticsearch 7:自定义 mapping 和 settings Elasticsearch 7:设置索引副本数量和分片数量 Elasticsearch 7:查看所有索引 Elasticsearch 7:数据类型 Elasticsearch 7:字符串类型 keyword 、text Elasticsearch 7:数组 Elasticsearch 7:添加和更新文档 Elasticsearch 7:通过 _bulk 批量添加文档 Elasticsearch 7:使用 from 、size 进行分页查询 Elasticsearch 7:查询中使用 sort 进行排序 Elasticsearch 7:查询结果只展示部分字段 Elasticsearch 7:查询结果中展示 _version 字段 Elasticsearch 7:使用 ignore_above 限制字符串长度 Elasticsearch 7:动态映射 Elasticsearch 7:doc_values 属性 Elasticsearch 7:刷新周期 refresh_interval Elasticsearch 7:使用 _refresh 刷新索引 Elasticsearch 7:分片(shard)限制 Elasticsearch 7:使用 _cat thread_pool 查询线程池运行情况 Elasticsearch 7:事务日志 translog Elasticsearch 7:文档 _id 的长度限制 Elasticsearch 7:分片 shard Elasticsearch 7:滚动查询 Elasticsearch 7:聚合查询 Elasticsearch 7:索引模板 Elasticsearch 7:获取文档所属的 shard Elasticsearch 7:获取版本号 Elasticsearch 7:获取指定 shard 中的文档 Elasticsearch 7:获取 shard 统计信息 Elasticsearch 7:搜索实战 Elasticsearch 7:Python 客户端 Elasticsearch 7:Java TransportClient API 客户端 Elasticsearch 7:Java REST Client API 客户端 Elasticsearch:将 SQL 转换为 DSL Elasticsearch 6 快速上手 Elasticsearch 5 快速上手 Elasticsearch 5:禁止自动创建索引 Elasticsearch 5:禁止动态增加字段 Elasticsearch 产品版本支持周期 基于 Elasticsearch 的站内搜索引擎实战

Elasticsearch 7:索引模板


#Elasticsearch


介绍

在新建索引/自动新建索引时,若索引名符合索引模板的条件,则会按照模板创建模板。

相关官方文档:

命令示例

查询所有模板信息:

GET _index_template

查询指定模板信息:

GET _index_template/template_1

GET _index_template/template*

删除模板:

DELETE /_index_template/template_1

操作实战1:基础示例

创建模板,匹配 stu 开头的索引:

PUT /_index_template/template_1
{
  "index_patterns" : ["stu*"],
  "template": {
    "mappings" : {
      "properties" : {
        "name" : { "type" : "keyword" },
        "age" : { "type" : "integer" }
      }
    },
    "settings" : {
      "number_of_shards" : 2
    }
  }
}

查询所有模板信息:

GET _index_template

创建索引时会自动匹配模板:

# 创建索引
PUT student

# 查询索引结构
GET student
# 查询结果
{
  "student" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "name" : {
          "type" : "keyword"
        }
      }
    },
    "settings" : {
        
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "student",
        "creation_date" : "1634356238830",
        "number_of_replicas" : "1",
        "uuid" : "DiR_5wuNQEqoMjVzRXN_RQ",
        "version" : {
          "created" : "7150099"
        }
      }
    }
  }
}

如果创建索引时,指定的部分数据属性和配置,则优先使用。命中的模板会作为补充: ​

PUT student_2
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "text"
      },
      "height" : {
        "type" : "long"
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 2
    }
  }
}


# 查询索引结构
GET student_2
# 查询结果
{
  "student_2" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"   # 通过模板增加该字段
        },
        "height" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",   // 没有使用模板指定的2
        "provided_name" : "student_2",
        "creation_date" : "1634518830185",
        "number_of_replicas" : "2",
        "uuid" : "QD9n3uPcSiud4d3fqayZOg",
        "version" : {
          "created" : "7150099"
        }
      }
    }
  }
}

删除模板:

# 请求
DELETE /_index_template/template_1

# 响应
{
  "acknowledged" : true
}

操作实战2:模板优先级

如果索引名,匹配了两个模板,会如何? ​

我们创建两个模板:

PUT /_index_template/template_1
{
  "index_patterns" : ["stu*"],
  "template": {
    "mappings" : {
      "properties" : {
        "name" : { "type" : "keyword" },
        "age" : { "type" : "integer" }
      }
    },
    "settings" : {
      "number_of_shards" : 2
    }
  }
}
PUT /_index_template/template_2
{
  "index_patterns" : ["stud*"],
  "template": {
    "mappings" : {
      "properties" : {
        "age" : { "type" : "long" },
        "height" : { "type" : "long" }
      }
    },
    "settings" : {
      "number_of_shards" : 3
    }
  }
}

// 报错如下:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "index template [template_2] has index patterns [stud*] matching patterns from existing templates [template_1] with patterns (template_1 => [stu*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "index template [template_2] has index patterns [stud*] matching patterns from existing templates [template_1] with patterns (template_1 => [stu*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
  },
  "status" : 400
}

如果 index_patterns 有相同的前缀,要指定 **priority****priority**** 默认值为0,代表最低优先级。创建索引时,使用最高优先级的模板。** ​

下面的方式创建,则没有问题:

PUT /_index_template/template_2
{
  "index_patterns" : ["stud*"],
  "priority": 1,
  "template": {
    "mappings" : {
      "properties" : {
        "age" : { "type" : "long" },
        "height" : { "type" : "long" }
      }
    },
    "settings" : {
      "number_of_shards" : 3
    }
  }
}

创建索引:

PUT student

查询索引结构: ​

{
  "student" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "height" : {
          "type" : "long"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "3",
        "provided_name" : "student",
        "creation_date" : "1634606600477",
        "number_of_replicas" : "1",
        "uuid" : "6tjI9CKAQGWGKpA3a1NFKQ",
        "version" : {
          "created" : "7150099"
        }
      }
    }
  }
}

操作实例3:更新索引/仅创建索引

使用 PUT /_index_template/<template_name> 时,若之前模板已存在则会覆盖。 使用 PUT /_index_template/<template_name>?create 时,若之前模板已存在则会报错。 ​

示例:

# 创建
PUT /_index_template/template_1
{
  "index_patterns" : ["stu*"],  # 匹配 stu 开头的索引
  "template": {
    "mappings" : {
      "properties" : {
        "name" : { "type" : "keyword" },
        "age" : { "type" : "integer" }  # 类型是 integer
      }
    },
    "settings" : {
      "number_of_shards" : 2
    }
  }
}

GET /_index_template/template_1
# 查询结果:
{
  "index_templates" : [
    {
      "name" : "template_1",
      "index_template" : {
        "index_patterns" : [
          "stu*"
        ],
        "template" : {
          "settings" : {
            "index" : {
              "number_of_shards" : "2"
            }
          },
          "mappings" : {
            "properties" : {
              "name" : {
                "type" : "keyword"
              },
              "age" : {
                "type" : "integer"
              }
            }
          }
        },
        "composed_of" : [ ]
      }
    }
  ]
}

# 再次创建
PUT /_index_template/template_1
{
  "index_patterns" : ["stud*"],      # 匹配  stud 开头的索引
  "template": {
    "mappings" : {
      "properties" : {
        "name" : { "type" : "keyword" },
        "age" : { "type" : "long" }  # 类型是 long ,和之前不一样
      }
    },
    "settings" : {
      "number_of_shards" : 2
    }
  }
}

// 再次查看
GET /_index_template/template_1

// 查询结果
{
  "index_templates" : [
    {
      "name" : "template_1",
      "index_template" : {
        "index_patterns" : [
          "stud*"
        ],
        "template" : {
          "settings" : {
            "index" : {
              "number_of_shards" : "2"
            }
          },
          "mappings" : {
            "properties" : {
              "name" : {
                "type" : "keyword"
              },
              "age" : {
                "type" : "long"
              }
            }
          }
        },
        "composed_of" : [ ]
      }
    }
  ]
}

// 追加 ?create 参数创建索引
PUT /_index_template/template_1?create
{
  "index_patterns" : ["stud*"],
  "template": {
    "mappings" : {
      "properties" : {
        "name" : { "type" : "keyword" },
        "age" : { "type" : "long" }
      }
    },
    "settings" : {
      "number_of_shards" : 2
    }
  }
}

# 会报错如下:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "index template [template_1] already exists"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "index template [template_1] already exists"
  },
  "status" : 400
}

组合模板

从 ES 7.8 引入了组合模板。 具体可以参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-templates.html



( 本文完 )