kuntao yu kuntao yu - 4 months ago 91
PowerShell Question

bulk data to ElasticSearch by powershell, but threw exception

when i import the data to ElasticSearch by powershell, it always throw exception, any one can help me?

code:

$a = "{'create':{'_index':'kuyu','_type':'test','_id':'4'}}`n{'title': 'this is a test', 'text': 'test', 'date': '2016-01-02'}".Replace("'","`"")
Invoke-RestMethod -Uri http://localhost:9200/_bulk -Method POST -Body $a


the exception is:

Invoke-RestMethod : {"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: no requests added;"},"status":400}
At line:1 char:1
+ Invoke-RestMethod -Uri http://localhost:9200/_bulk -Method POST -Bo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Val Val
Answer

I think you're simply missing a newline at the end of your request, try this:

$a = "{'create':{'_index':'kuyu','_type':'test','_id':'4'}}`n{'title': 'this is a test', 'text': 'test', 'date': '2016-01-02'}`n".Replace("'","`"")
Invoke-RestMethod -Uri http://localhost:9200/_bulk -Method POST -Body $a