How to update field format in Opensearch/Elasticsearch?

1.2k Views Asked by At

I am trying to change the format of a string field in opensearch:

PUT my_index/_mapping
{
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date",
        "format": "YYYY-MM-DD HH:mm:ss.SSS"
      }
    }
  }
}

Response is

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "Root mapping definition has unsupported parameters:  [mappings : {properties={timestamp={format=YYYY-MM-DD HH:mm:ss.SSS, type=date}}}]"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "Root mapping definition has unsupported parameters:  [mappings : {properties={timestamp={format=YYYY-MM-DD HH:mm:ss.SSS, type=date}}}]"
  },
  "status" : 400
}

I've spent days trying to figure this out, seems to me like Opensearch is just so unnecessarily complex.

1

There are 1 best solutions below

0
On

You cannot change the type of an existing field once it's been created. You need to reindex your index with the wrong mapping into a new index with the right mapping.

First, create the new index:

PUT new_index
{
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date",
        "format": "YYYY-MM-DD HH:mm:ss.SSS"
      }
    }
  }
}

Then, reindex the old index into the new one

POST _reindex
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  }
}