maximus
maximus

Reputation: 2427

How to update a field type in elasticsearch

The ElasticSearch documents just aren't clear on how to do this.

I indexed some tweets, and one of the fields, created_at, indexed as a string instead of a date. I can't find how to reindex with this change via a curl call. If reindexing is a complicated process, then I would much rather just delete what's there and start over. But, I can't find how to specify the field types either!

Any help is greatly appreciated.

Upvotes: 39

Views: 78393

Answers (4)

Jaya Krishna
Jaya Krishna

Reputation: 65

Updating a field type in existing index:

PUT test-index/_doc/_mapping
{
    "doc" : {
        "properties" : {
            "testDateField" : {"type" : "date"}
        }
    }
}

Adding a field with specific type in existing index:

PUT test-index
{
  "mappings": {
    "doc": {
      "properties": {
        "testDateField" : {
          "type": "date"
        }
      }
    }
  }
}

Upvotes: 3

Akhilraj N S
Akhilraj N S

Reputation: 9457

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

Upvotes: 12

dadoonet
dadoonet

Reputation: 14492

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

Upvotes: 33

Jawad
Jawad

Reputation: 95

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

Upvotes: 9

Related Questions