我只是想知道,如果请求的主体不是json而是具有多行的文本(每行都是json),为什么ES使用该 header 。例如:

{ "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接口(interface)中将某些内容定义为application / json是常见的,即使不是这样吗?您甚至不能仅通过cURL从Postman发送它,而cURL不验证正文语法。

最佳答案

从技术上讲,在调用_bulk端点时,内容类型 header 应为application/x-ndjson 而不是application/json as stated in their docs



它不是JSON数组的原因是,当协调节点接收到批量请求时,它可以简单地通过查看有多少行(即换行符)将其拆分为几个块,并将每个块发送到一个不同的节点来进行处理。处理。如果内容是JSON,则协调节点将必须全部解析它,并且对于几个兆字节的批量查询,这将对性能产生负面影响。

NDJSON是一种方便的格式,用于存储或流式传输可能一次处理一条记录的结构化数据。

关于json - 为什么Elasticsearch Bulk-API使用 “Content-Type: application/json” header ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54523844/

10-11 08:51