How to insert data to Elasticsearch

Elasticsearch is a schema-less, open source document store and search engine based on Apache Lucene that provides full text search capabilities.

It ingests data in the form of json.

This shot assumes you have basic knowledge of what Elasticsearch is and a working instance of Elasticsearch running in your system at http://localhost:9200.

Index one json document

Let’s try to index one json document to educative index:

curl -X POST "http://localhost:9200/educative/_doc/?pretty" -H 'Content-Type: application/json' -d'
{ "articleName" : "elasticsearch-intro" }
'

Here, we specify the content-type as json for it’s one json document.

Bulk index multiple json documents from command line

Elasticsearch provides _bulk APIsince it’s a single API call to perform bulk indexing of the documents that reduce overhead and improve indexing speed:

curl -X POST "http://localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "educative"} }
{ "articleName" : "elasticsearch-intro" }
{ "index" : { "_index" : "educative"} }
{ "articleName" : "elasticsearch-insert-data" }
{ "index" : { "_index" : "educative"} }
{ "articleName" : "elasticsearch-query-data" }
'

Note: index name has to be specified for every document that needs to be indexed, and the json documents have to be newline delimited.

Bulk index multiple json documents from a file

The above json documents can be read from a file and inserted into Elasticsearch.

Let’s suppose that the above data is stored in a text file named data.txt. The following command will index the data from a file:

curl -s -H "Content-Type: application/x-ndjson" -XPOST http://localhost:9200/_bulk --data-binary "@data.txt"

Note: Here, we specify the content-type as application/x-ndjson for it’s multiple newline delimited json documents.

If the file is in a different directory, then give the absolute path to the file:

curl -s -H "Content-Type: application/x-ndjson" -XPOST http://localhost:9200/_bulk --data-binary "@/Users/abhi/Downloads/temp/data.txt"