elasticsearch-文档API操作

elasticsearch-文档API操作

基于 7.12.x

新增

POST /book/_doc/1
{
    "name":"从入门到精通linux",
    "price": 100,
    "category":"tech"
}

# response
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

注意,如果id已经存在,es采用覆盖的形式(有点先删除,后插入的意思, 不过 _version 字段是一直递增)

查询

GET /book/_doc/1

# respnose
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 6,
    "_seq_no": 5,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux",
        "price": 100,
        "category": "tech"
    }
}

修改

两种方式

  • PUT 请求,对于相同 _id 多次更新, 先删除后插入,属性字段以最后一次请求的为主
PUT /book/_doc/1
{
    "name":"从入门到精通linux5",
    "price": 100,
    "category":"tech",
    "hello":1
}

# response
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 9,
    "_seq_no": 8,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux5",
        "price": 100,
        "category": "tech",
        "hello": 1
    }
}


# 第二次 PUT
PUT /book/_doc/1
{
    "name":"从入门到精通linux5",
    "price": 100,
    "category":"tech"
}
# response
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 10,
    "_seq_no": 9,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux5",
        "price": 100,
        "category": "tech"
    }
}

明显发现,字段 hello 在第二次 PUT 更新的时候被删除了

  • POST 请求,字段有则更新,无则插入
POST /book/_update/1
{
    "doc": {
        "name": "从入门到精通linux37",
        "price": 100,
        "category": "tech"
    }
}
# response
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 13,
    "_seq_no": 12,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux37",
        "price": 100,
        "category": "tech"
    }
}

# 第二次 POST update
POST /book/_update/1
{
    "doc": {
        "name": "从入门到精通linux37",
        "price": 100,
        "category": "tech",
        "hello":1
    }
}
# response
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 14,
    "_seq_no": 13,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux37",
        "price": 100,
        "category": "tech",
        "hello": 1
    }
}

# 第三次 POST update
POST /book/_update/1
{
    "doc": {
        "name": "从入门到精通linux38",
        "price": 100,
        "category": "tech"
    }
}
# response 
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 15,
    "_seq_no": 14,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "从入门到精通linux38",
        "price": 100,
        "category": "tech",
        "hello": 1
    }
}

可以发现, hello 字段新增后,后面更新条件中虽然删除了, 但是结果 hello 字段一直存在

删除

DELETE /book/_doc/1
{
    "_index": "book",
    "_type": "_doc",
    "_id": "1",
    "_version": 16,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 15,
    "_primary_term": 1
}

批量新增

POST /book/_bul      //注意格式紧凑
{ "index":{"_id":1} }
{"name":"从入门到精通java","price": 100,"category":"tech"}
{ "index":{"_id":2} }
{"name":"从入门到精通es","price": 100,"category":"tech"}
{ "index":{"_id":3} }
{"name":"隋唐英雄","price": 100,"category":"history"}

// response
{
    "took": 41,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 0,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 1,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 2,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值