1

I'm a beginner with Elasticsearch and am following an "Essential Training" in LinkedIn Learning. I'm trying to follow with bulk loading API and the instructor is using Linux, I'm on Windows. He created a text file to read in with data using "VI". I just created a text file and pasted the data and removed the ".txt". The contents of the file, called reqs, is this:

{
   "index":{
      "_index":"my-test",
      "_type":"my-type",
      "_id":"1"
   }
}{
   "col1":"val1"
}{
   "index":{
      "_index":"my-test",
      "_type":"my-type",
      "_id":"2"
   }
}{
   "col1":"val2"
}{
   "index":{
      "_index":"my-test",
      "_type":"my-type",
      "_id":"3"
   }
}{
   "col1":"val3"
}

I've tried saving it with a carriage return (new line) after the last line and without. I saved this into my elasticsearch folder (C:\elasticsearch-7.12.0) which is the same directory I'm running the following command from:

c:\elasticsearch-7.12.0>curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@reqs"; echo

When I do this, I'm getting the following error:

{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}
1
  • The application/x-ndjson format is newline-delimited JSON. I'm pretty sure the formatted JSON file will not work. Maybe ensure each document is on a separate line, no newlines for human-friendly formatting? Commented Apr 20, 2021 at 19:03

1 Answer 1

1

Use this below curl command

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/index-name/_bulk?pretty' --data-binary @reqs.json

reqs.json should look like this

{"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "1"}}
{"col1" : "val1"}
{"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "2"}}
{"col1" : "val2"}
{"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "3"}}
{"col1" : "val3"}
Sign up to request clarification or add additional context in comments.

4 Comments

I am SO sorry - I thought I had posted a reply (NEW to Stack Overflow). It didn't work - I got a {"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}
@Rob Searing have you saved the file in .json format ? i.e instead of just reqs, you need to save it as reqs.json
correct. I have elasticsearch and kibana both working (when I open a browser I can get localhost:9200 & :5601 - Kibana opens). I have reqs.json in the elasticsearch folder with contents: {"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "1"}} {"col1" : "val1"} {"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "2"}} {"col1" : "val2"} {"index" : {"_index" : "my-test", "_type" : "my-type", "_id" : "3"}} {"col1" : "val3"} I copy paste your curl command from the same folder I have the reqs.json file in (the elasticsearch one) and get
c:\elasticsearch-7.12.0>curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/index-name/_bulk?pretty' --data-binary @reqs.json curl: (6) Could not resolve host: application curl: (6) Could not resolve host: 'localhost

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.