Elasticsearch批量获取Multi GET API和批量操作Bulk API用法

Multi GET API
Bulk API

批量获取Multi GET API

mget API参数是一个docs数组,数组的每个节点定义一个文档的_index_type_id元数据。

mget可以有三种请求头 :

GET /_mget             #不指定index
GET /test/_mget        #指定index 
GET /test/type/_mget   #指定index和type 

用法示例:

GET /_mget
{
    "docs" : [
        {
            "_index" : "library",
            "_type" : "books",
            "_id" : "1",
            "_source" : "price"
        },
        {
            "_index" : "library",
            "_type" : "books",
            "_id" : "2",
            "_source" : ["title", "price"]    #使用数组方式指定多个字段
        },
        {
            "_index" : "myindex",
            "_type" : "doc",
            "_id" : "2"
        }
    ]
}

获取相同index相同type下不同ID的文档:

GET /library/books/_mget
{
    "ids": ["1","3"]
}   

批量操作Bulk API
为了实现多个文档的createindexupdatedelete
请求体格式:

{action:{metadata}}\n
{request body}\n
{action:{metadata}}\n
{request body}\n
...
action解释说明
create当文档不存在时创建
index创建新文档或替换已有文档
update局部更新文档
delete删除一个文档

bulk请求的路径有三种和前面的mget的请求类似:

GET /_bulk             #不指定index
GET /test/_bulk        #指定index 
GET /test/type/_bulk   #指定index和type 

用法示例:

POST /library/books/_bulk
{"index":{"_id":4}}
{"title":"es4","price":4}
{"index":{"_id":5}}
{"title":"es5","price":5}
{"index":{"_id":6}}
{"title":"es6","price":6}

可以用mget获取一下插入结果:

GET /library/books/_mget
{ 
  "ids": ["4","5","6"]
}

针对批量的deleteupdate操作示例:

POST /library/books/_bulk
{"delete":{"_index": "library","_type": "books","_id":"1"}}
{"update":{"_index": "library","_type": "books","_id":"4"}}
{"doc": {"price": "18"}}

(1)index 和 create 第二行是request body数据体
(2)delete 没有第二行
(3)update 第二行可以是partial doc,upsert或者是script

批处理,加载本地accounts.json到集群:

curl -H "Content-Type: application/json" -XPOST '192.168.20.60:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"

bulk处理文档大小的最佳值:
数据加载在每个节点里的RAM里,请求的数据超过一定的大小,那bulk的处理性能就会降低,文档数据大小跟硬件配置,文档复杂度,以及当前集群的负载有关。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值