Index Templates API

https://people.mozilla.org/~wkahngreene/elastic/guide/reference/api/admin-indices-templates.html

—- layout: guide title: Index Templates API cat: guide sidebar: reference_api —-

Index templates allow to define templates that will automatically be applied to new indices created. The templates include both settings and mappings, and a simple pattern template that controls if the template will be applied to the index created. For example:

curl -XPUT localhost:9200/_template/template_1 -d '
{
    “template” : “te*”,
    “settings” : {
        “number_of_shards” : 1
    },
    “mappings” : {
        “type1” : {
            “_source” : { “enabled” : false }
        }
    }
}
'

Defines a template named template_1, with a template pattern of te*. The settings and mappings will be applied to any index name that matches thete* template.

Deleting a Template

Index templates are identified by a name (in the above case template_1) and can be delete as well:

curl -XDELETE localhost:9200/_template/template_1

GETting a Template

Index templates are identified by a name (in the above case template_1) and can be retrieved using the following:

curl -XGET localhost:9200/_template/template_1

To get list of all index templates you can use Cluster State API and check for the metadata/templates section of the response.

Multiple Template Matching

Multiple index templates can potentially match an index, in this case, both the settings and mappings are merged into the final configuration of the index. The order of the merging can be controlled using the order parameter, with lower order being applied first, and higher orders overriding them. For example:

curl -XPUT localhost:9200/_template/template_1 -d '{
    “template” : “*”,
    “order” : 0,
    “settings” : {
        “number_of_shards” : 1
    },
    “mappings” : {
        “type1” : {
            “_source” : { “enabled” : false }
        }
    }}'

    curl -XPUT localhost:9200/_template/template_2 -d '{
    “template” : “te*”,
    “order” : 1,
    “settings” : {
        “number_of_shards” : 1
    },
    “mappings” : {
        “type1” : {
            “_source” : { “enabled” : true }
        }
    }}'

The above will disable storing the _source on all type1 types, but for indices of that start with te*, source will still be enabled. Note, for mappings, the merging is “deep”, meaning that specific object/property based mappings can easily be added/overridden on higher order templates, with lower order templates providing the basis.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码看起来是一个使用FastAPI框架构建的Web应用程序。如果您需要对代码进行修改,可以按照您的需求进行更改。以下是一些建议的修改: 1. 导入所需的模块:确保您已经安装了所需的库,例如`fastapi`,`jinja2`等,并在代码的顶部导入它们。 ```python from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates ``` 2. 创建FastAPI实例:根据您的应用程序需求,可以在创建FastAPI实例时添加一些参数,例如标题和描述。 ```python app = FastAPI(title="My App", description="A simple web application") ``` 3. 配置静态文件和模板目录:确保您的静态文件和模板文件位于正确的目录,并在应用程序中进行正确的配置。 ```python app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") ``` 4. 定义路由处理函数:根据您的应用程序需求,可以修改路由的路径和处理函数的逻辑。 ```python @app.get("/", response_class=HTMLResponse) async def index(request: Request): return templates.TemplateResponse("index.html", {"request": request, "containers": containers}) @app.post("/add_container") async def add_container(request: Request, name: str = Form(...), image: str = Form(...)): container = {"name": name, "image": image} containers.append(container) return templates.TemplateResponse("index.html", {"request": request, "containers": containers}) @app.get("/delete_container/{index}") async def delete_container(request: Request, index: int): containers.pop(index) return templates.TemplateResponse("index.html", {"request": request, "containers": containers}) @app.post("/update_container/{index}") async def update_container(request: Request, index: int, name: str = Form(...), image: str = Form(...)): container = {"name": name, "image": image} containers[index] = container return templates.TemplateResponse("index.html", {"request": request, "containers": containers}) ``` 请注意,这只是一些建议的修改,具体的修改取决于您的需求和应用程序的逻辑。希望这能帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值