es-文档的CRUD和批量操作-02

Elasticsearch 文档的CRUD和批量操作

文档的CRUD

Type 名,约定都用 _doc

  • index (如果ID不存在,创建新的文档。否则,先删除现有的文档,再创建新的文档,版本会增加)
    PUT my_index/_doc/1
    {“user”: “mike”, “comment”: “You know, for search”}

  • create (如果ID已经存在,会失败)
    PUT my_index/_create/1
    {“user”: “mike”, “comment”: “You know, for search”}

    POST my_index/_doc (不指定ID,自动生成)
    {“user”: “mike”, “comment”: “You know, for search”}

  • read
    GET my_index/_doc/1

  • update (文档必须已经存在,更新只会对响应字段做增量修改)
    POST my_index/_update/1
    { “doc”: {“user”: “mike”, “comment”: “You know, for Elasticsearch”}}

  • delete
    DELETE my_index/_doc/1

批量操作

Bulk API

  • 支持在一次api调用中,对不同的索引进行操作
  • 支持四中类型操作
    • index
    • create
    • update
    • delete
  • 可以在URI中指定index,也可以在请求的payload中进行
  • 操作中单挑操作失败,并不会影响其他操作
  • 返回结果包括了每一条操作执行的结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
POST _bulk
{ "index": { "_index": "test", "_id": "1"}}
{ "field1": "value1"}
{ "delete": {"_index": "test", "_id": "2"}}
{ "create": {"_index": "test2", "_id": "3"}}
{ "field1": "value3"}
{ "update": {"_id": "1", "_index": "test"}}
{ "doc": { "field2": "value2"}}

Response:
{
"took" : 361,
"errors" : true,
"items" : [
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 2,
"status" : 200
}
},
{
"delete" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 7,
"_primary_term" : 2,
"status" : 404
}
},
{
"create" : {
"_index" : "test2",
"_type" : "_doc",
"_id" : "3",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[3]: version conflict, document already exists (current version [1])",
"index_uuid" : "0OEwva4sTxeYKPqtUSNNkw",
"shard" : "0",
"index" : "test2"
}
}
},
{
"update" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 6,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 8,
"_primary_term" : 2,
"status" : 200
}
}
]
}

mget 批量读取

批量操作,可以减少网络连接所产生的开销,提高性能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
GET /_mget
{
"docs": [
{
"_index": "test",
"_id": "1"
},
{
"_index": "test2",
"_id": "2"
}
]
}

Response:
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_version" : 6,
"_seq_no" : 8,
"_primary_term" : 2,
"found" : true,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
},
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "2",
"found" : false
}
]
}

msearch 批量查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 批量读取
POST Kibana_sample_data_ecommerce/_msearch
{}
{ "query": {"match_all": {}}, "size":1 }
{ "index": "kibana_sample_data_flights"}
{ "query": {"match_all": {}}, "size": 2}


Response:
{
"took" : 7,
"responses" : [
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"field1" : "value1",
"field2" : "value2"
}
}
]
},
"status" : 200
},
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"field1" : "value3"
}
}
]
},
"status" : 200
}
]
}

常见错误返回

问题 原因
无法连接 网络故障或集群挂了
连接无法关闭 网络故障或节点出错
429 集群过于繁忙
4xx 请求体格式错误
500 集群内部错误

es-文档的CRUD和批量操作-02
http://example.com/2022/11/06/es/es-文档的CRUD和批量操作-02/
作者
weijun Wu
发布于
2022年11月6日
许可协议