Dynamic mapping did not cast as the docs state


(Ian Campbell) #1

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?


(Mark Walkom) #2

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


(Ian Campbell) #3
{
  "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
              }
            }
          }
        }
      }
    }
  }
}

(Mark Walkom) #4

That seems to match?


(Ian Campbell) #5

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.


(David Pilato) #6

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.


(Ian Campbell) #7

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


(system) #8

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