创建一条数据和更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14penn@ubuntu:~$ curl -XPUT '127.0.0.1:9200/customer/external/1?pretty&pretty' -d '{ "name": "penn" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}1
2
3
4
5
6
7
8
9
10
11
12
13
14penn@ubuntu:~$ curl -XPUT '127.0.0.1:9200/customer/external/1?pretty&pretty' -d '{ "name": "peng" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 2, //version +1
"result" : "updated", //数据覆盖更新
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false
}1
2
3
4
5
6
7
8
9
10
11
12
13
14penn@ubuntu:~$ curl -XPOST 'localhost:9200/customer/external?pretty&pretty' -d '{ "name": "penn" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "AVhU7yUoaCtZxg-qM7ht", //不指定ID,ES随机分配一个ID
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}Updating Documents
1
Whenever we do an update, Elasticsearch deletes the old document and then indexes a new document with the update applied to it in one shot.
1
2
3
4
5
6
7
8
9
10
11penn@ubuntu:~$ curl -XGET '127.0.0.1:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 3, //注意version变化
"found" : true,
"_source" : {
"name" : "penn" //内容
}
}1
2
3
4
5
6
7
8
9
10
11
12
13penn@ubuntu:~$ curl -XPOST '127.0.0.1:9200/customer/external/1/_update?pretty&pretty' -d '{ "doc": { "name": "peng" } }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 4, //version + 1
"result" : "updated", //操作状态
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}1
2
3
4
5
6
7
8
9
10
11penn@ubuntu:~$ curl -XGET '127.0.0.1:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 4,
"found" : true,
"_source" : {
"name" : "peng" //内容更改
}
}1
2
3
4
5
6
7
8
9
10
11
12
13penn@ubuntu:~$ curl -XPOST '127.0.0.1:9200/customer/external/1/_update?pretty&pretty' -d '{ "doc": { "name": "peng", "age": 27 } }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}1
2
3
4
5
6
7
8
9
10
11
12penn@ubuntu:~$ curl -XGET '127.0.0.1:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 5,
"found" : true,
"_source" : {
"name" : "peng",
"age" : 27
}
}1
2
3
4
5
6
7
8
9
10
11
12
13penn@ubuntu:~$ curl -XPOST '127.0.0.1:9200/customer/external/1/_update?pretty&pretty' -d '{ "script" : "ctx._source.age += 5" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 6,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}1
2
3
4
5
6
7
8
9
10
11
12penn@ubuntu:~$ curl -XGET '127.0.0.1:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 6,
"found" : true,
"_source" : {
"name" : "peng",
"age" : 32
}
}Deleting Documents
1
2
3
4
5
6
7
8
9
10
11
12
13
14penn@ubuntu:~$ curl -XDELETE '127.0.0.1:9200/customer/external/2?pretty'
{
"found" : false,
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}1
2
3
4
5
6
7penn@ubuntu:~$ curl -XGET '127.0.0.1:9200/customer/external/2?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"found" : false
}Batch Processing
1
2The bulk API executes all the actions sequentially and in order.
If a single action fails for whatever reason, it will continue to process the remainder of the actions after it.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
27penn@ubuntu:~$ curl -XPOST '127.0.0.1:9200/customer/external/_bulk?pretty&pretty' -d'
> {"index":{"_id":"1"}}
> {"name": "penn" }
> {"index":{"_id":"2"}}
> {"name": "penn" }'
{
"took" : 186,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 9,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
}
]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25penn@ubuntu:~$ curl -XPOST '127.0.0.1:9200/customer/external/_bulk?pretty&pretty' -d'
> {"update":{"_id":"1"}}
> {"doc": { "name": "John Doe becomes Jane Doe" } }
> {"delete":{"_id":"2"}}'
{
"took" : 61,
"errors" : false,
"items" : [
{
"update" : {
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 10,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"status" : 200
}
}
]
}