# Dynamic Template not working

**URL:** <https://discuss.elastic.co/t/dynamic-template-not-working/185247>\
**Category:** Elasticsearch\
**Created:** [June 11, 2019, 5:19pm UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247 "2019-06-11T17:19:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaya](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jaya](https://discuss.elastic.co/u/jaya)\
**Post date:** [June 11, 2019, 5:19pm UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/1 "2019-06-11T17:19:44Z")

</div>

I want to apply custom analyzer to all fields in an existing mapping to enable search with special characters.  
I created custom ngram analyzer and a dynamic template to apply custom\_analyzer on all text fields to do so.  
Search for simple text without any special character works fine But when I search for any special character within \_category field , it is returning 0 results.

To test for custom\_analyzer I created a new field(email) in the mapping with analyzer and search of special characters on this email field giving me expected results.  
I also created a new field(phone No) without analyzer to check if dynamic\_template apply my custom\_analyzer to that phone field , But I am not getting expected results.

Please suggest how dynamic\_templates work? If we can apply dynamic\_templates to existing mappings or if to a new field in existing mapping?

[http://localhost:9200/audit/\_mapping](http://localhost:9200/audit/_mapping)

"audit": {  
"mappings": {  
"logs": {  
"dynamic\_templates": [  
{  
"ngram\_string\_analyzers": {  
"match": "\*",  
"match\_mapping\_type": "string",  
"mapping": {  
"analyzer": "custom\_analyzer",  
"type": "text"  
}  
}  
}  
],  
"properties": {  
"\_category": {  
"type": "text",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
}  
},  
"\_createdOn": {  
"type": "date"  
}  
}  
}  
}

}

"audit": {  
"settings": {  
"index": {  
"number\_of\_shards": "5",  
"provided\_name": "audit",  
"creation\_date": "1556778930604",  
"analysis": {  
"analyzer": {  
"custom\_analyzer": {  
"type": "custom",  
"tokenizer": "my\_tokenizer"  
}  
},  
"tokenizer": {  
"my\_tokenizer": {  
"type": "ngram",  
"min\_gram": "1",  
"max\_gram": "10"  
}  
}  
},  
"number\_of\_replicas": "1",  
"version": {  
"created": "6040099"  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 12, 2019, 1:03am UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/2 "2019-06-12T01:03:38Z")

</div>

Hi,

> [@jaya](#):
>
> Please suggest how dynamic\_templates work? If we can apply dynamic\_templates to existing mappings or if to a new field in existing mapping?

I think the document is explicit and can help you to get the concept.

> **[Dynamic templates | Elasticsearch Guide \[7.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.1/dynamic-templates.html)**

About phone number mapping here a part of what I use but there's some specific things related to Japanese language, I tried to clean but just to get the concept, I copied it from somewhere but can't found back the source.

```
{
    "index_patterns" : "appli1_*",
    "order" : 1,
    "settings" : {
      "analysis" : {
            "filter" : {
"phone_number": {
                  "type": "pattern_capture",
                  "preserve_original": true,
                  "patterns": [
                    "\\d*"
                  ]
                },
                "smallest_digit": {
                  "type": "length",
                  "min": 6
                },
                "not_empty": {
                  "type": "length",
                  "min": 1
                }
            },
  "digits_only": {
                "type": "pattern_replace",
                "pattern": "[^\\d]"
              }
            },
"analyzer" : {
"my_phone_number_analyzer" : {
                  "char_filter": ["icu_normalizer", "digits_only"],
                    "tokenizer": "YOUR TOKENIZER",
                    "filter": ["phone_number", "smallest_digit"]
                },
                "my_phone_number_search_analyzer" : {
                  "char_filter": ["icu_normalizer", "digits_only"],
                  "tokenizer" : "YOUR TOKENIZER",
                  "filter" : ["not_empty"]
                }

```

In my case I call all my fields that have phone number with a \_number suffix

```
"mappings" : {
        "doc" : {
            "dynamic_templates" : [
{"phone_number": {
                  "match" : "*_number",
                  "mapping" : {
                    "type" : "text",
                    "fields":{
                      "untouched":{
                        "type": "keyword",
                        "ignore_above": 256
                      },
                      "my_phone_number_analyzer":{
                        "type": "text",
                        "analyzer": "my_phone_number_analyzer",
                        "search_analyzer" : "my_phone_number_search_analyzer"
                      },
                      "my_ngram_analyzer":{
                        "type": "text",
                        "analyzer": "my_ngram_analyzer"
                      }
                    }
                  }
                }}

```

Note that if you use elastic \>= 7 you need to remove the "doc" key as the type no more exist. (Not fully sure as I didn't migrate yet but you may get error if you copy paste).

Found back the source it's about US number as I said before I adapt it for Japanese so depend on your case maybe you 'll better to start from the original blog post:

> **[Analyzing U.S. Phone Numbers In Elasticsearch](https://khalidabuhakmeh.com/elasticsearch-indexing-of-us-phone-numbers)**
>
> Working with Elasticsearch and United States phone numbers can be tricky. Learn how to write analyzers that give users the best search results every time.

Hope it help.

---

<div class="post-metadata">

**Author:** ![jaya](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jaya](https://discuss.elastic.co/u/jaya)\
**Post date:** [June 12, 2019, 4:58pm UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/3 "2019-06-12T16:58:43Z")

</div>

Hi Gabriel,

Thanks for your quick reply.  
But phone No field is just an example, I have around 20 text fields that can have special characters in values. So I need to apply one custom analyzer to all fields. Also My mapping is dynamic and any text field can be added later also. So I want to make sure that each new field will also contain my custom analyzer.  
The only way I know is using dynamic templates and it is not working in my case. So, please help me if there is any other way to implement a custom analyzer on all text fields or if you can help me in dynamic templates.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 13, 2019, 1:02am UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/4 "2019-06-13T01:02:30Z")

</div>

Sorry I didn't get your problem, let me try again.

You create a field email something like "[aaa@aa.com](mailto:aaa@aa.com)" which is working because it's a string and in your template you defined

```
"ngram_string_analyzers": {
"match": "*",
"match_mapping_type": "string",

```

the mapping apply, no problem BUT

> [@jaya](#):
>
> I also created a new field(phone No) without analyzer to check if dynamic\_template apply my custom\_analyzer to that phone field , But I am not getting expected results.

this field is certainly 00112313213 \<-- which is not a string, but you didn't tell which mapping apply so I try to guess and it's certainly something like long.

So if you try to remove the "match\_mapping\_type": "string", it will apply to all your fields if it's what you want?

---

<div class="post-metadata">

**Author:** ![jaya](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jaya](https://discuss.elastic.co/u/jaya)\
**Post date:** [June 13, 2019, 4:22am UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/5 "2019-06-13T04:22:37Z")

</div>

> [@gabriel\_tessier](#):
>
> this field is certainly 00112313213 \<-- which is not a string,

Hi Gabriel,

My issue got resolved after changing the mapping of dynamic template to -  
"dynamic\_templates": [  
{  
"ngram\_string\_analyzers": {  
"match": "\*",  
"match\_mapping\_type": "string",  
"mapping": {  
"analyzer": "custom\_analyzer",  
"fields": {  
"keyword": {  
"type": "keyword",  
"ignore\_above": 256  
}  
},  
"type": "text"  
}  
}  
}  
]

---

<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 11, 2019, 4:22am UTC](https://discuss.elastic.co/t/dynamic-template-not-working/185247/6 "2019-07-11T04:22:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
