简体   繁体   English

为什么Elasticsearch Bulk-API使用“ Content-Type:应用程序/ json”标头?

[英]Why does Elasticsearch Bulk-API use “Content-Type: application/json” header?

I am just wondering why ES uses that header if the body of the request is not a json but text with multiple lines, each of which is a json. 我只是想知道为什么如果请求的主体不是json而是具有多行的文本(每行都是json),则ES为什么使用该标头。 For example: 例如:

{ "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"] }

This would be a valid request despite not being a well-formatted json. 尽管不是格式正确的json,这仍将是有效的请求。 Is it common in RESTful interfaces to define something as application/json even if it's not? 在RESTful接口中将某些内容定义为application / json是常见的,即使不是这样吗? You can't even send it from Postman, only from cURL, which does not validate the body syntax. 您甚至不能从Postman发送它,而只能从cURL发送它,这不验证主体语法。

Technically, when calling the _bulk endpoint, the content type header should be application/x-ndjson and not application/json as stated in their docs 从技术上讲,在调用_bulk端点时,内容类型标头应为application/x-ndjson而不是其文档中所述的 application/json

the final line of data must end with a newline character \\n. 最后一行数据必须以换行符\\ n结尾。 Each newline character may be preceded by a carriage return \\r. 每个换行符前面都可以有一个回车符\\ r。 When sending requests to this endpoint the Content-Type header should be set to application/x-ndjson. 向此端点发送请求时,Content-Type标头应设置为application / x-ndjson。

The reason it is not a JSON array is because when the coordinating node receives the bulk request, it can split it in several chunks simply by looking at how many lines (ie new line characters) there are and send each chunk to a different node for processing. 它不是JSON数组的原因是,当协调节点接收到批量请求时,它可以简单地通过查看有多少行(即换行符)将其拆分为几个块,并将每个块发送到一个不同的节点以进行处理。处理。 If the content was JSON, the coordinating node would have to parse it all and for several megabyte bulk queries, it would have a negative impact on performance. 如果内容是JSON,则协调节点将必须全部解析它,并且对于几个兆字节的批量查询,这将对性能产生负面影响。

NDJSON is a convenient format for storing or streaming structured data that may be processed one record at a time. NDJSON是一种方便的格式,用于存储或流式传输可能一次处理一条记录的结构化数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点Elasticsearch - 批量索引不起作用 - 不支持Content-Type标头[application / x-ldjson] - Node Elasticsearch - Bulk indexing not working - Content-Type header [application/x-ldjson] is not supported 响应标头不返回Content-Type application / json - Response header does not return Content-Type application/json 在标头中设置Content-Type / application / json - Setting Content-Type / application/json in header `header(“Content-type:application / json”)的用法;` - The usage of `header(“Content-type:application/json”);` 为什么 header 中的内容类型仍然是 text/plain 虽然我将其设置为 application/json - Why content-type in header remains text/plain though I set it as application/json 如何在响应头中将Content-Type:application / json更改为html - How to change Content-Type:application/json into html in response header 标头Content-Type:application / json返回text / html - header Content-Type: application/json returning text/html PHP header('Content-Type: application/json'); 不管用 - PHP header('Content-Type: application/json'); is not working 设置内容类型标头以使用JSON和Swift 3 + AlamoFire - Setting content-type header to use JSON with Swift 3 + AlamoFire 如何使用提取以应用程序/ JSON的内容类型发布 - How to use fetch to post with content-type of application/json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM