【问题标题】:Unable to bulk insert docs into elastic search index: "Illegal Argument Exception: bulk request must be terminated by newline"无法将文档批量插入弹性搜索索引:“非法参数异常:批量请求必须由换行符终止”
【发布时间】:2019-04-27 10:06:22
【问题描述】:

我想将包含我的my_docs.json 的文档插入到我的弹性搜索索引中。我的 json 文件如下所示:

{"_index":"twitter","_type":"_doc","_id":"v8XSJ2cB_TizemYYi5mW","_score":1,"_source":{"user":"me","c":"2018-11-18T17:16:08.953Z","content":"this is cool stuff"}}

{"_index":"twitter","_type":"_doc","_id":"x8XbJ2cB_TizemYYxZmf","_score":1,"_source":{"user":"you","c":"2018-11-18T17:26:13.634Z","content":"this is some other cool stuff"}}

我试过了:curl -XPUT 'http://localhost:9200/twitter/_bulk?pretty&refresh' -H "Content-Type: application/json" -d @my_docs.json

twitter 是我的索引。

我收到一个错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "The bulk request must be terminated by a newline [\n]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "The bulk request must be terminated by a newline [\n]"
  },
  "status" : 400
}

我在文件末尾添加了一个“\n”,但问题仍然存在,并且出现了同样的错误。

我该如何解决这个问题?

【问题讨论】:

标签: json elasticsearch curl


【解决方案1】:
curl -H "Content-Type: application/json" -XPOST "localhost:9200/{index}/{type}/_bulk?pretty&refresh" --data-binary "@my_docs.json"

注意:json 文件应具有以下格式的内容才能使上述内容生效:

{"index":{"_index":"my_index","_type":"_doc","_id":"1"}}
{"field1":"field 1 data 1","field2":11}
{"index":{"_index":"my_index","_type":"_doc","_id":"2"}}
{"field1":"field 1 data 2","field2":21}`

上面的每个文档都用两行表示。第一行指示索引的位置以及文档 ID 是什么。下一行是文档的实际数据。

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 2022-06-16
    • 2018-03-10
    • 2016-04-13
    • 2017-07-18
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多