gpt4 book ai didi

json - 为什么 Elasticsearch Bulk-API 使用 "Content-Type: application/json" header ?

转载 作者:行者123 更新时间:2023-12-02 22:43:14 24 4
gpt4 key购买 nike

我只是想知道如果请求的主体不是 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 是否很常见?您甚至不能从 Postman 发送它,只能从不验证正文语法的 cURL 发送。

最佳答案

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

the final line of data must end with a newline character \n. Each newline character may be preceded by a carriage return \r. When sending requests to this endpoint the Content-Type header should be set to application/x-ndjson.



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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com