Elasticsearch批量操作bulk
1.bulk请求体格式(不可以美化展示)
{action:{metadata }}\n
{request body }\n
{action: {netadata }}\n
{request body }\n
……
例如:
{“delete”: {“_index”:”library”,”_type”:”books”,”_id”:”1”}}
action(行为) | 解释
create |当文档不存在是创建之
index |创建新文档或替换已有文档
update |局部更新文档
delete |删除一个文档
#多重模式
#批量操作 _bulk
POST /library/books/_bulk
{"index": {"_id":1}}
{"title": "Elasticsearch:The Definitive Guide","price":5}
{"index": {"_id":2}}
{"title": "The Elasticsearch cookbook","price":6}
{"index": {"_id":3}}
{"title": "Elasticsearch Blueprinnts","price":7}
{"index": {"_id":4}}
{"title": "Thinking in Python","price":8}
{"index": {"_id":5}}
{"title": "Thinking in Java","price":9}
GET /library/
GET /library/books/_mget
{
"ids": ["1","2","3","4","5"]
}
# delete操作 和 update操作
# zhuyi注意delete下面没有具体的repuest body
POST /library/books/_bulk
{"delete": {"_index": "library", "_type": "books", "_id":"1"}}
{"create": "_index": "music", "_type": "classical", "_id":"1"}}
{"title":"Ave Verum Corpus"}
{"index": {"_index": "music", "_type": "classical"}}
{"title":"Litaniac de Veneravili Altaris Sacromento"}
{"update":{"_index": "library", "_type": "books", "_id":"2"}}
{"doc": {"price":"18"}}
GET /library/books/1
GET /library/books/_mget
{
"ids":["1","2","3","4","5"]
}
_bulk是为了实现多个文档的create/index/update或者delete。
_bulk处理文档大小的最佳值:
数据加载在每个节点里的RAM里
请求的数据超过一定的大小,那bulk的处理性能就会降低
文档数据大小跟硬件配置,文档复杂度,以及当前集群的负载密切相关