美文网首页
Index Templage 和 Dynamic Templat

Index Templage 和 Dynamic Templat

作者: 侧耳倾听y | 来源:发表于2021-11-21 10:10 被阅读0次

Index Templage

设定Mappings和Settings,并按照一定的规则,自动匹配到新创建的索引之上。也就是新创建索引时,不需要显示设置Mappings和Setting。

  • 模板仅在一个索引被创建时,才会产生作用。修改模板不会影响已创建的索引;
  • 可以设定多个索引模板,这些设置会被“merge”在一起;
  • 可以指定“order”的数值,控制“merging”的过程。

工作方式

当一个索引被创建时:

  1. 应用Elasticsearch默认的settings 和 mappings;
  2. 应用order数值低的Index Template中的设定;
  3. 应用order高的Index Template中的设定,之前的设定会被覆盖;
  4. 应用创建索引时,用户所指定的Settings和Mappings,并覆盖之前模板中的设定。

优先级:用户指定 > order高 > order低 > ES默认

demo

1.创建 两个template-> 2.查看template-> 3.创建一个能被模板匹配的索引并查看

  1. 创建Index Template
# 匹配所有索引,设置主分片为1,副本分片为1
PUT _template/template_default
{
  "index_patterns": ["*"],
  "order" : 0,
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas":1
  }
}
# 匹配名字为test开头的索引,
PUT /_template/template_test,设置主分片为1,副本分片为1,关闭日期推测,打开数字推测
{
    "index_patterns" : ["test*"],
    "order" : 1,
    "settings" : {
        "number_of_shards": 1,
        "number_of_replicas" : 2
    },
    "mappings" : {
        "date_detection": false,
        "numeric_detection": true
    }
}
  1. 查看template
# 查看名为template_default的模板
GET /_template/template_default
# 查看名字以temp开头的模板
GET /_template/temp*
  1. 创建索引
PUT testmy
{
    "settings":{
        "number_of_replicas":5
    }
}
# 查看索引
GET testmy/_mapping
GET testmy/_settings

Dynamic Template

应用在某个索引上面,根据Elasticsearch识别的数据类型,结合字段名称,来动态设定字段类型。

例如:

  1. 所有字符串类型都设定成keyword,或者关闭keyword字段;
  2. is开头的字段都设置成boolean;
  3. long开头的字段都设置成long类型。

设置Dynamic Template:

  • Template 有一个名称;
  • 匹配规则是一个数组;
  • 为匹配到字段设置Mapping。

demo

# 类型为string设置字段为keyword类型,类型为string且名字以字段名is开头,设置字段为boolean类型
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
            {
        "strings_as_boolean": {
          "match_mapping_type":   "string",
          "match":"is*",
          "mapping": {
            "type": "boolean"
          }
        }
      },
      {
        "strings_as_keywords": {
          "match_mapping_type":   "string",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}
# 匹配字段名为name.*的字段,不匹配字段名为*.middle的字段
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}

Search Templage

  • 新建一个Search Templage
POST _scripts/tmdb
{
  "script": {
    "lang": "mustache",
    "source": {
      "_source": [
        "title"
      ],
      "size": 20,
      "query": {
        "multi_match": {
          "query": "{{q}}",
          "fields": ["title"]
        }
      }
    }
  }
}
  • 查询Search Templage
GET _scripts/tmdb
  • 使用 Search Templage
POST titles/_search/template
{
    "id":"tmdb",
    "params": {
        "q": "barking"
    }
}

相关文章

网友评论

      本文标题:Index Templage 和 Dynamic Templat

      本文链接:https://www.haomeiwen.com/subject/uscutrtx.html