# Regexp in dynamic templates

**URL:** <https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403>\
**Category:** Elasticsearch\
**Created:** [November 19, 2018, 4:43pm UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403 "2018-11-19T16:43:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gerilya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gerilya/32/22317_2.png) [@gerilya](https://discuss.elastic.co/u/gerilya)\
**Post date:** [November 19, 2018, 4:43pm UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403/1 "2018-11-19T16:43:47Z")

</div>

Hello,

I would like to create dynamic mapping so that all strings matching IP address pattern are mapped to IP type and all other strings are mapped to keyword/text.  
I've tried the following index template, but it does not seem to catch IP addresses.

```
{
  "aliases": {},
  "mappings": {
    "doc": {
      "_all": {
        "enabled": false
      },
      "dynamic_templates": [
        {
          "strings": {
            "mapping": {
              "type": "keyword",
              "ignore_above": 256,
              "fields": {
                "raw": {
                  "type": "text"
                }
              }
            },
            "unmatch": "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
            "match_pattern": "regex",
            "match_mapping_type": "string"
          }
        },
        {
          "ip_address": {
            "match_mapping_type": "string",
            "match_pattern": "regex",
            "match": "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
            "mapping": {
              "type": "ip"
            }
          }
        }
      ],
      "_source": {
        "enabled": true
      }
    }
  },
  "settings": {
    "index": {
      "query": {
        "default_field": "uri"
      },
      "number_of_replicas": "1",
      "auto_expand_replicas": "false",
      "translog": {
        "durability": "async"
      },
      "number_of_shards": "8",
      "refresh_interval": "60s"
    }
  },
  "index_patterns": [
    "haproxy-*"
  ],
  "order": 100
}

```

I did not find any documentations/examples, so I would appreciate any help on this.  
Also, is there a way to debug dynamic\_template rules?  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![gerilya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gerilya/32/22317_2.png) [@gerilya](https://discuss.elastic.co/u/gerilya)\
**Post date:** [November 21, 2018, 7:06am UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403/2 "2018-11-21T07:06:30Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [November 21, 2018, 7:57am UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403/3 "2018-11-21T07:57:54Z")

</div>

Match\_pattern / Match works over field names rather than field values. See the [link](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html#match-unmatch) for details.

It would be better to define templates similar to below mentioned snippet. So any field names matching ip\_\* pattern will automatically be applied with IP mapping type.

```
{ "aliases": {}, "mappings": { "doc": { "_all": { "enabled": false }, "dynamic_templates": [{ "ip_address": { "match_mapping_type": "string", "match": "ip_*", "mapping": { "type": "ip" } } }], "_source": { "enabled": true } } }, "settings": { "index": { "query": { "default_field": "uri" }, "number_of_replicas": "1", "auto_expand_replicas": "false", "translog": { "durability": "async" }, "number_of_shards": "3", "refresh_interval": "60s" } }, "index_patterns": ["haproxy*"], "order": 100}

```

If you are naming fields as IP addresses, it is not a good idea because you'll soon face mapping explosions, as IPv4 alone can have roughly 2^32 combinations.

Hope this answers your question.

---

<div class="post-metadata">

**Author:** ![gerilya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gerilya/32/22317_2.png) [@gerilya](https://discuss.elastic.co/u/gerilya)\
**Post date:** [November 22, 2018, 11:48am UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403/4 "2018-11-22T11:48:33Z")

</div>

Thank you very much for taking the time and explaining this to me.  
You are absolutely correct that I wanted to match field values (rather than names), which is apparently not supported.

---

<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:** [December 20, 2018, 11:48am UTC](https://discuss.elastic.co/t/regexp-in-dynamic-templates/157403/5 "2018-12-20T11:48:37Z")

</div>

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