elasticsearch(六)----批量操作与分页

_bulk操作

  • 在Elasticsearch中,支持批量的插入、修改、删除操作,都是通过_bulk的api完成的。
  • 请求格式如下:
{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...
批量插入
  • post http://localhost:9200/haoke/user/_bulk
{"create":{"_index":"haoke","_type":"user","_id":2001}}
{"id":2001,"name":"name1","age": 20,"sex": "男"} 
{"create":{"_index":"haoke","_type":"user","_id":2002}}
{"id":2002,"name":"name2","age": 20,"sex": "男"} 
{"create":{"_index":"haoke","_type":"user","_id":2003}} 
{"id":2003,"name":"name3","age": 20,"sex": "男"}

在这里插入图片描述

  • 响应
{
    "took": 17,
    "errors": false,
    "items": [
        {
            "create": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2001",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 4,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2002",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 5,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2003",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 6,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}

批量删除
  • post http://localhost:9200/haoke/user/_bulk
  • 由于delete没有请求体,所以,action的下一行直接就是下一个action。
{"delete":{"_index":"haoke","_type":"user","_id":2001}} 
{"delete":{"_index":"haoke","_type":"user","_id":2002}} 
{"delete":{"_index":"haoke","_type":"user","_id":2003}}

在这里插入图片描述

  • 响应数据
{
    "took": 47,
    "errors": false,
    "items": [
        {
            "delete": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2001",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 7,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2002",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 8,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "haoke",
                "_type": "user",
                "_id": "2003",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 9,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}

分页

  • 和SQL使用LIMIT关键字返回只有一页的结果一样,Elasticsearch接受from和size参数:
    • size: 返回数据条数,默认10
    • from: 跳过开始的结果数,默认0
  • 如果你想每页显示5个结果,页码从1到3,那请求如下:
GET localhost:9200/haoke/user/_search?size=5 
GET localhost:9200/haoke/user/_search?size=5&from=5 
GET localhost:9200/haoke/user/_search?size=5&from=10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值