Elasticsearch 学习笔记

Elasticsearch 可以用于快速地储存、搜索和分析海量数据。这里会将其简称为 ES。大部分DSL在 Kibana Dev Tools 中执行。

👉文章列表
Elasticsearch 7 学习笔记 ⏬ 快速上手 Index、Type、Document 安装与启动 Kibana 的使用 下载历史版本 文档唯一性 默认端口与端口设置 创建和删除索引 自定义 mapping 和 settings 设置索引副本数量和分片数量 查看所有索引 数据类型 字符串类型 keyword 、text 数组 添加和更新文档 通过 _bulk 批量添加文档 使用 from 、size 进行分页查询 查询中使用 sort 进行排序 查询结果只展示部分字段 查询结果中展示 _version 字段 使用 ignore_above 限制字符串长度 动态映射 聚合查询
客户端 ⏬ Python 客户端 Java TransportClient API 客户端 Java REST Client API 客户端
其他 ⏬ ES 6 快速上手 ES 5 快速上手

Elasticsearch 7 : 索引模板


介绍

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

相关官方文档:

命令示例

查询所有模板信息:

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


( 本文完 )

文章目录