动态模板的作用就是动态的给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'"
}