Dynamic mapping did not cast as the docs state

I ran the following sample from this page:

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?

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

{
  "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
              }
            }
          }
        }
      }
    }
  }
}

That seems to match?

1 Like

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.

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.

1 Like

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

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