将base64转换后的字符串放入Elasticsearch时出错

时间:2016-09-27 10:38:38

标签: elasticsearch

我创建了一个简单的映射:

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

PUT titlebodyattachments为空。

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

然后我制作以下脚本,使用attachments文件的内容更新MY_PDF.pdf字段,并将其转换为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}\"
}}'

当我运行脚本时,我收到以下错误:

{
  "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
}

我做错了什么?也许我要更改以下行?

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

我也尝试过this解决方案而没有运气。我必须保留我正在展示的订单。首先创建没有attachments的项目,然后使用_update将.PDF的内容附加到其中。

提前致谢

1 个答案:

答案 0 :(得分:1)

这样的事情应该做:

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