我想使用批量API将以下文件添加到Elasticsearch:
{"_id":{"date":"01-2007","profile":"Da","dgo":"DGO_E_AIEG","consumerType":"residential"},"value":{"min":120.42509,"minKwh":0.20071,"nbItems":6.0}}
使用命令
curl -XPOST -H 'Content-Type: application/json' localhost:9200/_bulk --data-binary Downloads/bob/test.json
但我犯了以下错误:
{"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}
注意:文件末尾显示空行
答案 0 :(得分:2)
在文档中说:
注意:最后一行数据必须以换行符结尾\ n。
上面有一个例子,说明文件的样子。 https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html。也许在每行末尾添加'
可以解决问题。
<强>更新强>
将数据放入JSON文件的方式可能有问题。例如,以下数据位于\n
:
example.json
运行以下{ "index" : { "_index" : "example", "_type" : "doc", "_id" : "1" } }
{ "field1" : "value1" }
<space here>
命令时,它可以工作:
curl
可能是您未在JSON文件中包含重要内容,或者您没有&#34; @ your_file.json&#34;,或者像提到的其他海报一样,您不能#39; t的内容类型为curl -X POST -H "Content-Type: application/x-ndjson" localhost:9200/_bulk --data-binary "@example.json"
。
答案 1 :(得分:2)
答案很简单
{ "index":{ "_index":"schools_gov", "_type":"school", "_id":"1" } }
{ "name":"Model School", "city":"Hyderabad"}
{ "index":{ "_index":"schools_gov", "_type":"school", "_id":"2" } }
{ "name":"Government School", "city":"Pune"}
不会起作用,但下面的json会起作用
{ "index":{ "_index":"schools_gov", "_type":"school", "_id":"1" } }
{ "name":"Model School", "city":"Hyderabad"}
{ "index":{ "_index":"schools_gov", "_type":"school", "_id":"2" } }
{ "name":"Government School", "city":"Pune"}
//Give a new line here. Not '\n' but the actual new line.
HTTP命令为 POST http://localhost:9200/schools_gov/_bulk
答案 2 :(得分:0)
如错误所述,您只需在文件末尾添加一个新行。 如果您使用的是* nix系统,则可以执行以下操作:
echo "\n" >> Downloads/bob/test.json
此外,正如文档https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html中所述,Content-Type
应为application/x-ndjson
向此端点发送请求时,Content-Type标头应该 设置为application / x-ndjson
所以命令应该是:
curl -XPOST -H 'Content-Type: application/x-ndjson' localhost:9200/_bulk --data-binary Downloads/bob/test.json
答案 3 :(得分:0)
该错误消息非常令人困惑。我输入了-data-binary
并收到了相同的消息。消息使我误入歧途。