(二)Elasticsearch学习笔记:CURD

Create

PUT http://192.168.56.101:9200/school/student/1,自动创建school索引,学生类型,最后指定id为1

1
2
3
4
5
6
7
{
"name":"许薇",
"class":"计算机科学与技术1班",
"sc_id":"001",
"from":"广州",
"birth":"1993-1-1"
}

成功添加后返回下面JSON内容

1
2
3
4
5
6
7
8
9
10
11
12
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}

Update

POST http://192.168.56.101:9200/school/student/1/_update,发送要更新的字段,例如

1
2
3
{
"script":"ctx._source.from=\"深圳\""
}

更新成功后,_version字段会+1

1
2
3
4
5
6
7
8
9
10
11
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 4,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}

再次查询,可以看到from字段是更新了的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 4,
"found": true,
"_source": {
"name": "许薇",
"class": "计算机科学与技术1班",
"sc_id": "001",
"from": "深圳",
"birth": "1993-1-1"
}
}

Read

GET http://192.168.56.101:9200/school/student/1,成功查询到则返回下面JSON数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "许薇",
"class": "计算机科学与技术1班",
"sc_id": "001",
"from": "深圳",
"birth": "1993-1-1"
}
}

Delete

DELETE http://192.168.56.101:9200/school/student/2

成功删除后返回下面JSON信息,其中_version也会+1;

文档删除后,ES保留了删除文档的版本信息,所以仍然可以检查该版本号。

默认情况下,版本信息在删除的60秒内可用,可以通过index.gc_deletes修改可用时间。

1
2
3
4
5
6
7
8
9
10
11
12
{
"found": true,
"_index": "school",
"_type": "student",
"_id": "2",
"_version": 2,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}