elasticsearch批量插入JSON文件

时间:2016-04-19 10:53:37

标签: json search elasticsearch bulkinsert

我有以下JSON file

我用awk去除空格,尾随,下一行

awk -v ORS= -v OFS= '{$1=$1}1' data.json

我在data.json的顶部添加了一个创建请求,后跟\ n和其余的数据。

{"create": {"_index":"socteam", "_type":"products"}} 

当我发出批量提交请求时,我收到以下错误

CURL -XPUT http://localhost:9200/_bulk

{
  "took": 1,
  "errors": true,
  "items": [
    {
      "create": {
        "_index": "socteam",
        "_type": "products",
        "_id": "AVQuGPff-1Y7OIPIJaLX",
        "status": 400,
        "error": {
          "type": "mapper_parsing_exception",
          "reason": "failed to parse",
          "caused_by": {
            "type": "not_x_content_exception",
            "reason": "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
          }
        }
      }
    }
  ]

对此错误的含义有何疑问?我没有创建任何映射,我正在使用vanilla elasticsearch

1 个答案:

答案 0 :(得分:1)

根据this doc,您必须在URL中指定索引和类型:

curl -XPUT 'localhost:9200/socteam/products/_bulk?pretty' --data-binary "@data.json"

适用于PUT和POST方法。
您的data.json文件应该具有如下结构:

{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

也许存在另一种导入数据的方法,但我知道这一点......希望它能帮助......