Skip to content

文档基本操作

创建 _doc、_create

创建的关键字为 _create_doc

创建的时候,指定索引名,后面带上 _doc, 然后在请求体中写入对应的字段和值即可。

ES 会自动生成对应的文档 ID,文档 ID 为 _id 字段。

1
2
3
4
5
6
POST /<INDEX_NAME>/_doc
{
    "<FIELD_NAME>": "<VALUE>",
    "<FIELD_NAME>": "<VALUE>",
    ...
}
Example
1
2
3
4
5
6
7
8
9
POST {{host}}/product/_doc/ HTTP/1.1
Content-Type: application/json

{
    "title": "IPhone 12",
    "price": 8999.99,
    "description": "balabala",
    "create_at": "2022-07-17"
}
HTTP/1.1 201 Created
Location: /product/_doc/gbxXC4IBc5NJpEpg9EmE
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 163

{
    "_index": "product",
    "_type": "_doc",
    "_id": "gbxXC4IBc5NJpEpg9EmE",  // 自动生成的 ID
    "_version": 1,
    "result": "created",    // 表示创建成功,如果已有记录,则是 updated 更新成功
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

也可以创建的时候指定 _id

1
2
3
4
5
6
POST /<INDEX_NAME>/_doc/<_id>
{
    "<FIELD_NAME>": "<VALUE>",
    "<FIELD_NAME>": "<VALUE>",
    ...
}

Example
1
2
3
4
5
6
7
8
9
POST {{host}}/product/_doc/1 HTTP/1.1
Content-Type: application/json

{
    "title": "IPhone 11",
    "price": 7999.99,
    "description": "balabala11",
    "create_at": "2022-07-19"
}
HTTP/1.1 201 Created
Location: /product/_doc/1
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 140

{
    "_index": "product",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

更新 _doc、_update

更新有两种方式,一种是「全量更新」,也叫「覆盖更新」;一种是「增量更新」,只更新指定的字段。

更新有两个关键字:_update_doc

全量更新使用 _doc,增量更新使用 _update

下面这种方式是全量更新,是先删除原始文档,再将新的数据插入。

1
2
3
4
5
6
PUT /<INDEX_NAME>/_doc/<_id>

{
    "<FIELD_NAME>": "<VALUE>",
    ...
}

全量更新

我们先插入一条数据:

1
2
3
4
5
6
7
8
9
POST {{host}}/product/_doc/1 HTTP/1.1
Content-Type: application/json

{
    "title": "IPhone 11",
    "price": 7999.99,
    "description": "balabala11",
    "create_at": "2022-07-19"
}
HTTP/1.1 201 Created
Location: /product/_doc/1
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 139

{
    "_index": "product",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}

我们查看一下:

...
{
    ...
    "_source": {
        "title": "IPhone 11",
        "price": 7999.99,
        "description": "balabala11",
        "create_at": "2022-07-19"
    }
}

然后进行修改:

1
2
3
4
5
6
PUT {{host}}/product/_doc/1 HTTP/1.1
Content-Type: application/json

{
    "title": "IPhone 20"
}
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 141

{
    "_index": "product",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 11,
    "_primary_term": 1
}

然后再次查看:

1
2
3
4
5
6
7
...
{
    ...
    "_source": {
        "title": "IPhone 20"
    }
}

可以看到其他字段都没了。这种就是覆盖的方式进行更新。

下面这种方式是增量更新:

1
2
3
4
5
6
7
8
POST /<INDEX_NAME>/_update/<_id>

{
    "doc": {
        "<FIELD_NAME>": "<VALUE>",
        ...
    }
}
增量更新

我们先插入一条数据:

1
2
3
4
5
6
7
8
9
POST {{host}}/product/_doc/2 HTTP/1.1
Content-Type: application/json

{
    "title": "IPhone 11",
    "price": 7999.99,
    "description": "balabala11",
    "create_at": "2022-07-19"
}
HTTP/1.1 201 Created
Location: /product/_doc/2
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 142

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

我们查看一下:

...
{
    ...
    "_source": {
        "title": "IPhone 11",
        "price": 7999.99,
        "description": "balabala11",
        "create_at": "2022-07-19"
    }
}

然后进行修改:

1
2
3
4
5
6
7
8
POST {{host}}/product/_update/2 HTTP/1.1
Content-Type: application/json

{
    "doc": {
        "title": "IPhone 20"
    }
}
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 123

{
    "_index": "product",
    "_type": "_doc",
    "_id": "2",
    "_version": 10,
    "result": "noop",
    "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
    }
}

然后再次查看:

...
{
    ...
    "_source": {
        "title": "IPhone 20",
        "price": 7999.99,
        "description": "balabala11",
        "create_at": "2022-07-19"
    }
}

可以看到其他字段都在,这种就是增量的方式进行更新。

删除

DELETE /<INDEX_NAME>/_doc/<_id>
Example
DELETE {{host}}/product/_doc/1 HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 141

{
    "_index": "product",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

查看

基于 ID 查询

GET /<INDEX_NAME>/_doc/<_id>

example1
GET {{host}}/product/_doc/1 HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 192

{
    "_index": "product",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 4,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "IPhone 11",
        "price": 7999.99,
        "description": "balabala11",
        "create_at": "2022-07-19"
    }
}
example2
GET {{host}}/product/_doc/gbxXC4IBc5NJpEpg9EmE HTTP/1.1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 208

{
    "_index": "product",
    "_type": "_doc",
    "_id": "gbxXC4IBc5NJpEpg9EmE",
    "_version": 1,
    "_seq_no": 3,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "IPhone 12",
        "price": 8999.99,
        "description": "balabala",
        "create_at": "2022-07-17"
    }
}

批量操作 _bulk

批量操作的关键字为 _bulk

POST /_bulk

批量有更新、创建、删除的操作。通过多个 JSON 来表示。要注意的是 JSON 内部不可以换行,JSON之间必须换行。

  • 创建:

    1
    2
    3
    POST /<INDEX_NAME>/_bulk
    { "index": { "_index": <_index>, "_id": <_id> } }
    { "<FIELD>": "<VALUE>", ...}
    
    1
    2
    3
    POST /<INDEX_NAME>/_bulk
    { "create": { "_index": <_index>, "_id": <_id> } }
    { "<FIELD>": "<VALUE>", ...}
    

  • 更新:

    1
    2
    3
    POST /<INDEX_NAME>/_bulk
    { "update": { "_id": <_id>, "_index": <_index> } }
    { "doc": { "<FIELD>": "<VALUE>", ... } }
    

  • 删除:

    POST /<INDEX_NAME>/_bulk
    { "delete": { "_id": <_id>, "_index": <_index> } }
    

  • 可以混在同一个请求中:

    1
    2
    3
    4
    5
    6
    7
    8
    POST /<INDEX_NAME>/_bulk
    { "create": { "_index": <_index>, "_id": <_id> } }
      { "<FIELD>": "<VALUE>", ...}
    { "index": { "_index": <_index>, "_id": <_id> } }
      { "<FIELD>": "<VALUE>", ...}
    { "update": { "_id": <_id>, "_index": <_index> } }
      { "doc": { "<FIELD>": "<VALUE>", ... } }
    { "delete": { "_id": <_id>, "_index": <_index> } }
    

Example

1
2
3
4
POST {{host}}/product/_bulk HTTP/1.1
Content-Type: application/json

< req.json
1
2
3
4
{ "index": { "_id": 1 } }
{ "title": "Andriod1", "price": 1999.00, "description": "Androd yyds", "create_at": "2022-06-06"}
{ "create": { "_id": 2 } }
{ "title": "Iphone1", "price": 4999.00, "description": "Iphone yyds", "create_at": "2022-06-06"}

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-encoding: gzip
content-length: 189

{
"took": 11,
"errors": false,
"items": [
    {
        "index": {
            "_index": "product",
            "_type": "_doc",
            "_id": "1",
            "_version": 1,
            "result": "created",
            "_shards": {
                "total": 1,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 1,
            "_primary_term": 1,
            "status": 201
        }
    },
    {
        "create": {
            "_index": "product",
            "_type": "_doc",
            "_id": "2",
            "_version": 1,
            "result": "created",
            "_shards": {
                "total": 1,
                "successful": 1,
                "failed": 0
            },
            "_seq_no": 2,
            "_primary_term": 1,
            "status": 201
        }
    }
]
}

Warning

批量操作不会因为一个失败而全部失败,而是继续执行后续操作,最后返回所有处理结果。