# Unable to get results using date as string field

**URL:** https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247
**Category:** Elasticsearch
**Created:** [February 9, 2016, 7:50am UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247 "2016-02-09T07:50:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rraghuvittal](https://avatars.discourse-cdn.com/v4/letter/r/ea5d25/32.png) [@rraghuvittal](https://discuss.elastic.co/u/rraghuvittal)
#### Post date: [February 9, 2016, 7:50am UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247/1 "2016-02-09T07:50:54Z")

</div>

Hi All,

I have created a string field and inserting the date value in [yyyy-mm-dd] format. As per my requirement i can't change the field type to date.When i try to search data using the range query it is not giving the results between two dates.

```
PUT testdate
 {
   "mappings": {
      "documents": {
        "dynamic" : "strict",
         "properties": {
           "name":{
             "type": "string"
           },
           "cdate":{
             "type": "string"
           }
         }
      }
   }
}

POST testdate/documents/_bulk
{ "index": { "_id": 1 }}
{ "name" : 10, "cdate" : "2001-01-01" }
{ "index": { "_id": 2 }}
{ "name" : 20, "cdate" : "2001-01-11" }
{ "index": { "_id": 3 }}
{ "name" : 30, "cdate" : "2001-01-25" }

```

Query

```
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "cdate": {
              "from": "2001-01-01",
              "to": "2002-02-02"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "facets": {}
}

```

when i tried it is giving wrong data.

please help me.

---

<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 9, 2016, 8:09am UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247/2 "2016-02-09T08:09:21Z")

</div>

First you should use a date type. BTW, elasticsearch would autodetect this as dates if you don't specify a mapping.

Coming back to your question, as your String uses the default analyzer, your text `2001-01-01` has been broken into [`2001`, `01`, `01`]. Which gives here inaccurate results or no result at all. If you **really** want to do it using strings, you have to set `index: not_analyzed` on your field.

My 2 cents

---

<div class="post-metadata">

### Author: ![rraghuvittal](https://avatars.discourse-cdn.com/v4/letter/r/ea5d25/32.png) [@rraghuvittal](https://discuss.elastic.co/u/rraghuvittal)
#### Post date: [February 9, 2016, 9:24am UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247/3 "2016-02-09T09:24:12Z")

</div>

thank you for reply.

is there any difference the way we define mappings for the index. in the above example for "cdate" mapping structure is like...

"properties": {  
"cdate":{  
"type": "string",  
"index":"not\_analyzed"  
}  
}  
able to get the exact results.

when i change the mapping as  
"properties": {  
"cdate": {  
"type": "string",  
"fields": {  
"raw": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
}

not able to get the proper results.

---

<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 9, 2016, 11:15am UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247/4 "2016-02-09T11:15:09Z")

</div>

Please format your code.

If you do this, then you have to search in `cdate.raw` field.

---

<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: [July 5, 2017, 11:18pm UTC](https://discuss.elastic.co/t/unable-to-get-results-using-date-as-string-field/41247/5 "2017-07-05T23:18:00Z")

</div>


