Elasticsearch的CRUD

CRUD

动作 API
index PUT my_index/_doc/1
{“a”:”mike”,”b”,”bb”}
Create PUT my_index/_create/1
{…}
Read GET my_index/_doc/1
Update POST my_index/_update/1
{}
DELETE DELETE my_index/_doc/1

Create

1
2
3
4
5
PUT users/_create/1
{
"name":"zjj",
"age":"25"
}

  • 支持自动生成文档ID和指定文档ID2种方式
  • 调用 POST 动作系统会自动生成document id
  • 使用HTTP PUT 创建时,URL中显示指定 _create,如果ID文档存在,操作失败

GET

1
GET users/_doc/1

  • 找到文档,返回http 200
    • 文档元信息
      • _index/_type/
      • 版本信息,同一个ID的文档,即使被删除Version号也会不断真加
      • _souece 中默认包含了文档的所有原始信息
  • 找不到文档,返回http 404

Index

1
2
3
4
PUT users/_doc/1
{
"age": "30"
}

  • index 和 create 不一样的地方是,create如果存在该数据会报错,index会覆盖。

Update

1
2
3
4
5
6
POST users/_update/1
{
"doc":{
"name":"zjj"
}
}

  • update 和 index的区别是,index是覆盖操作,update会保留原来的数据

Bulk API

  • 在一次API调用中,对不同的索引进行操作
  • 支持CUD和INDEX操作
  • 单条失败其他不影响
  • 返回结果包括了每一条执行
1
2
3
4
5
6
POST _bulk
{ "create":{"_index":"users","_id":"5"}}
{ "name":"dx","age":"25"}
{"update":{"_index":"users","_id":"5"}}
{"name":"dxx"}
{"delete":{"_index":"users","_id":"2"}}

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
{
"took" : 61,
"errors" : false,
"items" : [
{
"create" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 15,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 16,
"_primary_term" : 1,
"status" : 200
}
}
]
}

mget

  • 批量按照ID获取数据
  • 有一些简洁的写法可以看文档
1
2
3
4
5
6
7
8
9
10
GET /_mget
{
"docs":[
{"_index":"users","_id":"1"},
{"_index":"users","_id":"2"},
{"_index":"users","_id":"3"},
{"_index":"users","_id":"4"},
{"_index":"users","_id":"5"}
]
}

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
{
"docs" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 12,
"_seq_no" : 11,
"_primary_term" : 1,
"found" : true,
"_source" : {
"age" : "30",
"name" : "zjj"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"found" : false
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"_seq_no" : 13,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "dx",
"age" : "25"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "4",
"found" : false
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 2,
"_seq_no" : 15,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "dxx",
"age" : "25"
}
}
]
}

msearch

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~