Writing an item to Elastic Search
It is easy to write an item to an index on Elastic Search, just do the following:
curl -XPUT 'https://servername:9200/index_name/type_name/id' -d '{
"name" : "Barack Obama"
}'
On the command above you will send:
index_name: your index name
type_name: name of the defined type
id: id that will be associated to the item. This id will be available at the _id field.
The item is passed as parameter using the -d option. Elastic Search, by default, will try to use the current mapping. If any property is not found, Elastic Search will update the mapping automatically.
If we add item with a field called 'extra_field' that was not defined in the index, the mapping will be automatically updated. We add the item:
curl -XPUT 'https://servername:9200/index_name/type_name/7' -d '{
"name" : "Barack Obama 2", "extra_field" : "President"
}'
The mapping will be updated, we can check the current mapping with the following command:
https://servername:9200/index_name/type_name/_mapping?pretty
Here is the mapping, the extra_field was automatically added as string:
{
"index_name" : {
"mappings" : {
"type_name" : {
"properties" : {
"extra_field" : {
"type" : "string"
},
"name" : {
"type" : "string"
}
}
}
}
}
}
Pay special attention if you are dynamic adding a date field, Elastic Search will try to detect it as date and try to parse it. If later you add a date an invalid date on this field, it will fail. If it is a matter to you, use the date_detection parameter, better described in the documentation.