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
.
json
documentLet’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.
json
documents from command lineElasticsearch provides
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 thejson
documents have to be newline delimited.
json
documents from a fileThe 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
asapplication/x-ndjson
for it’s multiple newline delimitedjson
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"