Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 726
maximus Avatar asked Apr 30 '13 01:04

maximus


People also ask

Can we update type of a field in Elasticsearch?

In Elasticsearch you can't change the type of a field once the data indexed.

How do you update existing field mapping in Elasticsearch?

You can use the update mapping API to add new properties to an existing object field. To see how this works, try the following example. Use the create index API to create an index with the name object field and an inner first text field. Use the update mapping API to add a new inner last text field to the name field.

Can we update data in Elasticsearch?

The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

How do I change type in Kibana?

The only ways to change the format are; reindex into a new index. resend the data to Elasticsearch (aka reindex)


2 Answers

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"}         }     } } ' 
like image 93
dadoonet Avatar answered Oct 10 '22 07:10

dadoonet


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"}     } }  
like image 45
Jawad Avatar answered Oct 10 '22 06:10

Jawad