Judy T Raj
Judy T Raj

Reputation: 1844

Elasticsearch: Bulk request throws error in Elasticsearch 6.1.1

I recently upgraded to Elasticsearch version 6.1.1 and now I can't bulk index documents from a JSON file. When I do it inline, it works fine. Here are the contents of the document:

{"index" : {}}
{"name": "Carlson Barnes", "age": 34}
{"index":{}}
{"name": "Sheppard Stein","age": 39}
{"index":{}}
{"name": "Nixon Singleton","age": 36}
{"index":{}}
{"name": "Sharron Sosa","age": 33}
{"index":{}}
{"name": "Kendra Cabrera","age": 24}
{"index":{}}
{"name": "Young Robinson","age": 20}

When I run this command,

curl -XPUT 'localhost:9200/subscribers/ppl/_bulk?pretty' -H 'Content-Type: application/json' -d @customers_full.json

I get this error:

"error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "The bulk request must be terminated by a newline [\n]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "The bulk request must be terminated by a newline [\n]"
  },
  "status" : 400

It works fine if I send the data inline and in Elasticsearch 5.x. I tried adding newlines as well as the newline character to the end of the file. Doesn't seem to work.

Upvotes: 29

Views: 45882

Answers (20)

Ashish Sondagar
Ashish Sondagar

Reputation: 1095

  • I facing the same issue,
  • I solve by just adding new line in End of the payload (like in line no.3 I just add extra line, \\n)
  • Sample PostMan Request:

enter image description here

  • Sharing Sample cURL Request:

curl --location 'http://127.0.0.1:9200/_bulk'
--header 'Content-Type: application/x-ndjson'
--data '{ "update": { "_id": 3813, "_index": "catalyst3dev_allproductgroup" } } { "doc": {"name": "Carlson Barnes", "age": 34}, "doc_as_upsert": true } '

Upvotes: 0

Garry Kasprov
Garry Kasprov

Reputation: 11

When I use /Users/myName/file.json the question show, but when I use @/Users/myName/file.json it works.

Upvotes: 1

Yaki Klein
Yaki Klein

Reputation: 4366

A few things to check:

  1. The file ends with new line (\n).
  2. The new line is using Unix eol (LF) and not mac or windows eol.
  3. When specifying the file name in the curl command, make sure "@" was added before the file name.

Upvotes: 0

Ehsan Sarshar
Ehsan Sarshar

Reputation: 3211

I just forgot to add an @ symbol before file name like this

--data-binary "@products.json"

Upvotes: 3

fatkobra
fatkobra

Reputation: 3

How do you do that if you are not using a data-file? I am having the issue but not sending data from a file.

const data1 = {
    "amount" : "100",
    "@timestamp" : `${UTC_timestamp}`,
    "transaction_attributes" : {
    "channel" : "channel-foobarbaz",
    "session_id" : "session-1234",
    "information" : "iinformation-foobarbaznformation-foobarbaz"
    },
    "currency" : {
    "currency_description" : "my currency description",
    },
    "external_timestamp" : "2021-12-03T11:22:55.206229500Z" };

  
// execute a post
let res = http.post(url,JSON.stringify(data1),params);

Upvotes: 0

Promise Preston
Promise Preston

Reputation: 28800

I had a similar issue when working with Elasticsearch 7.3.

Here's how I solved it.

  1. Locate the .json file, say products.json file.
  2. Double click to open the .json file in your text editor.
  3. Scroll to the end of the .json file and then press the ENTER key on your keyboard.
  4. Close the .json file. This will create a new line at the end of .json file.
  5. Go back to your terminal and run the command below.

N/B: For the command below, the .json file name is products.json which I am importing to http://localhost:9200/ecommerce/product

curl -H "Content-type: application/json" -XPOST "http://localhost:9200/ecommerce/product/_bulk?pretty" --data-binary "@products.json"

That's all.

I hope this helps

Upvotes: 5

Karim
Karim

Reputation: 11

You need to use --data-binary instead of -d in your curl request. Please see: Bulk API

Upvotes: 1

Sudhakar Polukonda
Sudhakar Polukonda

Reputation: 11

I faced a similar issue on windows using elastic 7.9.1 When I used below CURL command.

curl -s -H "Content-Type: application/json" -XPOST localhost:9200/accounts/docs/_bulk?filter_path=items.*.error --data-binary  "@textoutES.json"  >> erroredAtES.json"

I tried to manually add Newline at the end of the file but did not work.

I have created my JSON by extracting data from MySQL database like below to make sure my records should end with LINE FEED and CARRIAGE RETURN.

Then, it is working for me:

SELECT CONCAT('{"index":{"_id":"',id,'"}}\r\n',request_data,'\r\n') reqestData FROM cards 

More importantly you End-of-File should have a carriage-return and Line-Feed (CRLF)if you are using windows. Also if any line in JSON contains a CR but no LF then you will get parsing exception Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@2d5ed2ca

Windows CRLF and EOF

Upvotes: 1

Shubham Paldewar
Shubham Paldewar

Reputation: 51

For me, the issue was only due to the wrong file name. I have used customer_full.json in command whereas the file was named customer_full in my file system (without the extension).

So in my case,this command worked for me:

curl -H "Content-Type: application/x-ndjson" -XPOST 'http://localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"customer_full" 

Upvotes: 1

Brian Loveless
Brian Loveless

Reputation: 13

I was struggling with this for a hot minute. Mine was caused by a space in my curl request between the --data and the -binary and gave the same error - must end with new line [\\n]}.

So double-check that in the curl req it's --data-binary not --data - binary

Upvotes: 1

Sathishkumar
Sathishkumar

Reputation: 3733

Add empty line at the end of the JSON file and save the file and then try to run the below command

curl -XPOST localhost:9200/subscribers/ppl/_bulk?pretty --data-binary @customers_full.json -H 'Content-Type: application/json'

Upvotes: 74

meol
meol

Reputation: 1033

This worked in my local set-up.

curl -H "Content-type:application/json" -XPOST "http://localhost:9200/customer/personal/_bulk?pretty" --data-binary @"generated.json"

Upvotes: 0

sourabh kumar verma
sourabh kumar verma

Reputation: 21

Press Enter end of the line inside the JSON file and run the command again.

curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"generated.json"

Upvotes: 2

Vince I
Vince I

Reputation: 610

I had the same problem running on Windows 10, using ElasticSearch 7.5.1.

I tried all the answers; none of them worked. I was certain I had a newline at the end of my file.

To get it to work, I had to ensure the file I was uploading was using UNIX end-of-line characters (0A only, no 0D), and also the encoding had to be UTF-8.

ElasticSearch Bulk Fail

Using Notepad++, you can edit the metadata of the file.

Update EOL

Update Encoding

Finally some good news:

enter image description here

Upvotes: 2

Android14
Android14

Reputation: 1125

As the document says: use the --data-binary flag instead of plain -d

-d doesn’t preserve newlines and doesn't format the json.

I faced this problem because of JSON formatting.

Upvotes: 27

Rajat Goel
Rajat Goel

Reputation: 173

This worked for me:

curl -H "Content-Type: application/x-ndjson" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@C:\Program Files\Elastic\Elasticsearch\7.2.0\accounts.json"

Upvotes: 5

bnahayo
bnahayo

Reputation: 121

For anyone using postman to make requests to ElasticSearch

Just press enter to create an empty new line!

And voila, problem solved

Upvotes: 4

Raya Fratkina
Raya Fratkina

Reputation: 71

I ran into the same issue and spent hours adding and removing newlines before somebody pointed out I mis-typed the file name... So note that curl will throw the same error if the file is not actually present, making this super-confusing.

Upvotes: 7

user6589750
user6589750

Reputation: 21

you just need to open json file and then go to the end of the file ( Ctrl+end) and then please Enter to break a new line.

Upvotes: 2

Val
Val

Reputation: 217254

The error is pretty clear:

The bulk request must be terminated by a newline [\n]

So you simply need to add a newline at the end of your customers_full.json file and you'll be ok.

Upvotes: 8

Related Questions