# Dynamic mapping did not cast as the docs state

**URL:** https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857
**Category:** Elasticsearch
**Created:** [January 31, 2018, 4:55pm UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857 "2018-01-31T16:55:22Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![IanRC72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ianrc72/32/26086_2.png) [@IanRC72](https://discuss.elastic.co/u/IanRC72)
#### Post date: [January 31, 2018, 4:55pm UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/1 "2018-01-31T16:55:23Z")

</div>

I ran the following sample from [this page](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html):

```
PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "longs_as_strings": {
            "match_mapping_type": "string",
            "match": "long_*",
            "unmatch": "*_text",
            "mapping": {
              "type": "long"
            }
          }
        }
      ]
    }
  }
}

PUT my_index/my_type/1
{
  "long_num": "5", 
  "long_text": "foo" 
}

```

The result:

```
GET my_index/_search
...

  {
    "_index": "my_index",
    "_type": "my_type",
    "_id": "1",
    "_score": 1,
    "_source": {
      "long_num": "5",
      "long_text": "foo"
    }
  }

```

I was expecting long\_num to be of type long not string.

Am I missing something here?

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [February 2, 2018, 12:41am UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/2 "2018-02-02T00:41:07Z")

</div>

What does the mapping look like? That is, what does `GET my_index/_mapping` show.

---

<div class="post-metadata">

### Author: ![IanRC72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ianrc72/32/26086_2.png) [@IanRC72](https://discuss.elastic.co/u/IanRC72)
#### Post date: [February 2, 2018, 4:59pm UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/3 "2018-02-02T16:59:57Z")

</div>

> [@warkolm](#):
>
> GET my\_index/\_mapping

```
{
  "my_index": {
    "mappings": {
      "my_type": {
        "dynamic_templates": [
          {
            "longs_as_strings": {
              "match": "long_*",
              "unmatch": "*_text",
              "match_mapping_type": "string",
              "mapping": {
                "type": "long"
              }
            }
          }
        ],
        "properties": {
          "long_num": {
            "type": "long"
          },
          "long_text": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [February 2, 2018, 8:53pm UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/4 "2018-02-02T20:53:04Z")

</div>

> [@IanRC72](#):
>
> I was expecting long\_num to be of type long not string.

> [@IanRC72](#):
>
> "long\_num": {  
> "type": "long"  
> }

That seems to match?

---

<div class="post-metadata">

### Author: ![IanRC72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ianrc72/32/26086_2.png) [@IanRC72](https://discuss.elastic.co/u/IanRC72)
#### Post date: [February 3, 2018, 8:14am UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/5 "2018-02-03T08:14:43Z")

</div>

Yes, but the output doesn't:

```
"_source": {
  "long_num": "5",
  "long_text": "foo"
}

```

Somehow it's mapped as a long but displayed (stored?) as a string. That doesn't make sense.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 3, 2018, 9:21am UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/6 "2018-02-03T09:21:54Z")

</div>

Elasticsearch never transforms the source document.

For example when you index a date "2018-02-03" it is indexed internally as a number but when you get back the source, it won't be a number but the exact string you sent.

---

<div class="post-metadata">

### Author: ![IanRC72](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ianrc72/32/26086_2.png) [@IanRC72](https://discuss.elastic.co/u/IanRC72)
#### Post date: [February 3, 2018, 9:26am UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/7 "2018-02-03T09:26:58Z")

</div>

Ah, that makes sense. Thanks much for explaining this.

---

<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: [March 3, 2018, 9:27am UTC](https://discuss.elastic.co/t/dynamic-mapping-did-not-cast-as-the-docs-state/117857/8 "2018-03-03T09:27:04Z")

</div>

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