0

I create a simple mapping:

curl -XPUT 'localhost:9200/ficherosindex?pretty=true' -d '{
  "mappings": {
    "items": {
       "dynamic": "strict",
       "properties" : {
            "title" : { "type": "string" },
            "body" : { "type": "string" },
            "attachments" : { "type": "attachment" }
}}}}'

I make PUT the title and the body, leaving attachments empty.

curl -XPUT 'localhost:9200/ficherosindex/items/1' -d '{
  "title": "This is a test title",
  "body" : "This is the body of the java",
  "attachments" : ""
}'

And then I make the following script to update the attachments fields with the content of the MY_PDF.pdf file, converting it to base64.

#!/bin/sh
coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
curl -X POST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -d '{
    "doc" : {
            "attachments" : \"${coded}\"
}}'

When I run the script I'm getting the following error:

{
  "error" : {
    "root_cause" : [ {
      "type" : "json_parse_exception",
      "reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@6c8caddf; line: 3, column: 30]"
    } ],
    "type" : "json_parse_exception",
    "reason" : "Unexpected character ('\\' (code 92)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@6c8caddf; line: 3, column: 30]"
  },
  "status" : 500
}

What I'm doing wrong? Maybe I've to change the following line?

{
    "doc" : {
       "attachments" : \"${coded}\"
}}'

I also tried this solution with no luck. I have to mantain the order I'm showing. First create the item without the attachments and then use the _update to append the content of the .PDF to it.

Thanks in advance

8
  • Try $(coded) instead of ${coded} and remove the backslashes in front of the double quotes: "attachments" : "$(coded)" Commented Sep 27, 2016 at 11:29
  • @Val It doesnt like to contain the $: Illegal character '$' (code 0x24) in base64 content\n at [Source: org.elasticsearch.common.io.stream.InputStreamStreamInput@43f0f1cc; line: 1, column: 87]" Commented Sep 27, 2016 at 11:58
  • Can you try this instead: "attachments" : ""$(coded)"" i.e. two double quotes Commented Sep 27, 2016 at 11:59
  • It finally worked with "'"$coded"'" . The problem now is that I'm getting the curl: Argument list too long error. It seems I've to work with files instead of loading the entire string on the fly. Commented Sep 27, 2016 at 12:11
  • Can you try to add -v to your curl to see the exact payload that is being sent, please? Commented Sep 27, 2016 at 12:15

1 Answer 1

1

Something like this should do:

#!/bin/sh
coded=`cat MY_PDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`

curl -XPOST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -H "Content-Type: application/json" -d @- <<CURL_DATA
{ "doc": { "attachments": "$coded" }}
CURL_DATA
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.