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.
In Elasticsearch you can't change the type of a field once the data indexed.
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.
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.
The only ways to change the format are; reindex into a new index. resend the data to Elasticsearch (aka reindex)
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"} } } } '
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"} } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With