Load Json to Elasticsearch

I am new to Elasticseach and I am on the 7.3.0 release. On the documentation page: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-explore-data.html
there is command to load the "accounts.json". I copied that into file "account.json" and ran the provided curl command with following results:

PS C:\cmsp1\Elasticsearch> curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type "System.String" to type "System.Collections.IDictionary".
At line:1 char:9

  • curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/ ...
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Invoke-WebRequest], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

The first few lines of the account.json is:
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
Can you help? I run on Win 10

The problem here is that the command is being run with PowerShell, rather than curl.

For better or worse, PowerShell aliased curl command to Invoke-WebRequest in an early version, which won't be removed in a future version, due to the potential for breaking changes.

You have a few options:

  1. Download curl for Windows and invoke the curl.exe with the parameters as above. You may consider putting curl.exe in the path environment variable, and removing the curl alias in your PowerShell profile:

    Remove-Item Alias:curl
    
  2. Use WSL for Windows, and use curl within

  3. Use PowerShell to make the request, rather than curl

     Invoke-WebRequest "http://localhost:9200/bank/_bulk?pretty&refresh" `
         -Method Post -ContentType "application/json" `
         -InFile .\accounts.json -UseBasicParsing
    

Thank you for quick response.

I already had curl installed so I ran standard CMD with following results. Looking at the accounts.json file, all lines are terminated with CR/LF.

Is there some special configuration missing?
Is the problem in the command formulation or in the source file?

C:\cmsp1\Elasticsearch>curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
{
"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
}

Each line must end with \n and not \r\n in the new line delimited JSON bulk request body.

If you save the file directly from GitHub with right-click > save, it'll be \n delimited. The alternative is to change the existing file you have to replace \r\n with \n.

This worked - the termination must be Unix. Changed with Notepad++
Thank you!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.