# Map all keys under a key to string

**URL:** <https://discuss.elastic.co/t/map-all-keys-under-a-key-to-string/86862>\
**Category:** Elasticsearch\
**Created:** [May 23, 2017, 7:52pm UTC](https://discuss.elastic.co/t/map-all-keys-under-a-key-to-string/86862 "2017-05-23T19:52:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_Yeh](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@Justin\_Yeh](https://discuss.elastic.co/u/Justin_Yeh)\
**Post date:** [May 23, 2017, 7:52pm UTC](https://discuss.elastic.co/t/map-all-keys-under-a-key-to-string/86862/1 "2017-05-23T19:52:21Z")

</div>

Hi, I'm trying to make elasticsearch map all keys under a specific key to a string value. I know the parent key name, but the nested keys can be of arbitrary key name and value type.

For example, two json blobs:

{  
"key1":"stringvalue",  
"key2": {  
"arbitraryname1": 0  
}  
}

{  
"key1":"stringvalue",  
"key2": {  
"arbitraryname1": "stringvalue"  
}  
}

I'm not sure how to map the above so that "arbitraryname1":0 and "arbitraryname1":"stringvalue" both get mapped as strings: "arbitraryname1":"1" and "arbitraryname1":"stringvalue"

I need to do this because the value type of arbitraryname1 can change. I also can't do an explicit mapping on arbitraryname1 because i do not know the key names ahead of time.

So what I want is just to say "for all entries underneath key2, map those as strings". Is there some way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 26, 2017, 8:55am UTC](https://discuss.elastic.co/t/map-all-keys-under-a-key-to-string/86862/2 "2017-05-26T08:55:26Z")

</div>

Use `path_match` for this:

```auto
PUT t
{
  "mappings": {
    "t": {
      "dynamic_templates": [
        {
          "arbitrary_as_strings": {
            "path_match": "key2.*",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ]
    }
  }
}

PUT t/t/1
{
  "key1": "stringvalue",
  "key2": {
    "arbitraryname1": 0
  }
}

GET _mapping

```

returns:

```auto
{
  "t": {
    "mappings": {
      "t": {
        "dynamic_templates": [
          {
            "arbitrary_as_strings": {
              "path_match": "key2.*",
              "mapping": {
                "type": "keyword"
              }
            }
          }
        ],
        "properties": {
          "key1": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "key2": {
            "properties": {
              "arbitraryname1": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [June 23, 2017, 8:55am UTC](https://discuss.elastic.co/t/map-all-keys-under-a-key-to-string/86862/3 "2017-06-23T08:55:45Z")

</div>

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