How to update a field type in elasticsearch
You need to define a mapping using Put Mapping API.
curl -XPUT 'http://localhost:9200/twitter/_doc/_mapping' -H 'Content-Type: application/json' -d '
{
"_doc" : {
"properties" : {
"message" : {"type" : "text", "store" : true}
}
}
}
'
A date can be defined as follow:
curl -XPUT 'http://localhost:9200/twitter/_doc/_mapping' -H 'Content-Type: application/json' -d '
{
"_doc" : {
"properties" : {
"user" : {"type" : "keyword", "null_value" : "na"},
"message" : {"type" : "text"},
"postDate" : {"type" : "date"},
"priority" : {"type" : "integer"},
"rank" : {"type" : "float"}
}
}
}
'
New version of Elasticsearch wont support field type changing, But we can achive this by reindexing. You can follow the below steps to achive the reindeing of an index and change the type in Elasticsearch.
Create new index
PUT project_new
Update the mapping with new field type mapping
PUT project_new/_mapping/_doc
{
"properties": {
"created_by": {
"type": "text"
},
"created_date": {
"type": "date"
},
"description": {
"type": "text"
}
}
}
Reindex the new index with old one ie data migrations
POST _reindex
{
"source": {
"index": "project"
},
"dest": {
"index": "project_new",
"version_type": "external"
}
}
Change the alias of newly created index to point to old index name
POST _aliases
{
"actions": [
{
"add": {
"index": "project_new",
"alias": "project"
}
},
{
"remove_index": {
"index": "project"
}
}
]
}
Now you will be able to view the updated type in your existing index.
Tested and working in Eleasticsearch Version 6.4.3
You also need to specify format not just type if you are inserting a mysql timestamp then you should just add a format to it like this.
"properties": {
"updated_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
If we consider your example then it should be like
"tweet" : {
"properties" : {
"user" : {"type" : "string", "index" : "not_analyzed"},
"message" : {"type" : "string", "null_value" : "na"},
"postDate" : {"type" : "date" , "format": "yyyy-MM-dd HH:mm:ss" },
"priority" : {"type" : "integer"},
"rank" : {"type" : "float"}
}
}