我只是想知道,如果请求的主体不是json而是具有多行的文本(每行都是json),为什么ES会使用该标头。例如:
{ "create" : { "_index" : "movies", "_type" : "movie", "_id" : "135569" } }
{ "id": "135569", "title" : "Star Trek Beyond", "year":2016 , "genre":["Action", "Adventure", "Sci-Fi"] }
{ "create" : { "_index" : "movies", "_type" : "movie", "_id" : "122886" } }
{ "id": "122886", "title" : "Star Wars: Episode VII - The Force Awakens", "year":2015 , "genre":["Action", "Adventure", "Fantasy", "Sci-Fi", "IMAX"] }
{ "create" : { "_index" : "movies", "_type" : "movie", "_id" : "109487" } }
{ "id": "109487", "title" : "Interstellar", "year":2014 , "genre":["Sci-Fi", "IMAX"] }
{ "create" : { "_index" : "movies", "_type" : "movie", "_id" : "58559" } }
{ "id": "58559", "title" : "Dark Knight, The", "year":2008 , "genre":["Action", "Crime", "Drama", "IMAX"] }
{ "create" : { "_index" : "movies", "_type" : "movie", "_id" : "1924" } }
{ "id": "1924", "title" : "Plan 9 from Outer Space", "year":1959 , "genre":["Horror", "Sci-Fi"] }
尽管不是格式正确的json,这将是一个有效的请求。在RESTful接口中将某些内容定义为application / json是常见的,即使不是这样吗?您甚至不能仅从cURL发送来自Postman的邮件,而该URL无法验证正文语法。
答案 0 :(得分:3)
从技术上讲,在调用_bulk
端点时,内容类型标头应为application/x-ndjson
而不是application/json
as stated in their docs
最后一行数据必须以换行符\ n结尾。每个换行符前面都可以有一个回车符\ r。在向该端点发送请求时,Content-Type标头应设置为application / x-ndjson。
之所以不是JSON数组,是因为当协调节点接收到批量请求时,它可以简单地通过查看有多少行(即新行字符)将其拆分为几个块并将每个块发送到一个不同的节点进行处理。如果内容是JSON,则协调节点将必须全部解析它,并且对于几个兆字节的批量查询,这将对性能产生负面影响。
NDJSON是一种方便的格式,用于存储或流式传输可能一次处理一条记录的结构化数据。