Individual records are called documents in ElasticSearch. Documents contain different information relating to a single record and are contained in a corresponding index. For example, an index named clients can include a document containing the name, surname, and email address. View the document as an individual entity, ie each record describes a single element in that object.
How to create a document in ElasticSearch with a predefined ID?
When you already know the ID that you want your new document to be associated with, you can insert a new document with that ID.
The following example from the command line will insert a new document with our predefined ID into Elasticsearch:
user@server [~]# curl -XPUT 'localhost:9200/mynewindex/external/mynewid?pretty' -d '
{
"first_name": "John",
"last_name": "Doe",
"email_address": "[email protected]"
}'
As shown, we are making a simple PUT request using the curl command to our ElasticSearch server that is located at localhost on port 9200.
Then, we are defining an index that we want this document to be located in which is mynewindex. After this, we are stating the data type that we want to place the data within. In this case, our type is external.
We are then defining the ID within the last part of the URL which in this case, is mynewid. The last little bit (?pretty) is simply stating that we want a well-formatted response.
Lastly, is simply our data that we are placing into the document, formatted in JSON. As you can see, we are inserting an individual's first name, last name, and email address.
You'll get back a answer which looks like this:
user@server [~]# curl -XPUT 'localhost:9200/mynewindex/external/mynewid?pretty' -d '
{
"first_name": "John",
"last_name": "Doe",
"email_address": "[email protected]"
}'
{
"_index" : "mynewindex",
"_type" : "external",
"_id" : "mynewid",
"_version" : 1,
"created" : true
}
Now, our new document was successfully created in the index mynewindex with the type external and the ID mynewid.
Next, we will see what happens if you don't have an ID that you specifically want to define.
How to insert a new document without defining an ID
It is quite easy to Insert a document without specifically defining an ID. If an ID is not passed, it will simply create one for you.
We can do this by executing the following command:
user@server [~]# curl -XPOST 'localhost:9200/mynewindex/external?pretty' -d '
{
"first_name": "John",
"last_name": "Doe",
"email_address": "[email protected]"
}'
As you can see, is very similar to creating a document with a pre-defined ID but with a few minor changes.
First, we are sending a POST request instead of a PUT request. Next, we are simply removing the ID that we previously had defined.
You'll get back a answer which looks like this:
user@server [~]# curl -XPOST 'localhost:9200/mynewindex/external?pretty' -d '
{
"first_name": "John",
"last_name": "Doe",
"email_address": "[email protected]"
}'
{
"_index" : "mynewindex",
"_type" : "external",
"_id" : "AUruRKQIWyBxgi2aaH34",
"_version" : 1,
"created" : true
}
Now the ID has been automatically created with a random string. It is particularly useful if the data you are adding, such as a list of clients, may not have a way to define it differently.
Now you will be able to insert new documents into ElasticSearch.
