-
Login in to your sandbox
-
We have not yet configured elasticsearch service to start automatically, start it manually:
sudo systemctl start elasticsearch
-
Service will start but the listener will take its time before responding to incoming requests, verify with the command:
curl localhost:9200
-
Give it a few minutes before you get json response with a tagline
"You Know, for Search"
-
Post a new document:
curl -XPOST localhost:9200/orders/_doc/1 \ -H 'content-type: application/json' \ -d ' { "id": "1", "placedOn": "2016-10-17T13:03:30.830Z" }'
-
Fetch mapping:
curl 'localhost:9200/orders/_mapping?pretty=true'
-
Expected response:
{ "orders" : { "mappings" : { "_doc" : { "properties" : { "id" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "placedOn" : { "type" : "date" } } } } } }
-
Try modifying existing mapping:
curl -XPUT 'localhost:9200/orders/_mapping?pretty=true' \ -H 'content-type: application/json' \ -d ' { "orders" : { "mappings" : { "_doc" : { "properties" : { "id" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "placedOn" : { "type" : "date", "format" : "strict_date_optional_time||epoch_millis" } } } } } }'
-
What's the outcome? And why?
-
Let us try again:
-
curl -XPUT 'localhost:9200/orders/_mapping?pretty=true' \ -H 'content-type: application/json' \ -d ' { "properties" : { "id" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "placedOn" : { "type" : "date", "format" : "strict_date_optional_time||epoch_millis" } } }'
-
Did it work? What's the difference?
-
Let's modify data type for existing field
curl -XPUT 'localhost:9200/orders/_mapping?pretty=true' \ -H 'content-type: application/json' \ -d '{ "properties": { "id": { "type": "double" }, "placedOn": { "type": "date", "format": "strict_date_optional_time||epoch_millis" } } }'
-
Seriously, what now? Why? So much for the dynamic data mapping?
-
What if we need to change data type after we have indexed the data?
-
There is no (more) option to delete the mapping - delete and recreate index is the only option :-(
curl -XDELETE localhost:9200/orders curl -XPUT 'localhost:9200/orders?pretty=true' \ -H 'content-type: application/json' \ -d ' { "settings": { "number_of_shards": 3, "number_of_replicas": 2 }, "mappings": { "properties": { "id": { "type": "double" }, "placedOn": { "type": "date", "format": "strict_date_optional_time||epoch_millis" } } } }'
-
Watch out: there is no warning or confirmation using curl!
-
Psst: lookout for the proper response too:
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "orders" }