8

I'm trying to insert a JSON data file in my elasticsearch instance.

curl -s -H “Content-Type: application/x-ndjson” -XPOST localhost:9200/_bulk —-data-binary “@restaurants.json”; echo

However, after executing this command I get an error that says

{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}

The JSON file basically has an array of the below object. Only thing is that I've put just one object here to save space. However, there are more than one objects present.

Structure is like given below;

[
   {
      "address": {
         "building": "351",
         "coord": [
            -73.98513559999999,
            40.7676919
         ],
         "street": "West   57 Street",
         "zipcode": "10019"
      },
      "borough": "Manhattan",
      "cuisine": "Irish",
      "name": "Dj Reynolds Pub And Restaurant",
      "grades": [
         {
            "date": {
               "$date": "2014-09-06T00:00:00.000Z"
            },
            "grade": "A",
            "score": 2
         },
         {
            "date": {
               "$date": "2013-07-22T00:00:00.000Z"
            },
            "grade": "A",
            "score": 11
         },
         {
            "date": {
               "$date": "2012-07-31T00:00:00.000Z"
            },
            "grade": "A",
            "score": 12
         },
         {
            "date": {
               "$date": "2011-12-29T00:00:00.000Z"
            },
            "grade": "A",
            "score": 12
         }
      ],
      "id": "30191841"
   }
]
Vivek Malhotra
  • 751
  • 2
  • 7
  • 17
  • This answer may help: https://stackoverflow.com/questions/33340153/elasticsearch-bulk-index-json-data/33340234#33340234 – Val Jan 06 '18 at 06:04

3 Answers3

8

The bulk API requires one document per line, which means you can't have newlines in your documents. Try stripping all the white spaces from the JSON you're submitting. You are also just submit a stream of documents, and not a JSON array of objects. See Bulk API documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Oleksi
  • 12,947
  • 4
  • 56
  • 80
7

I had this same issue in Windows and am posting this here for anyone that comes across the same question with this error referenced.

{"error":{"root_cause":[{"type":"parse_exception","reason":"request body is required"}],"type":"parse_exception","reason":"request body is required"},"status":400}

I found at least two causes:

  1. cURL doesn't know where the file you are trying to load is:

In this case, make sure to run cURL from the directory where the file is located. Try executing the following command and making sure you see the file.

more file.json

  1. Windows command line DOES NOT support single quotes within the cURL command line:

BAD

curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/_bulk' --data-binary '@file.json'

GOOD

curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/_bulk" --data-binary "@file.json"

Ken Tice
  • 71
  • 1
  • 2
1

I solved it by wrapping the url with quotation mark.

curl  -s -H “Content-Type: application/x-ndjson” -XPOST "localhost:9200/_bulk —-data-binary “@restaurants.json”;
Pajri Aprilio
  • 322
  • 5
  • 16