社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Elasticsearch

为什么ElasticSearch批量API使用“content-type:application/json”头?

Phil • 7 年前 • 2484 次点击  

我只是想知道,如果请求的主体不是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那里发送,curl不能验证body语法。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30725
文章 [ 1 ]  |  最新文章 7 年前
Val
Reply   •   1 楼
Val    7 年前

技术上讲,当呼叫 _bulk 终结点,内容类型头应为 application/x-ndjson 而不是 application/json as stated in their docs

最后一行数据必须以换行符结尾。每个换行符前面都可以有一个回车符。向此端点发送请求时,Content-Type头应设置为application/x-ndjson。

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

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