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

**URL:** https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110
**Category:** Elasticsearch
**Created:** [February 22, 2017, 9:19pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110 "2017-02-22T21:19:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![spynxo](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@spynxo](https://discuss.elastic.co/u/spynxo)
#### Post date: [February 22, 2017, 9:19pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/1 "2017-02-22T21:19:42Z")

</div>

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:

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

```

I then indexed the following document:

```auto
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:

```auto
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!

---

<div class="post-metadata">

### Author: ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)
#### Post date: [February 22, 2017, 11:19pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/2 "2017-02-22T23:19:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![spynxo](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@spynxo](https://discuss.elastic.co/u/spynxo)
#### Post date: [February 22, 2017, 11:58pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/3 "2017-02-22T23:58:56Z")

</div>

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.

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

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

```

---

<div class="post-metadata">

### Author: ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)
#### Post date: [February 23, 2017, 1:06am UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/4 "2017-02-23T01:06:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![spynxo](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@spynxo](https://discuss.elastic.co/u/spynxo)
#### Post date: [February 23, 2017, 6:42pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/5 "2017-02-23T18:42:09Z")

</div>

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.

---

<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 23, 2017, 6:42pm UTC](https://discuss.elastic.co/t/field-is-of-type-string-when-doing-search-when-mapping-specifies-double/76110/6 "2017-03-23T18:42:38Z")

</div>

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