动态模板的作用就是动态的给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'"
}
本文介绍如何使用Elasticsearch的动态模板功能来自动设置新增字段的属性类型,并通过实例展示了如何创建索引、更新数据。
2398

被折叠的 条评论
为什么被折叠?



