Index a new document

Hi All,

I am trying to index a document into the customer index using the below command as suggested in the elasticsearch guide:
curl -XPUT localhost:9200/customer/external/1?Gibbon -d '{ "name": "John Doe"}'

But wen I execute this command, I am getting the below error:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to p
arse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by"
:{"type":"not_x_content_exception","reason":"Compressor detection can only be ca
lled on some xcontent bytes or compressed xcontent bytes"}},"status":400}curl: (
6) Could not resolve host: name
curl: (3) [globbing] unmatched close brace/bracket in column 9

Can anyone help me to solve this.

The index is getting created properly.

Where did you see this exact command? Can not find it in the guide.

Hi David,

I am refering the online documentation for elasticsearch. Please refer the below url which mentions this command:
https://www.elastic.co/guide/en/elasticsearch/reference/current/_index_and_query_a_document.html

Can you please share the documents for the same if you have any, as i am a beginner in this tool?

Thanks.

So this line does not come from the documentation:

curl -XPUT localhost:9200/customer/external/1?Gibbon -d '{ "name": "John Doe"}'

Try this:

curl -XPUT localhost:9200/customer/external/1 -d '{ "name": "John Doe"}'

Thanks David but the suggested command did not work. Showing below error:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to p
arse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by"
:{"type":"not_x_content_exception","reason":"Compressor detection can only be ca
lled on some xcontent bytes or compressed xcontent bytes"}},"status":400}curl: (
6) Could not resolve host: name
curl: (3) [globbing] unmatched close brace/bracket in column 9

Can you do a print screen of what you are doing. I have hard time understanding.

Ok. So curl on windows really sucks. You should use a REST plugin for your browser to run REST calls.
I personally use SENSE as it allows to copy CURL commands directly.

1 Like

I had the same poblem on Windows 8.1 and could solve it the following way:

  1. Use " instead of ' to wrap the JSON document to be inserted
  2. Mask the " within the document with "

So, your example should then look like this:
curl -XPUT localhost:9200/customer/external/1 -d "{ "name": "John Doe"}"

Good luck!

1 Like

For Python users with requests library:

python requests.put() (and post()) can give you the same error. This thread made me realize that the data=document parameter to requests must be a JSON string and not a dict object.

The dict object gets packed by *requests. Maybe it's always form-encoded. Anyway, Elasticsearch doesn't like it.

Dump the dict* as JSON first!

url = "http://localhost:9200/the_index/invoices/9780349139036"
r = requests.put(url, data=json.dumps(doc_dict), headers=JSON_HEADERS)

8 Likes

Thanks, that works for me

Yes looks like some issue with curl on windows. Sense worked. Thanks

That was the solution. You're awesome. Thanks.

I know this is an old post. Just for completeness, windows does not support CURL commands natively. If you wish to use curl commands, I would recommend installing the 3rd party cURL using the following steps:

  1. Download the appropriate CAB file for your system architecture from the following link: https://curl.haxx.se/download.html.
  2. Extract the file into the c:\windows\system32 folder.
  3. Open a Command prompt, and navigate to c:\windows\system32, and issue the appropriate cURL commands.

However, when in a Powershell console, or ISE, the Invoke-WebRequest cmdlet has an alias called 'curl', so should not be confused with the *nix equivalent. To create an index using Powershell's Invoke-Webrequest, use the following:

Invoke-WebRequest -Method PUT -Uri http://localhost:9200/<index>

List your indices as follows:

Invoke-WebRequest –Method GET –Uri http://localhost:9200/_cat/indices?v | Select Content | Format-List

Index a document:

$name = @{"name" = "John Doe"}
$json = $name | ConvertTo-Json

Invoke-WebRequest -Method PUT -Uri "http://localhost:9200/customer/external/1" -Body $json -ContentType 'application/json'