Field is of type string when doing search when mapping specifies double

Hi All,

I have a question about field types in ES 2.3. I want to index a document with a field of type string and want ES to store it as a double. I created a 'myindex' index with the following mapping:

"mappings": {
    "mytype": {
      "properties": {
        "myfield": {
          "type": "double"
        }
      }
    }
  }

I then indexed the following document:

curl -XPUT 'localhost:9200/myindex/mytype/mydoc1' -d '{
  "myfield": "0509195614.3333333331234"
}'

The problem is that when I query the index, the field shows up as a string:

curl -XGET 'localhost:9200/myindex/_search'

{
        "_index": "myindex",
        "_type": "mytype",
        "_id": "mydoc1",
        "_score": 1,
        "_source": {
          "myfield": "0509195614.3333333331234"
        }
      }

Am I doing something wrong? Is this expected?

Thanks!

This is expected. You can't store a value as one type, then index it as another.

The weird thing is that when I query the document and ask for that particular field it returns it as a number. If I just query for the document without specifying the field it returns it as a string. I'm confused as to what's going on.

curl -XGET 'localhost:9200/myindex/mytype/mydoc1?fields=myfield'

{
  "_index": "myindex",
  "_type": "mytype",
  "_id": "mydoc1",
  "_version": 1,
  "found": true,
  "fields": {
    "myfield": [
      509195614.3333333
    ]
  }
}

This is a bit confusing. It is due to ES being proactive and knows that you really meant double and not string. ES converts it internally to a float or integer even if you use "123" in the _source, but it can have some strange stuff happen in other situations.

In this case I'm defining in the mapping that the "myfield" field should be a double. I'm wondering if operations like sort or range queries will be done over numbers instead of strings.