You can use the _bulk
API to bulk import a large JSON file into Elasticsearch.
curl -s -H "Content-Type: application/json" -XPOST \
'http://localhost:9200/_bulk' --data-binary @/path/to/products.json
The products.json
file must be formatted in the following way:
{ "index" : { "_index" : "products", "_id" : "1" } }
{ "title" : "Nike Running Shoes" }
{ "index" : { "_index" : "products", "_id" : "2" } }
{ "title" : "Adidas Basketball Shoes" }
As you can see in the file snippet above, each record requires two lines:
- The first line specifies the index into which the record should be indexed and its
_id
. - The second line is the actual document to be indexed.
If all you have is a regular JSON file (plain_products.json
), you can use jq
to quickly convert that
to the format that Elasticsearch requires before calling the API:
cat plain_products.json | \
jq -c '.[] | {"index": {"_index": "products", "_id": .id}}, .' | \
curl -XPOST http://localhost:9200/_bulk --data-binary @-