Error putting base64 converted string into Elasticsearch

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

This?

curl -X POST 'localhost:9200/ficherosindex/items/1/_update?pretty=true' -d '{
        "doc" : {
                "attachments" : "${coded}"
    }}'

Thanks @dadoonet

I finally solved it using the following script as the content of coded needs to be loaded from a file. Otherwise you get a curl: Argument list too long error.

#!/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

Please format your code using </> icon as explained in this guide. It will make your post more readable.

Probably you should run something like:

var=$(base64 "$filename"| perl -pe 's/\n//g');
var1=$(curl -XPOST 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -H "Content-Type: application/json" -d @- <<CURL_DATA
{ "data": "$var" }
CURL_DATA)
1 Like