# Convert field data type from text to Date

**URL:** <https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781>\
**Category:** Elasticsearch\
**Tags:** reindex\
**Created:** [January 12, 2021, 4:10am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781 "2021-01-12T04:10:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 12, 2021, 4:10am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/1 "2021-01-12T04:10:19Z")

</div>

Hi All,

We have created an index that consists of text data type for all fields, but we need to convert timestamp field from text to date (format "2021-01-12 09:19:25.890") but unable to do.

I have tried creating a new index with the correct data type as below

PUT /index-0002  
{  
"settings" : {  
"number\_of\_shards" : 4,  
"number\_of\_replicas" : 1  
},  
"mappings" : {  
"properties" : {  
"filed1" : { "type" : "long"},  
"filed2" : { "type" : "text" },  
"filed3" : { "type" : "text" },  
"filed4": { "type" : "text" },  
"timestamp": {  
"type": "date",  
"format": "yyyy-MM-dd HH:mm:ss.SSS"  
}  
}  
}  
}

migrated data from old to new index using reindex API

POST /\_reindex  
{  
"source": {  
"index": "index-0001"  
},  
"dest": {  
"index": "index-0002"  
}  
}

but once it is done the index data type changed to text instead of date

Please help me resolve this issue.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 12, 2021, 5:13am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/2 "2021-01-12T05:13:46Z")

</div>

Welcome to our community! 😃

What is the mapping of the `index-0001` index?

---

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 12, 2021, 5:26am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/3 "2021-01-12T05:26:46Z")

</div>

Thank you for quick response  
mapping for index-0001

"mappings" : {  
"properties" : {  
"filed1" : { "type" : "text"},  
"filed2" : { "type" : "text" },  
"filed3" : { "type" : "text" },  
"filed4": { "type" : "text" },  
"timestamp": { "type" : "text" }  
}  
}

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 12, 2021, 6:31am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/4 "2021-01-12T06:31:54Z")

</div>

Can you post an example document as well please.

---

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 12, 2021, 6:54am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/5 "2021-01-12T06:54:45Z")

</div>

Document Example  
{  
"\_index" : "index-0001",  
"\_type" : "\_doc",  
"\_id" : "rE64RXYBWfNr3RkH119n",  
"\_version" : 1,  
"\_seq\_no" : 240,  
"\_primary\_term" : 1,  
"found" : true,  
"\_source" : {  
"filed1" : "7",  
"filed2" : "info",  
"filed3" : "Source exception",  
"filed4" : "Exiting ",  
"timestamp" : "2020-12-09 03:32:59.830"  
}  
}

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 12, 2021, 7:00am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/6 "2021-01-12T07:00:18Z")

</div>

It'd help if you could please format your code/logs/config using the `</>` button, or markdown style back ticks. It helps to make things easy to read which helps us help you 🙂

---

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 13, 2021, 8:27am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/7 "2021-01-13T08:27:02Z")

</div>

PFB details

new index with the correct data type as below

```
   PUT /index-0002
   {
   "settings":{
      "number_of_shards":4,
      "number_of_replicas":1
   },
   "mappings":{
      "properties":{
         "filed1":{
            "type":"long"
         },
         "filed2":{
            "type":"text"
         },
         "filed3":{
            "type":"text"
         },
         "filed4":{
            "type":"text"
         },
         "timestamp":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss.SSS"
         }
      }
   }
}

```

migrated data from old to new index using reindex API

```
 POST /_reindex
{
   "source":{
      "index":"index-0001"
   },
   "dest":{
      "index":"index-0002"
   }
}

```

index-0001 mapping

```
"mappings":{
   "properties":{
      "filed1":{
         "type":"text"
      },
      "filed2":{
         "type":"text"
      },
      "filed3":{
         "type":"text"
      },
      "filed4":{
         "type":"text"
      },
      "timestamp":{
         "type":"text"
      }
   }
}

```

Document Example

```
{
   "_index":"index-0001",
   "_type":"_doc",
   "_id":"rE64RXYBWfNr3RkH119n",
   "_version":1,
   "_seq_no":240,
   "_primary_term":1,
   "found":true,
   "_source":{
      "filed1":"7",
      "filed2":"info",
      "filed3":"Source exception",
      "filed4":"Exiting ",
      "timestamp":"2020-12-09 03:32:59.830"
   }
}
```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [January 13, 2021, 8:51am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/8 "2021-01-13T08:51:14Z")

</div>

That looks fine and the new index has the correct mapping. You should now be able to delete the old index. I assume this solves the problem?

---

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 13, 2021, 9:48am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/9 "2021-01-13T09:48:55Z")

</div>

But after executing reindex API, in the new Index, timestamp field data type is text instead of date data type

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 13, 2021, 9:26pm UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/10 "2021-01-13T21:26:41Z")

</div>

> [@Sathish22](#):
>
> `index-0002`

I don't believe you have posted the mapping for that, just what you used as a template. If you can post the mapping it should make things clearer.

---

<div class="post-metadata">

**Author:** ![Sathish22](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@Sathish22](https://discuss.elastic.co/u/Sathish22)\
**Post date:** [January 15, 2021, 6:08am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/11 "2021-01-15T06:08:30Z")

</div>

index-0002 mapping

```
"mappings":{
  "properties":{
     "filed1":{
        "type":"long"
     },
     "filed2":{
        "type":"text"
     },
     "filed3":{
        "type":"text"
     },
     "filed4":{
        "type":"text"
     },
     "timestamp":{
        "type":"date",
        "format":"yyyy-MM-dd HH:mm:ss.SSS"
     }
  }
}
```

---

<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:** [February 12, 2021, 6:08am UTC](https://discuss.elastic.co/t/convert-field-data-type-from-text-to-date/260781/12 "2021-02-12T06:08:45Z")

</div>

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