无法将文档批量插入弹性搜索索引:“非法参数异常:批量请求必须由换行符终止”

时间:2018-11-25 16:42:00

标签: json elasticsearch curl

我想将包含Error Domain=NSCocoaErrorDomain Code=4864 "Expected to decode Array<Any> but found a dictionary instead." UserInfo={NSCodingPath=( ), NSDebugDescription=Expected to decode Array<Any> but found a dictionary instead.} 的文档插入我的Elasticsearch索引。我的json文件看起来像这样:

my_docs.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

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

该如何解决?

1 个答案:

答案 0 :(得分:0)

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。下一行是文档的实际数据。