ElastiSearch--批量操作

mget,多条获取,批量查询多条数据,可减少网络开销次数。
批量查询id为1和2的数据

GET /_mget
{
  "docs":[
    {
      "_index":"test_index",
      "_type":"test_type",
      "_id":1
    },
    {
      "_index":"test_index",
      "_type":"test_type",
      "_id":2
    }
  ]  
}

返回的为两个结果

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "test_content": "my test1"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "test_content": "my test"
      }
    }
  ]
}

同一个index下可以简写为:

GET /test_index/_mget
{
  "docs":[
    {
      "_type":"test_type",
      "_id":1
    },
    {
      "_type":"test_type",
      "_id":2
    }
  ]  
}

同一个index和type下可以简写为:

GET /test_index/test_type/_mget
{
  "ids":[1,2]
}

批量bulk操作,需要注意的是,在kibana中,不能用json的格式,每个json串不能换行,只能放到一行。json串与json串之前必须要换行。
删除

POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":"3"}}

创建数据

POST /_bulk
{"create":{"_index":"test_index","_type":"test_type","_id":"12"}}
{"test_content":"test12"}

查询数据

POST /_bulk
{"index":{"_index":"test_index","_type":"test_type","_id":"13"}}
{"test_content":"test13"}

更新操作

POST /_bulk
{"update":{"_index":"test_index","_type":"test_type","_id":"13","_retry_on_conflict":3}}
{"doc":{"test_content":"buld test update01"}}

bulk中任意一个操作失败是不会影响到其他的,但是会在结果中显示。
json串操作的是同一个index和type,可以把index和type写到url中
如下

POST /test_index/test_type/_bulk
{"create":{"_index":"test_index","_type":"test_type","_id":"16"}}
{"test_content":"test16"}
{"delete":{"_index":"test_index","_type":"test_type","_id":"13"}}

bulk request会加载到内存中,如果太大的话,性能会降低。需要不停的测试性能大小。

多条搜索请求合并为一次,发送一次请求到es集群中。

POST /_msearch
{"index":"test_index","type":"test_type"}
{"query":{"match": {"test_content": "test16"}}}
{"index":"test_index","type":"test_type"}
{"query":{"match": {"test_content": "test13"}}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值