众所周知,Elasticsearch的语法还是很恶心的。因此需要大量的调试,有些人在kibana上进行调试,但是如果想使用Postman进行调试时,大部分还是没有问题的。主要是bulk操作,有一些特殊性。关键问题在于换行这里。
如果你要使用postman调试时,如果有以下报错,希望可以帮助到你
以下方案以Elasticsearch7.10版本官方文档Elasticsearch7.10 Bulk为例
官方示例
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
第二句大致翻译为:actions 使用换行符分隔的 JSON (NDJSON) 结构在请求正文中指定:
翻译:数据的最后一行必须以换行符 \n
结尾。每个换行符前面可以有一个回车符 \r
。向 _bulk 端点发送请求时,应将 Content-Type 标头设置为 application/x-ndjson
。
官网说的很明白,就是每个插入以及最后一行都必须有换行!!我们可以将最后一行空出来
结果成功 !!!
当然还有另外一种导入方式就是选择二进制文件的导入方式。
第三种方法是使用curl的方式
如果要向 curl 提供文本文件输入,则必须使用 --data-binary
标志,而不是普通的 -d
。后者不保留换行符。例:
$ cat requests
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
$ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
{"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}
挺麻烦的,不建议使用