elasticsearch 学习历程之动态模板

动态模板的作用就是动态的给Mapping中定义的类设置属性及类型


1、设置mapping,dynamic_templates 动态的设置新增字段的属性类型

PUT my_index

{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "my_type_dynamic": {
            "path_match": "stash.*", //设置stash 对象里面的数据只能为string,其他类型则会报错。
            "mapping": {
              "type": "string",
         
            "index": "not_analyzed"
            }
          }
        }
      ],
      "dynamic": "strict",
      "properties": {
        "title": {
          "type": "string"
        },
        "stash": {
          "type": "object",
          "dynamic": true
        }
      }
    }
  }
}
 
2、创建索引
POST my_index/my_type/1
{
  "title": "测试动态模板",
  "stash": {
    "new_field": "Success! update"
  }
}
 
3、查看数据
GET  my_index/my_type/1
 
查询得到数据为
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "title": "测试动态模板",
        "stash": {
            "new_field": "Success! update"
        }
    }
}
 
4、更新动态模板数据
  
 POST /my_index/my_type/1/_update
{
	"script" : "ctx._source.stash.new_field = 'value_of_new_field update'"
}
 



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值