# Dynamic mappings using dynamic templates

**URL:** https://discuss.elastic.co/t/dynamic-mappings-using-dynamic-templates/14773
**Category:** Elasticsearch
**Created:** [December 9, 2013, 4:39pm UTC](https://discuss.elastic.co/t/dynamic-mappings-using-dynamic-templates/14773 "2013-12-09T16:39:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Nicka](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nicka](https://discuss.elastic.co/u/Nicka)
#### Post date: [December 9, 2013, 4:39pm UTC](https://discuss.elastic.co/t/dynamic-mappings-using-dynamic-templates/14773/1 "2013-12-09T16:39:11Z")

</div>

Hi,

I am trying to use dynamic mapping where I want to use dynamic templates.

But it looks that if I have dynamic field name and value name , ES doesn't  
create the mapping for this. It works when I have field as String, Number.  
A problem is coming when I have field like json object (example: field  
details)

{

"id": "abcd",

"details: {

"description": "text",

"number": 123

}

}

What I'm doing is:

/_**Step1  
//I'm creating the index with the  
settings:  
//**_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

$ curl -XPOST "[http://localhost:9200/nicka/](http://localhost:9200/nicka/)" -d'

{

"settings":{

```
"number_of_shards":3,

"number_of_replicas":1

,"more":{},

"index" : {

```

"analysis" : {

"analyzer" : {

"autocomplete" : {

"tokenizer" : "whitespace",

"filter" : ["lowercase", "engram"]

}

},

"filter" : {

"engram" : {

"type" : "edgeNGram",

"min\_gram" : 3,

"max\_gram" : 10

}

}

}

}

}

}'

//**Step2  
//_Define mapping:  
//_**\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

$ curl -XPOST "[http://localhost:9200/nicka/items/\_mapping](http://localhost:9200/nicka/items/_mapping)" -d'

{

"items" : {

```
"dynamic_templates" : [

    {

        "template_level_0" :{

            "match" : "id*",

            "match_mapping_type" : "string",

            "mapping" : {

                "type" : "string",

                "index" : "not_analyzed"

            }

        }

    },

    {

        "template_level_1" : {

"match" : "*",

```

"mapping" : {

"type" : "multi\_field",

"fields" : {

"{name}" : {

```
            "type" : "{dynamic_type}",

            "index" : "analyzed",

            "store": "yes",

            "index_analyzer" : "autocomplete",

            "search_analyzer" : "standard"

},

"original":{"type" : "string", "index":"not_analyzed"}

```

}

}

}

```
    }

```

]

}

}'

/\*\*\*\*\*\*\*\*_ **Step3** _  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\ ***Create document:**

* * *

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

$ curl -XPUT "[http://localhost:9200/nicka/items/1](http://localhost:9200/nicka/items/1)" -d'

> {

"id" : "123-324-abcde",

```
  "number" : 1 234567,

```

"details" : {

```
 "description" : "some example text for document",

  "name" : "document 1",

  "number" : 1234567,

  "driver" : "John Smith"

```

},

"tags" : ["document", "driver", "car"]

}'

{"ok":true,"\_index":"nicka","\_type":"items","\_id":"1","\_version":1}

1. PROBLEM: When I am checking the mapping I have result this:

$ curl -XGET "[http://localhost:9200/nicka/items/\_mapping?pretty](http://localhost:9200/nicka/items/_mapping?pretty)"

{

"items" : {

```
"dynamic_templates" : [ {

  "template_level_0" : {

    "mapping" : {

      "type" : "string",

      "index" : "not_analyzed"

    },

    "match" : "id*",

    "match_mapping_type" : "string"

  }

}, {

  "template_level_1" : {

    "mapping" : {

      "type" : "multi_field",

      "fields" : {

        "{name}" : {

          "type" : "{dynamic_type}",

          "index" : "analyzed",

          "store" : "yes",

          "index_analyzer" : "autocomplete",

          "search_analyzer" : "standard"

        },

        "original" : {

          "type" : "string",

          "index" : "not_analyzed"

        }

      }

    },

    "match" : "*"

  }

} ],

"properties" : { }

```

}

}

As you see the properties is empty so it is not correct. When I'm create  
simple document key-value I am able to see all mapping for these  
relationships in this place: "properties".

/\*\*\*\*\*\*\*\*_ **Step4** _  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\ ***Search for test:**

* * *

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

When I'm trying to finde documet using filed: details.name I got correct  
result:

$ curl -XGET "[http://localhost:9200/nicka/items/\_search?pretty](http://localhost:9200/nicka/items/_search?pretty)" -d '

{

"query" : {

"term" : { "details.name" : "document" }

}

}'

{

"took" : 1,

"timed\_out" : false,

"\_shards" : {

```
"total" : 3,

"successful" : 3,

"failed" : 0

```

},

"hits" : {

```
"total" : 1,

"max_score" : 0.30685282,

"hits" : [ {

  "_index" : "nicka",

  "_type" : "items",

  "_id" : "1",

  "_score" : 0.30685282, "_source" :

```

{

"id" : "123-324-abcde",

"details" : {

```
 "description" : "some example text for document",

 "name" : "document",

 "number" : 1234567,

 "driver" : "Nicka"

```

},

"tags" : ["document", "driver", "car"]

}

```
} ]

```

}

}

Could you check this and tell me why I have this error?  
If I wrote something what is not clear, please let me know. I will try to  
explain more.

Thanks a lot!  
Nicka

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/652c9f1d-80e6-437c-8720-68759e9282ca%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/652c9f1d-80e6-437c-8720-68759e9282ca%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![abhishek5678](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abhishek5678](https://discuss.elastic.co/u/abhishek5678)
#### Post date: [May 5, 2017, 4:35am UTC](https://discuss.elastic.co/t/dynamic-mappings-using-dynamic-templates/14773/2 "2017-05-05T04:35:25Z")

</div>

Hi Nicka,  
I want to perform dynamic mapping with three level nested data but I am not able to get.If you knows about that then please help me.I am sending my mapping file and [data.so](http://data.so) In this way i am not able to get the correct mapping.help me to putting the correct mapping with my data.  
INDEX:

PUT /icd10\_codes1  
{  
"settings": {  
"number\_of\_shards": 1,  
"analysis": {  
"filter": {  
"autocomplete\_filter": {  
"type": "edge\_ngram",  
"min\_gram": 1,  
"max\_gram": 20  
}  
},  
"analyzer": {  
"autocomplete": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"autocomplete\_filter"  
]  
}  
}  
}  
}  
}

MAPPING:

PUT /icd10\_codes1/\_mapping/my\_type  
{  
"properties": {  
"name": {  
"type": "string",  
"analyzer": "autocomplete"  
},  
"depth":{  
"type":"long"  
},  
"dynamic\_templates": [{  
"nested\_children": {  
"match": "children",  
"match\_mapping\_type": "string",  
"mapping": {  
"index": "analyzed",  
"analyzer": "autocomplete"  
}  
}  
}],  
"dynamic\_templates": [{  
"nested\_children.children": {  
"match": "children.children",  
"match\_mapping\_type": "string",  
"mapping": {  
"index": "analyzed",  
"analyzer": "autocomplete"  
}  
}  
}],  
"dynamic\_templates": [{  
"nested\_children.children.children": {  
"match": "children.children.children",  
"match\_mapping\_type": "string",  
"mapping": {  
"index": "analyzed",  
"analyzer": "autocomplete"  
}  
}  
}]

```
  }
}

```

DATA:

POST /icd10\_codes1/my\_type/1  
{  
"name": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism (D50-D89)",  
"depth": 1,  
"children": [{  
"name": "Nutritional anemias (D50-D53)",  
"depth": 2,  
"children": [{  
"code": "D50",  
"name": "Iron deficiency anemia",  
"depth": 3,  
"children": [{  
"code": "D50.0",  
"name": "Iron deficiency anemia secondary to blood loss (chronic)",  
"depth": 4  
}, {  
"code": "D50.1",  
"name": "Sideropenic dysphagia",  
"depth": 4  
}, {  
"code": "D50.8",  
"name": "Other iron deficiency anemias",  
"depth": 4  
}, {  
"code": "D50.9",  
"name": "Iron deficiency anemia, unspecified",  
"depth": 4  
}]  
}]  
}]  
}

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:00pm UTC](https://discuss.elastic.co/t/dynamic-mappings-using-dynamic-templates/14773/3 "2017-07-05T22:00:33Z")

</div>


