Convert field data type from text to Date

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.

Welcome to our community! :smiley:

What is the mapping of the index-0001 index?

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" }
}
}

Can you post an example document as well please.

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"
}
}

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 :slight_smile:

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"
   }
}

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?

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

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.

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"
     }
  }
}

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